home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / sampler / 03 / diverse / play.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-12-14  |  2.5 KB  |  98 lines

  1. program PlayGame;
  2. {
  3.        purpose          implement tic-tac-toe on the computer
  4.        author           bruce f. webster
  5.        last update      12 Dec 87 -- 1100 mst
  6. }
  7. uses CRT,TicTac,Moves,GameIO;
  8.  
  9. procedure StartGame(var theGame : Game);
  10. {
  11.   purpose      set up a new game
  12.   pre          none
  13.   post         g,cflag,cmove have been initialized
  14.                no moves have been made
  15.                the tictactoe grid has been drawn on the screen
  16. }
  17. var
  18.   Ans          : Char;
  19. begin
  20.   ClrScr;
  21.   ReadChar(Ans,'Who moves first:  H)uman or C)omputer? ',['H','C']);
  22.   if Ans = 'C'
  23.     then CFlag := 0
  24.     else CFlag := 1;
  25.   ReadChar(Ans,'Do you wish to be X or O? ',['X','O']);
  26.   if Ans = 'X'
  27.     then CMove := O
  28.     else CMove := X;
  29.   if CFlag <> 0
  30.   then NewGame(theGame,Opposite(CMove))
  31.   else NewGame(theGAme,CMove);
  32.   DrawGrid;
  33.   DisplayGame(theGame)
  34. end; { of proc Initialize }
  35.  
  36. procedure GetMove(theGame : Game; var L : Location);
  37. {
  38.    purpose     select the next move for the game
  39.    pre         g has been initialized
  40.                0 or more moves have been made
  41.                the game is not yet over
  42.    post        l contains a value from 1..9
  43.                getloc(g,l) is BLANK
  44. }
  45. var
  46.   I            : Integer;
  47. begin
  48.   if (MovesMade(theGame) mod 2) = CFlag
  49.   then GenerateMove(theGame,L)
  50.  else begin
  51.   repeat
  52.     ReadInt(I,'Enter move',1,9);
  53.     if GetLoc(theGame,I) <> BLANK
  54.      then Write(^G)
  55.    until GetLoc(theGame,I) = BLANK;
  56.    L := I
  57.  end
  58. end; { of proc GetMove }
  59.  
  60. procedure ShowResults(var theGame : Game);
  61. {
  62.    purpose     show results of tictactoe game
  63.    pre         g has been initialized, 5 or more moves have been made
  64.                the game is over
  65.    post        the results of the game are displayed on the screen
  66. }
  67. var
  68.   M           : Move;
  69. begin
  70.   M := Winner(theGame);
  71.   GoToXY(1,1); ClrEol;
  72.   case M of
  73.     BLANK  : Write('The game was a draw');
  74.     X      : Write('The winner is X');
  75.     O      : Write('The winner is O')
  76.   end;
  77.   Write(' -- press any key to continue (Q to quit)  ')
  78. end; { of proc CleanUp }
  79.  
  80. var
  81.   theGame     : Game;
  82.   Next        : Location;
  83.   Ch          : Char;
  84.  
  85. begin  { main body of program TicTacToe }
  86.   repeat
  87.     StartGame(theGame);
  88.     repeat
  89.       GetMove(theGame,Next);
  90.       DoMove(theGame,Next);
  91.       DisplayGame(theGame);
  92.     until GameOver(theGame);
  93.     ShowResults(theGame);
  94.     Ch := ReadKey;
  95.   until (Ch in ['Q','q']);
  96.         ClrScr
  97. end. { of program TicTacToe }
  98.