September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,31 +1,28 @@
[[wp:Penney's_game|Penney's game]]   is a game where two players bet on being the first to see a particular sequence of   [[wp:Coin_flipping|heads or tails]]   in consecutive tosses of a   [[wp:Fair_coin|fair coin]].
[[wp:Penney's_game|Penney's game]] is a game where two players bet on being the first to see a particular sequence of [[wp:Coin_flipping|heads or tails]] in consecutive tosses of a [[wp:Fair_coin|fair coin]].
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or
'''HTH''' for short.
The other player on seeing the first players choice will choose his sequence.
The coin is tossed and the first player to see his sequence in the sequence of coin tosses wins.
Heads, Tails, Heads, or '''HTH''' for short.
The other player on seeing the first players choice will choose his sequence. The coin is tossed and the first player to see his sequence in the sequence of coin tosses wins.
;Example:
One player might choose the sequence   '''HHT'''   and the other   '''THT'''.  
One player might choose the sequence '''HHT''' and the other '''THT'''.
Successive coin tosses of   '''HTTHT'''   gives the win to the second player as the last three coin tosses are his sequence.
Successive coin tosses of '''HTTHT''' gives the win to the second player as the last three coin tosses are his sequence.
;Task:
;Task
Create a program that tosses the coin, keeps score and plays Penney's game against a human opponent.
:*   Who chooses and shows their sequence of three should be chosen randomly.
:*   If going first, the computer should randomly choose its sequence of three.
:*   If going second, the computer should automatically play [[wp:Penney's_game#Analysis_of_the_three-bit_game|the optimum sequence]].
:*   Successive coin tosses should be shown.
* Who chooses and shows their sequence of three should be chosen randomly.
* If going first, the computer should randomly choose its sequence of three.
* If going second, the computer should automatically play [[wp:Penney's_game#Analysis_of_the_three-bit_game|the optimum sequence]].
* Successive coin tosses should be shown.
<br>
Show output of a game where the computer chooses first and a game where the user goes first here on this page.
;See also:
* &nbsp; [https://www.youtube.com/watch?v=OcYnlSenF04 The Penney Ante Part 1] (Video).
* &nbsp; [https://www.youtube.com/watch?v=U9wak7g5yQA The Penney Ante Part 2] (Video).
;See also
* [https://www.youtube.com/watch?v=OcYnlSenF04 The Penney Ante Part 1] (Video).
* [https://www.youtube.com/watch?v=U9wak7g5yQA The Penney Ante Part 2] (Video).
<br><br>

View file

@ -0,0 +1,31 @@
10 IF RND>=.5 THEN GOTO 100
20 PRINT "YOU PICK FIRST."
30 INPUT P$
40 PRINT "YOU CHOSE ";P$
50 LET C$="H"
60 IF P$(2)="H" THEN LET C$="T"
70 LET C$=C$+P$( TO 2)
80 PRINT "I CHOOSE ";C$
90 GOTO 190
100 PRINT "I PICK FIRST."
110 LET C$=""
120 FOR I=1 TO 3
130 LET C$=C$+"H"
140 IF RND>=.5 THEN LET C$(I)="T"
150 NEXT I
160 PRINT "I CHOOSE ";C$
170 INPUT P$
180 PRINT "YOU CHOSE ";P$
190 LET G$=""
200 IF RND>=.5 THEN GOTO 230
210 LET G$=G$+"H"
220 GOTO 240
230 LET G$=G$+"T"
240 PRINT AT 5,0;G$
250 IF LEN G$<3 THEN GOTO 200
260 IF G$(LEN G$-2 TO )=P$ THEN GOTO 290
270 IF G$(LEN G$-2 TO )=C$ THEN GOTO 310
280 GOTO 200
290 PRINT "YOU WIN"
300 STOP
310 PRINT "I WIN"

View file

@ -0,0 +1,64 @@
(ns penney.core
(:gen-class))
(def heads \H)
(def tails \T)
(defn flip-coin []
(let [flip (rand-int 2)]
(if (= flip 0) heads tails)))
(defn turn [coin]
(if (= coin heads) tails heads))
(defn first-index [combo coll]
(some #(if (= (second %) combo) (first %)) coll))
(defn find-winner [h c]
(if (< h c)
(do (println "YOU WIN!\n") :human)
(do (println "COMPUTER WINS!\n") :computer)))
(defn flip-off [human comp]
(let [flips (repeatedly flip-coin)
idx-flips (map-indexed vector (partition 3 1 flips))
h (first-index (seq human) idx-flips)
c (first-index (seq comp) idx-flips)]
(println (format "Tosses: %s" (apply str (take (+ 3 (min h c)) flips))))
(find-winner h c)))
(defn valid? [combo]
(if (empty? combo) true (and (= 3 (count combo)) (every? #(or (= heads %) (= tails %)) combo))))
(defn ask-move []
(println "What sequence of 3 Heads/Tails do you choose?")
(let [input (clojure.string/upper-case (read-line))]
(if-not (valid? input) (recur) input)))
(defn optimize-against [combo]
(let [mid (nth combo 1)
comp (str (turn mid) (first combo) mid)]
(println (format "Computer chooses %s: " comp)) comp))
(defn initial-move [game]
(let [combo (apply str (repeatedly 3 flip-coin))]
(println "--------------")
(println (format "Current score | CPU: %s, You: %s" (:computer game) (:human game)))
(if (= (:first-player game) tails)
(do
(println "Computer goes first and chooses: " combo)
combo)
(println "YOU get to go first."))))
(defn play-game [game]
(let [c-move (initial-move game)
h-move (ask-move)]
(if-not (empty? h-move)
(let [winner (flip-off h-move (if (nil? c-move) (optimize-against h-move) c-move))]
(recur (assoc game winner (inc (winner game)) :first-player (flip-coin))))
(println "Thanks for playing!"))))
(defn -main [& args]
(println "Penney's Game.")
(play-game {:first-player (flip-coin)
:human 0, :computer 0}))

View file

@ -0,0 +1,87 @@
package main
import "fmt"
import "math/rand"
func main(){
var a1,a2,a3,y,match,j,k int
var inp string
y=1
for y==1{
fmt.Println("Enter your sequence:")
fmt.Scanln(&inp)
var Ai [3] int
var user [3] int
for j=0;j<3;j++{
if(inp[j]==104){
user[j]=1
}else{
user[j]=0
}
}
for k=0;k<3;k++{
Ai[k]=rand.Intn(2)
}
for user[0]==Ai[0]&&user[1]==Ai[1]&&user[2]==Ai[2]{
for k=0;k<3;k++{
Ai[k]=rand.Intn(2)
}
}
fmt.Println("You gave the sequence:")
printht(user)
fmt.Println()
fmt.Println("The computer generated sequence is:")
printht(Ai)
fmt.Println()
a1=rand.Intn(2)
a2=rand.Intn(2)
a3=rand.Intn(2)
fmt.Print("The generated sequence is:")
printh(a1)
printh(a2)
printh(a3)
match=0
for match==0{
if(matches(user,a1,a2,a3)==1){
fmt.Println()
fmt.Println("You have won!!!")
match=1
}else if(matches(Ai,a1,a2,a3)==1){
fmt.Println()
fmt.Println("You lost!! Computer wins")
match=1
}else{
a1=a2
a2=a3
a3=rand.Intn(2)
printh(a3)
}
}
fmt.Println("Do you want to continue(0/1):")
fmt.Scanln(&y)
}
}
func printht(a [3] int) int{
var i int
for i=0;i<3;i++{
if(a[i]==1){
fmt.Print("h")
}else{
fmt.Print("t")
}
}
return 1
}
func printh(a int) int{
if(a==1){
fmt.Print("h")
}else{
fmt.Print("t")
}
return 1
}
func matches(a [3] int,p int,q int,r int) int{
if(a[0]==p&&a[1]==q&&a[2]==r){
return 1
}else{
return 0
}
}

View file

@ -0,0 +1,88 @@
function penny_game()
local player, computer = "", ""
function player_choose()
io.write( "Enter your sequence of three H and/or T: " )
local t = io.read():upper()
if #t > 3 then t = t:sub( 1, 3 )
elseif #t < 3 then return ""
end
for i = 1, 3 do
c = t:sub( i, i )
if c ~= "T" and c ~= "H" then
print( "Just H's and T's!" )
return ""
end
end
return t
end
function computer_choose()
local t = ""
if #player > 0 then
if player:sub( 2, 2 ) == "T" then
t = "H"
else
t = "T";
end
t = t .. player:sub( 1, 2 )
else
for i = 1, 3 do
if math.random( 2 ) == 1 then
t = t .. "H"
else
t = t .. "T"
end
end
end
return t
end
if math.random( 2 ) == 1 then
computer = computer_choose()
io.write( "My sequence is: " .. computer .. "\n" )
while( true ) do
player = player_choose()
if player:len() == 3 then break end
end
else
while( true ) do
player = player_choose()
if player:len() == 3 then break end
end
computer = computer_choose()
io.write( "My sequence is: " .. computer .. "\n" )
end
local coin, i = "", 1
while( true ) do
if math.random( 2 ) == 1 then
coin = coin .. "T"
io.write( "T" )
else
coin = coin .. "H"
io.write( "H" )
end
if #coin > 2 then
local seq = coin:sub( i, i + 2 )
i = i + 1
if seq == player then
print( "\nPlayer WINS!!!" )
return 1
elseif seq == computer then
print( "\nComputer WINS!!!" )
return -1
end
end
end
end
math.randomseed( os.time() )
local cpu, user = 0, 0
repeat
r = penny_game()
if r > 0 then
user = user + 1
else
cpu = cpu + 1
end
print( "Player: " .. user .. " CPU: " .. cpu )
io.write( "Play again (Y/N)? " )
r = io.read()
until( r == "N" or r == "n" )

View file

@ -0,0 +1,18 @@
fcn coinToss{ (0).random(2) and "H" or "T" } // (0).random(2) --> 0<=r<2
reg myBet, yourBet;
if(coinToss()=="H"){ // The toss says I go first
myBet=(3).pump(String,coinToss);
println("I bet ",myBet);
yourBet=ask("Your bet of three (H/T): ")[0,3].toUpper();
}else{
yourBet=ask("Your bet of three (H/T): ")[0,3].toUpper();
myBet=((yourBet[1]=="H") and "T" or "H") + yourBet[0,2];
println("I bet ",myBet);
}
print("Flipping: "); coins:="";
while(1){
print(toss:=coinToss()); coins=coins + toss;
if(Void!=coins.find(yourBet)){ println(" You win!"); break; }
if(Void!=coins.find(myBet)) { println(" I win!"); break; }
// ignore we both won
}