The Ada Program: craps.adb

  1 -- craps.adb:  Play one game of craps, gambling game using two dice
  2 with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Numerics.Discrete_Random;
  3 use  Ada;
  4 
  5 procedure Craps is
  6 
  7    subtype Spots is Integer Range 1 .. 6;
  8    subtype Points is Integer Range 2 .. 12;
  9 
 10    package Random_Package is new Numerics.Discrete_Random (Spots);
 11    Seed: Random_Package.Generator;
 12 
 13    function Roll_Two (Verbose: Boolean := False) return Points is
 14       First, Second: Spots;
 15    begin
 16       First  := Random_Package.Random (Seed);
 17       Second := Random_Package.Random (Seed);
 18       if (Verbose) then
 19          Text_IO.Put ("Rolling ... ");
 20          Integer_Text_IO.Put (First, Width=>3);
 21          Integer_Text_IO.Put (Second, Width=>3);
 22          Text_IO.New_Line;
 23       end if;
 24       return (First + Second);
 25    end Roll_Two;
 26 
 27    Initial, Total: Points;
 28 
 29 begin
 30    Random_Package.Reset (Seed); -- set seed different every time
 31    Initial := Roll_Two (True);  -- "come out" roll
 32    case Initial is
 33       when 7|11   => Text_IO.Put_Line ("Natural win!");
 34       when 2|3|12 => Text_IO.Put_Line ("Loose!");
 35       when others =>
 36         -- Point established
 37         loop
 38            Total := Roll_Two (True);
 39            exit when Total = Initial or else Total = 7;
 40         end loop;
 41         if (Total = 7) then
 42            Text_IO.Put_Line ("Player craps out.  Loose!");
 43         else
 44            Text_IO.Put_Line ("Player makes the point.  Win!");
 45         end if;
 46    end case;
 47 end Craps;