home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / dice.ex < prev    next >
Text File  |  1994-02-11  |  1KB  |  69 lines

  1.         -----------------------------------
  2.         -- roll dice and see a histogram --
  3.         -----------------------------------
  4.  
  5. include get.e
  6.  
  7. constant KEYBOARD = 0,
  8.      SCREEN = 1
  9.  
  10. function get_number()
  11. -- input a number
  12.     sequence input
  13.  
  14.     input = get(KEYBOARD) 
  15.     if input[1] = GET_SUCCESS then
  16.     return input[2]
  17.     else
  18.     abort(1)
  19.     end if
  20. end function
  21.  
  22. function sum(sequence x)
  23. -- add up a sequence of numbers
  24.     integer total
  25.  
  26.     total = 0
  27.     for i = 1 to length(x) do
  28.     total = total + x[i]
  29.     end for
  30.     return total
  31. end function
  32.  
  33. procedure dice()
  34. -- main procedure
  35.     integer ndice, nrolls, score, lines
  36.     sequence count, roll
  37.  
  38.     puts(SCREEN, "number of dice? ")  
  39.     ndice = get_number()
  40.  
  41.     puts(SCREEN, "\nnumber of rolls? ")
  42.     nrolls = get_number()
  43.  
  44.     count = repeat(0, 6 * ndice)
  45.  
  46.     -- roll the dice
  47.     for r = 1 to nrolls do
  48.     roll = rand(repeat(6, ndice))
  49.     score = sum(roll) 
  50.     count[score] = count[score] + 1 
  51.     end for
  52.  
  53.     -- display the results
  54.     printf(1, "\n\n\t%d dice, %d rolls\n", {ndice, nrolls})
  55.     lines = 1
  56.     for i = ndice to 6 * ndice do
  57.     printf(SCREEN, "%2d: ", i)  
  58.     puts(SCREEN, repeat('*', count[i]) & '\n')
  59.     if remainder(lines, 24) = 0 then
  60.         while get_key() = -1 do
  61.         end while
  62.     end if
  63.     lines = lines + 1
  64.     end for
  65. end procedure
  66.  
  67. dice()
  68.  
  69.