Little dice game
Posted: Tue Dec 19, 2017 2:14 pm
Throw a dice to gain 100 points
I encountered this little game in a popular math book.
In this version there are two players. You play against the computer.
Each turn you may throw the dice as many times as you want. The result is added to a turn total. If you stop the turn, the turn total is added to your game total. The player who gains a game total of at least 100 points is the winner.
However, if throwing the dice results in a 1, then the turn result so far is lost and the turn switches to the other player.
At the start of the game you have to enter the number of tries that the computer player is allowed during one turn.
A simple probability calculation shows that an optimal strategy exists (that is, how long should you the dice keep rolling during one turn?). If you keep it rolling for to many times, you will encounter a 1 sooner or later, and loose the acquired turn total.
It's your turn if the button "Throw dice" is visible.
I encountered this little game in a popular math book.
In this version there are two players. You play against the computer.
Each turn you may throw the dice as many times as you want. The result is added to a turn total. If you stop the turn, the turn total is added to your game total. The player who gains a game total of at least 100 points is the winner.
However, if throwing the dice results in a 1, then the turn result so far is lost and the turn switches to the other player.
At the start of the game you have to enter the number of tries that the computer player is allowed during one turn.
A simple probability calculation shows that an optimal strategy exists (that is, how long should you the dice keep rolling during one turn?). If you keep it rolling for to many times, you will encounter a 1 sooner or later, and loose the acquired turn total.
It's your turn if the button "Throw dice" is visible.
Code: Select all
input "strategy of computer player (stop after x throws) : x ": nc
graphics ! graphics clear ! draw color .4,.4,.4
draw text "computer stops after " & nc & " throws" at 200,10
randomize
set buttons font size 50
button "dice" text "" at 355,110 size 60,60
set buttons font size 30
button "go" text "Trow dice" at 20,50 size 150,50
button "stop" text "Stop!" at 200,50 size 150,50
button "comp" text "Computer" at 500,50 size 150,50
button "pd" text "" at 120,120 size 120,40
button "ps" text "" at 120,180 size 120,40
button "pt" text "" at 120,240 size 120,40
button "cd" text "" at 520,120 size 120,40
button "cs" text "" at 520,180 size 120,40
button "ct" text "" at 520,240 size 120,40
button "win" text "" at 255,300 size 250,50
button "win" hide
draw font size 32
draw text ". turn total ." at 245,185
draw text ". game total ." at 245,245
player_turn:
button "go" show
while 1
if bp("go") then
dice=throw() ! button "pd" text dice
if dice=1 then
ps_tot=0 ! button "ps" text 0
pause 1 ! goto computer_turn
end if
ps_tot+=dice ! button "ps" text ps_tot ! pause 1
end if
if bp("stop") then
pt_tot+=ps_tot ! ps_tot=0 ! button "pt" text pt_tot
if pt_tot>=100 then b_win("Player wins")
goto computer_turn ! end if
end while
computer_turn:
button "go" hide
for i=1 to nc
dice=throw() ! button "cd" text dice
if dice=1 then
cs_tot=0 ! button "cs" text 0
pause 1 ! goto player_turn
end if
cs_tot+=dice ! button "cs" text cs_tot ! pause 1
if ct_tot+cs_tot>=100 then break
next i
ct_tot+=cs_tot ! button "ct" text ct_tot ! cs_tot=0
if ct_tot>=100 then b_win("Computer wins")
goto player_turn
end
def bp(a$) = button_pressed(a$)
def throw ()
set buttons font size 50
for i=1 to 4+rnd(5)
dice=1+rnd(6) ! button "dice" text chr$(9855+dice)
pause .3
next i
draw font size 30
return dice
end def
def b_win(a$)
button "win" text a$ ! button "win" show
stop
end def