Little dice game

Post Reply
Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Little dice game

Post 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


User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Little dice game

Post 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. 🤠
The only thing that gets me down is gravity...

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Little dice game

Post 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
Attachments
dice.mp3
(18.13 KiB) Downloaded 434 times
The only thing that gets me down is gravity...

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Little dice game

Post 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

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Little dice game

Post 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
The only thing that gets me down is gravity...

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Little dice game

Post 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

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Little dice game

Post by rbytes »

I'm glad it worked. What is the wave file you are generating?
The only thing that gets me down is gravity...

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Little dice game

Post 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.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Little dice game

Post by rbytes »

OK. I will only play your wave files for my dog if she chews up my slippers. ;)
The only thing that gets me down is gravity...

Post Reply