1. Re: Dice version 3

On Sun, 4 Jun 2000 19:24:53 -0500, A.R.S. Alvin Koffman 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.

Well, I figure I'd better stop modifying it before it gets to a point where
you don't recognize it.  I tried putting in as many comments as possible.
I basically re-did it to get rid of all global variables (I don't like
global variables when they are not needed).  I also cleaned up the main
program loop.

If you have any questions about what I've done just let me know at
bkb at cnw.com

            -- Dice version 3 by
            -- Joe Schwenk jschwenk at hsonline.net
            -- Alvin Koffman ka9qlq at hotmail.com
                  -- with trace
                  -- trace(1)

include graphics.e -- include graphics
include get.e      -- include keyboard input

constant
  --BKB: not necessary for such a small program but by making these messages
  --     constants you can change them easily by going to this one spot...
  Msg_Winner = "\t*****WINNER!!*****\n",
  Msg_Loser  = "\t*****YAH BIG LOSER!!*****\n",
  Msg_Snake  = "\t*****SNAKE EYES TURKEY!!*****\n"

-----------------------------------------------------------

procedure winner()     -- CALL FOR WINNER

    text_color(10)     -- change text color
    puts (1, Msg_Winner & Msg_Winner ) -- display winner

end procedure

-----------------------------------------------------------

--BKB: we can avoid using 't' as a global variable by making this
--     a function that returns the total
function throw_dice() -- call this to throw dice
  --BKB: since d1 and d1 are local to this procedure
  --     we should declare them here
  integer d1, d2, total

  d1 = rand( 6 )    -- throw dice
  d2 = rand( 6 )
  total = d1 + d2   -- total dice
  text_color( 12 )   -- change text color
--    puts (1, "\nDice 1 is: ") -- display results
--    print (1, d1)
--    puts (1, "\tDice 2 is: ")
--    print (1, d2)
--    puts (1, "\tDice total is: ")
--    print (1, t)
  --BKB: note how we can put the output all on one line,
  --    (but your method makes it easier to change colors of values)
  printf( 1, "Dice 1 is: %d\tDice 2 is: %d\tDice total is: %d\n",
             {d1,d2,total} )
  return total  -- return the total
end function

-----------------------------------------------------------
--BKB:  since we removed the global variable 't' we need to pass
--      the total to be used in this procedure
procedure loser( integer t )    -- call for loser

  if t = 2 then       -- is it snake eyes
    text_color (14)   -- change text color
    puts (1, Msg_Snake & Msg_Snake )
  else
    text_color (5)
    puts (1, Msg_Loser & Msg_Loser )
  end if

end procedure

-----------------------------------------------------------
--BKB: we pass the value 'first_roll' (this was 'z' in the original program)
procedure other_throws( integer z ) -- keep throwing the dice
  --BKB: declare 'total' to accept value returned by 'throw_dice()'
  integer total

  while 1 do                -- multiple throw loop

    total = throw_dice()    -- next throw

    --BKB: we can check all losing conditions at once
    if total = 2 or total = 7 or total = 11 then
      loser( total )
      exit -- while loop
    elsif total = z then
      winner()
      exit --while loop
    end if

  end while      -- end multiple throw loop

--BKB: above we broke out of the while loop by using 'exit' but since
--     there are no other instructions after the while loop we could
--     also use 'return' to break out of the procedure

end procedure  -- end next throw

-----------------------------------------------------------

procedure game()-- start game
  integer first_roll

  --BKB: we made throw_dice() a function that returns the total
  first_roll = throw_dice()  -- keep to compare first throw with others

  --BKB: we can check both winning conditions at once
  if first_roll = 7 or first_roll = 11 then -- we won
    winner()
  elsif first_roll = 2 then -- handle snake-eyes in loser()
    loser( first_roll )
  else -- no 7 or 11 or snake-eyes? throw on
    other_throws( first_roll )
  end if

end procedure

-----------------------------------------------------------

function play_again()
-- returns 1 (True) if we want to play or 0 (False) if we don't
  atom play

  text_color( 7 )
  puts( 1, "Play? " )  -- ask question

  play = wait_key()  -- wait for answer

  puts( 1, play & "\n" ) -- echo user input

  --BKB: accept lower or upper case 'Y'
  if play = 'y' or play = 'Y' then
    return 1 -- True
  else
    return 0 -- False
  end if

end function

----------------------------------------------------------------------
-- start program.
----------------------------------------------------------------------
while play_again() do -- loop to check if you want to play
  game()
end while

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu