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

  1. unit GameIO;
  2. interface
  3.  
  4. uses CRT,TicTac;
  5.  
  6. type                           { define types for parameters }
  7.   CharSet      = set of Char;
  8.   MsgStr       = string[80];
  9.  
  10. procedure DisplayGame(theGame : Game);
  11. procedure DrawGrid;
  12. procedure ReadChar(var Ch : Char; Prompt : MsgStr; OKSet : CharSet);
  13. procedure ReadInt(var Val  : Integer;
  14.                   Prompt   : MsgStr;
  15.                   Low,High : Integer);
  16.  
  17. implementation
  18.  
  19. const
  20.   BoardX               = 10;      { positioning for tictactoe grid }
  21.   BoardY               = 10;
  22.   Bar                  = #186;    { special characters used for grid }
  23.   Line                 = #205;
  24.   Cross                = #206;
  25.  
  26. procedure DrawGrid;
  27. {
  28.    purpose     draws full-sized tictactoe grid,
  29.                with smaller numbered one beside it
  30.    pre         screen has been cleared
  31.    post        two grids drawn on screen
  32. }
  33.  
  34.   procedure DrawHorz(X,Y : Integer);
  35.   {
  36.      purpose     draws horizontal bar for tictactoe grid
  37.      pre         x <= 63, y <= 23
  38.      post        bar is written to screen
  39.   }
  40.   begin
  41.     GoToXY(X,Y);
  42.     Write(Line,Line,Line,Line,Line,Cross);
  43.     Write(Line,Line,Line,Line,Line,Cross);
  44.     Write(Line,Line,Line,Line,Line)
  45.   end; { of locproc DrawHorz }
  46.  
  47.   procedure DrawVert(X,Y : Integer);
  48.   {
  49.      purpose    draws vertical bars for tictactoe grid
  50.      pre        x <= 78, y <= 16
  51.      post       vertical bar appears on screen (with gaps for crosses)
  52.   }
  53.   var
  54.     J,I       : Integer;
  55.   begin
  56.     for J := 1 to 3 do begin
  57.       for I := 0 to 2 do begin
  58.         GoToXY(X,Y+I); Write(Bar)
  59.       end;
  60.       Y := Y + 4
  61.     end
  62.   end; { of locproc DrawVert }
  63.  
  64.   procedure DrawMoves(X,Y : Integer);
  65.   {
  66.      purpose     draws 3x3 grid with numbered positions
  67.      pre         x <= 77, y <= 21
  68.      post        3x3 grid drawn on screen
  69.   }
  70.  begin
  71.    GoToXY(X,Y);   Write('1',Bar,'2',Bar,'3');
  72.    GoToXY(X,Y+1); Write(Line,Cross,Line,Cross,Line);
  73.    GoToXY(X,Y+2); Write('4',Bar,'5',Bar,'6');
  74.    GoToXY(X,Y+3); Write(Line,Cross,Line,Cross,Line);
  75.    GoToXY(X,Y+4); Write('7',Bar,'8',Bar,'9')
  76.  end; { of locproc DrawMoves }
  77.  
  78. begin
  79.   DrawHorz(BoardX,BoardY);
  80.   DrawHorz(BoardX,BoardY+4);
  81.   DrawVert(BoardX+5,BoardY-3);
  82.   DrawVert(BoardX+11,BoardY-3);
  83.   DrawMoves(BoardX+20,BoardY)
  84. end; { of proc DrawGrid }
  85.  
  86. procedure DisplayGame(theGame : Game);
  87. {
  88.    purpose     draws status of tictactoe game on screen
  89.    pre         grid has already been drawn on screen
  90.    post        contents of each grid location are displayed
  91. }
  92. var
  93.   I,Col,Row   : Integer;
  94.   M           : Move;
  95. begin
  96.   for I := 1 to GLim do begin
  97.     M := GetLoc(theGame,I);
  98.     Col := BoardX + 2 + 6 * ((I-1) mod 3);
  99.     Row := BoardY - 2 + 4 * ((I-1) div 3);
  100.     GoToXY(Col,Row);
  101.     case M of
  102.       BLANK  : Write(' ');
  103.       X      : Write('X');
  104.       O      : Write('O')
  105.     end
  106.   end
  107. end; { of proc DisplayGame }
  108.  
  109. procedure ReadChar(var Ch : Char; Prompt : MsgStr; OKSet : CharSet);
  110. {
  111.    purpose     prompt for and get one character of a given set
  112.    pre         okset is non-empty and contains valid uppercase
  113.                characters (including digits, punctuation, etc.)
  114.    post        readchar() returns some character contained in okset
  115. }
  116. begin
  117.   GoToXY(1,1); ClrEol;
  118.   Write(Prompt);
  119.   repeat
  120.     Ch := UpCase(ReadKey)
  121.   until Ch in OKSet;
  122.   Write(Ch)
  123. end; { of proc ReadChar }
  124.  
  125. procedure ReadInt(var Val  : Integer;
  126.                   Prompt   : MsgStr;
  127.                   Low,High : Integer);
  128. {
  129.    purpose     prompt for and get an integer value in a given range
  130.    pre         low <= high
  131.    post        readint() returns some value in the range low..high
  132. }
  133. begin
  134. {$I-}
  135.   repeat
  136.     GoToXY(1,1); ClrEol;
  137.     Write(Prompt,' (',Low,',',High,'): ');
  138.     Readln(Val)
  139.   until (IOResult = 0) and (Val >= Low) and (Val <= High)
  140. {$I+}
  141. end; { of proc ReadInt }
  142.  
  143. end. { of unit GameIO }
  144.