1. Re: Dice
- Posted by Irv Mullins <irv at ELLIJAY.COM>
Jun 05, 2000
-
Last edited Jun 06, 2000
On Sun, 04 Jun 2000, Alvin wrote:
>
> I'm a beginner and this is my first program. Anybody want to pick at
> it and help me do better? I also want to use the EX02.EXW example in Dave's
> Winlib to port this code in to so it'll run in winders[8*9 As of yet I don't
> know how. I'm also interested in using sprite graphics for dice.
Just to show that there are lots of different ways to write this in Euphoria,
here's my version, which uses a different concept.
Irv
include get.e -- for wait_key() function
include wildcard.e -- for upper() function
object list -- to store rolls of dice
constant TOTAL = 3, ROLL_AGAIN = 0, GAME_OVER = 1
function roll_dice() -- roll & display 2 dice, return dice and total
sequence dice
dice = {rand(6), rand(6),0}
dice[TOTAL] = dice[1] + dice[2]
printf(1,"Die 1: %d Die 2: %d for a total of %2d\n",dice)
return {dice}
end function
function play() -- ask for user input
integer key
puts(1,"Play? ")
key = wait_key()
puts(1,"\n")
return upper(key) = 'Y'
end function
function a_winner(object roll) -- rules of the game
integer first, last
first = 1
last = length(roll)
if roll[last][TOTAL] = 2 then
puts(1,"** SNAKE EYES **\n\n")
return GAME_OVER
end if
if last = 1 then -- this is first roll
if roll[last][TOTAL] = 7
or roll[last][TOTAL] = 11 then
puts(1,"** WINNER **\n\n")
return GAME_OVER
end if
else -- on subsequent rolls
if roll[last][TOTAL] = 7
or roll[last][TOTAL] = 11 then
puts(1,"** LOSER **\n\n")
return GAME_OVER
elsif roll[first][TOTAL] = roll[last][TOTAL] then
puts(1,"*** WINNER ***\n\n")
return GAME_OVER
end if
end if
return ROLL_AGAIN
end function
-- MAIN LOOP --
while play() do
list = roll_dice() -- get first roll
if not a_winner(list) then -- roll again
while 1 do
list &= roll_dice() -- append new roll to list
if a_winner(list) then exit
end if
end while
end if
end while -- play