Page 1 of 1

Little dice game

Posted: Tue Dec 19, 2017 2:14 pm
by Henko
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.

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


Re: Little dice game

Posted: Thu Dec 21, 2017 6:17 pm
by rbytes
It is nice to see a program post again. Thanks, Henko.

Little dice game shows how useful some of the higher ASCII characters can be, such as making the faces of dice.

I can't resist looking for a free sound effect of dice rolling that I could add. I might do that in the next few days, unless you want to. 🤠

Re: Little dice game

Posted: Sat Dec 23, 2017 10:09 pm
by rbytes
Here it is with a sound effect. The attached mp3 should be in the same location as the program.

Code: Select all

/*
Little Dice Game
by Henko
sound by rbytes
December 2017
*/

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
music "dice" load "dice.mp3"

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
music "dice" play
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

Re: Little dice game

Posted: Sun Dec 24, 2017 2:45 pm
by Henko
Hi Richard,
Maybe a stupid question: how do i copy dice.mp3 into the sB sandbox? I tried via Dropbox, but got a message that importing a program file is not allowed.
Mvg, Henk

Re: Little dice game

Posted: Sun Dec 24, 2017 3:13 pm
by rbytes
I just tested the usual method, and it worked for me.
1. Click on the link for "dice.mp3" on the Forum post
2. Choose Save to Dropbox
3. Choose Apps/Smart Basic as the Dropbox destination
4. Click the Save button
5. Then launch Smart Basic
6. Click the Dropbox button
7. Find "dice.mp3" and copy it
8. Click the Dropbox button again
Now you are in the Smart Basic sandbox
9.Navigate to where you want the file and Paste it

Re: Little dice game

Posted: Tue Dec 26, 2017 3:47 pm
by Henko
Thanks Richard,
I followed your steps carefully, and now it worked.

Next thing i will pick up is the wave generating program. I still have to build in the facility to save the generated wave to a .wav file and play it.

Mvg, Henk

Re: Little dice game

Posted: Tue Dec 26, 2017 4:19 pm
by rbytes
I'm glad it worked. What is the wave file you are generating?

Re: Little dice game

Posted: Tue Dec 26, 2017 5:08 pm
by Henko
It's generated by a program i submitted more than a year ago. With it, you build a wave patern by defining amplitude, frequency, and phase shift of sinus-, sawtooth-, triangular-, and blockwaves. Finally, noise can be added.
You can inspect the result in detail, a fast FFT is performed and shown.
The last thing is that you want to hear what you've made :lol: . That's what i will add.

Re: Little dice game

Posted: Tue Dec 26, 2017 5:22 pm
by rbytes
OK. I will only play your wave files for my dog if she chews up my slippers. ;)