fork download
  1. WITH ada.Text_IO; USE ada.Text_IO;
  2. WITH ada.Integer_Text_IO; USE ada.Integer_Text_IO;
  3. PROCEDURE nim IS
  4. SUBTYPE piletype IS character RANGE 'A'..'B'; -- used to keep track of the pile being used, A or B.
  5. chips, -- chips holds initial number of chips
  6. pile_a, pile_b : natural; -- pile_a, pile_b hold the current number of chips in each pile
  7. done : boolean := false; -- set to true when the game is over
  8. whichpile : piletype; -- holds the current pile being picked from
  9. chipstaken : positive; -- holds the number of chips taken on current turn
  10. p1turn : boolean := true; -- keeps track of whose turn it is ...
  11. -- when p1turn is true it is player 1's turn,
  12. -- when false it is player 2's turn
  13. PROCEDURE Displaypile(achips, bchips : natural);
  14. FUNCTION Initgame RETURN natural;
  15. FUNCTION Getpile(achips, bchips : natural; whoseturn : boolean) RETURN piletype;
  16. FUNCTION Getchips(achips, bchips: natural; pile: piletype; whoseturn: boolean) RETURN positive;
  17. --FUNCTION Iswinner(achips, bchips: natural) RETURN boolean;
  18. --PROCEDURE Declarewinner(whoseturn : boolean);
  19.  
  20. PROCEDURE Displaypile(Achips, Bchips : Natural) IS
  21. Acount: integer :=1;
  22. Bcount: integer :=1;
  23. BEGIN
  24. New_line;
  25. Put("The piles look like this:"); -- This procedure displays the chips in each pile by outputting some symbol like an 'o' or a '#'
  26. New_line;
  27. Put("A: ");
  28.  
  29. WHILE Acount <= achips LOOP
  30. Put("0");
  31. IF Acount REM 5=0 THEN
  32. Put(" ");
  33. END IF;
  34. ACount:= ACount + 1;
  35. END LOOP;
  36.  
  37. New_line;
  38. Put("B: ");
  39.  
  40. WHILE Bcount <= Bchips LOOP
  41. Put("0");
  42. IF Bcount REM 5=0 THEN
  43. Put(" ");
  44. End IF;
  45. Bcount:= Bcount + 1;
  46. END LOOP;
  47.  
  48. END Displaypile;
  49.  
  50.  
  51.  
  52. FUNCTION Initgame RETURN Natural IS
  53. SUBTYPE One_Twenty IS Integer RANGE 1..20;
  54. initchips: One_Twenty;
  55. BEGIN
  56. Put("How many chips do you want to start with in each pile? (max 20)");
  57. get(initchips);
  58. return(initchips);
  59. END Initgame;
  60.  
  61. FUNCTION Getpile(Achips, Bchips : Natural; Whoseturn : Boolean) RETURN Piletype IS
  62. chosenpile: piletype;
  63. BEGIN
  64. NEW_LINE;
  65. IF P1turn THEN
  66. Put("Player 1, would you like to take chips from pile A or B? ");
  67. Get(chosenpile);
  68.  
  69. IF chosenpile= 'A' AND achips < 1 THEN
  70. Put("Sorry there are not that many chips in the pile. Please try again.");
  71. ELSIF Chosenpile= 'B' AND Bchips < 1 THEN
  72. PUT("Sorry there are not that many chips in the pile. Please try again.");
  73. END IF;
  74. ELSE
  75. Put("Player 2, would you like to take chips from pile A or B?");
  76. Get(Chosenpile);
  77. IF chosenpile= 'A' AND achips < 1 THEN
  78. Put("Sorry there are not that many chips in the pile. Please try again.");
  79. ELSIF Chosenpile= 'B' AND Bchips < 1 THEN
  80. PUT("Sorry there are not that many chips in the pile. Please try again.");
  81. END IF;
  82. END IF;
  83. return (Whichpile);
  84. END Getpile;
  85.  
  86. FUNCTION Getchips(achips, bchips: natural; pile : piletype; whoseturn : boolean) RETURN positive IS
  87.  
  88. notdone : boolean := true; -- set to true when acceptable chipstaken is set
  89.  
  90. BEGIN
  91.  
  92. NEW_LINE;
  93. IF P1turn THEN
  94. WHILE (notdone) LOOP
  95. Put("Player 1, how many chips would you like from pile ");
  96. Put(Whichpile);
  97. Put("? ");
  98. Get(chipstaken);
  99. IF Whichpile = 'A' AND chipstaken > achips THEN
  100. Put("Sorry there are not that many chips in the pile.");
  101. Put(“Please try again.");
  102. ELSIF Whichpile = 'B' AND chipstaken > bchips THEN
  103. Put("Sorry there are not that many chips in the pile.");
  104. Put(“Please try again.");
  105. ELSE
  106. notdone:= false;
  107. END IF;
  108.  
  109. END LOOP;
  110. ELSE -- Not P1turn
  111. WHILE (notdone) LOOP
  112.  
  113. Put("Player 2, how many chips would you like from pile ");
  114. Put(Whichpile);
  115. Put("? ");
  116. Get(chipstaken);
  117. IF Whichpile = 'A' AND chipstaken > achips THEN
  118. Put("Sorry there are not that many chips in the pile.");
  119. Put(“Please try again.");
  120. ELSIF Whichpile = 'B' AND chipstaken > bchips THEN
  121. Put("Sorry there are not that many chips in the pile.");
  122. Put(“Please try again.");
  123. ELSE
  124. notdone:= false;
  125. END IF;
  126.  
  127. END LOOP;
  128.  
  129. END IF;
  130. return(chipstaken);
  131. END Getchips;
  132.  
  133. FUNCTION Iswinner (achips, bchips : natural) RETURN boolean IS
  134. BEGIN
  135. IF achips = 0 AND bchips = 0 THEN
  136. return(true);
  137. ELSE
  138. return (false);
  139. ENDIF;
  140. END Iswinner;
  141.  
  142. PROCEDURE Declarewinner (whoseturn : boolean) IS
  143. BEGIN
  144.  
  145. IF P1turn THEN
  146. put(“Congratulations player 1. You won the game!);
  147. ELSE
  148. put(“Congratulations player 2. You won the game!);
  149. END IF;
  150.  
  151. END Declarewinner;
  152.  
  153.  
  154. BEGIN -- main program of nim
  155. chips := initgame;
  156. put(item=> "the number of chips you chose is ");
  157. put(chips);
  158. pile_a := chips;
  159. pile_b := chips;
  160. WHILE (NOT done) LOOP
  161. Displaypile(achips => pile_a, bchips => pile_b);
  162. whichpile := Getpile(achips => pile_a, bchips => pile_b, whoseturn => p1turn);
  163. chipstaken := Getchips(achips => pile_a, bchips => pile_b, pile => whichpile, whoseturn => p1turn);
  164. IF whichpile = 'A' THEN
  165. pile_a := pile_a - chipstaken;
  166. ELSE
  167. pile_b := pile_b - chipstaken;
  168. END IF;
  169. IF Iswinner(achips => pile_a, bchips => pile_b) THEN
  170. done := true;
  171. ELSE
  172. p1turn := NOT p1turn;
  173. END IF;
  174. END LOOP;
  175. Declarewinner(p1turn);
  176. END nim;
  177.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
gnatgcc -c -pipe -O2 prog.adb
prog.adb:60:01: declaration expected
prog.adb:60:02: illegal character
prog.adb:85:01: declaration expected
prog.adb:85:02: illegal character
prog.adb:101:06: illegal character
prog.adb:104:06: illegal character
prog.adb:119:06: illegal character
prog.adb:122:06: illegal character
prog.adb:132:01: declaration expected
prog.adb:132:02: illegal character
prog.adb:140:01: missing "END IF;" for "IF" at line 135
prog.adb:141:01: declaration expected
prog.adb:141:02: illegal character
prog.adb:146:06: illegal character
prog.adb:148:06: illegal character
gnatmake: "prog.adb" compilation error
stdout
Standard output is empty