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

  1. unit Moves;
  2. {
  3.    MOVES.PAS   unit for making computer's moves for tictactoe
  4.          author      bruce f. webster
  5.          last update 12 dec 87
  6. }
  7. interface
  8.  
  9. uses TicTac;
  10.  
  11. var
  12.   CFlag        : Integer; { 0 if computer moves first, 1 otherwise }
  13.   CMove        : Move;    { contains computer's market (X,O)       }
  14.  
  15. procedure GenerateMove(G : Game; var L : Location);
  16.  
  17. implementation
  18.  
  19. function WinFound(G : Game; var L : Location) : Boolean;
  20. {
  21.    purpose     checks for winning move in game
  22.    pre         g has been initialized, 0 or more moves have been made,
  23.                the game is not yet over
  24.    post        if the next move can win the game
  25.                then l is set to that move and winfound() returns TRUE
  26.                else l is unchanged and winfound() returns FALSE
  27. }
  28. var
  29.   Temp         : Game;
  30.   I            : Integer;
  31. begin
  32.   I := 1;
  33.   WinFound := FALSE;
  34.   repeat
  35.     if GetLoc(G,I) = BLANK then begin
  36.       Temp := G;
  37.       DoMove(Temp,I);
  38.       if GameOver(Temp) and (Winner(Temp) <> BLANK) then begin
  39.         L := I;
  40.         WinFound := TRUE;
  41.         Exit;
  42.       end;
  43.     end;
  44.     I := I + 1;
  45.   until I > GLim;
  46. end; { of func WinFound }
  47.  
  48. function BlockFound(G: Game; var L : Location) : Boolean;
  49. {
  50.    purpose    checks for blocking move in game
  51.    pre        g has been initialized, 0 or more moves have been made,
  52.               the game is not yet over
  53.    post       if the next move can prevent the following move from
  54.               winning the game
  55.               then l is set to that move and blockfound() returns TRUE
  56.               else l is unchanged and blockfound() returns FALSE
  57. }
  58. var
  59.   Temp         : Game;
  60.   I            : Integer;
  61.   J            : Location;
  62. begin
  63.   I := 1;
  64.   BlockFound := FALSE;
  65.   repeat
  66.     if GetLoc(G,I) = BLANK then begin
  67.       Temp := G;
  68.       DoMove(Temp,I);
  69.       if not WinFound(Temp,J) then begin
  70.         L := I;
  71.         BlockFound := TRUE;
  72.         Exit
  73.       end
  74.     end;
  75.     I := I + 1
  76.   until I > GLIM;
  77. end; { of func BlockFound }
  78.  
  79. procedure GenerateMove(G : Game; var L : Location);
  80. {
  81.    purpose     generates next move for computer
  82.    pre         g has been initialized, 0 or more moves have been made,
  83.                the game is not yet over
  84.    post        *l contains a value in the range 1..9,
  85.                getloc(g,*l) returns BLANK
  86.    strategy    goes first for move to the center (*l == 5)
  87.                then focuses on corners, then moves randomly
  88.                always looks for winning move
  89.                after 3 or more moves, also looks for blocking moves
  90.    analysis    not perfect, but simple and effective; won't always
  91.                win when it could, but always plays to at least a draw
  92. }
  93. var
  94.   NMoves       : Integer;
  95. begin
  96.   L := 5;
  97.   NMoves := MovesMade(G);
  98.   if NMoves <= 2 then begin
  99.     if GetLoc(G,L) = BLANK
  100.       then Exit
  101.   end;
  102.   if WinFound(G,L) then Exit;
  103.   if (NMoves > 2) and BlockFound(G,L) then Exit;
  104.   repeat
  105.     if NMoves <= 4
  106.       then L := 1 + 2 * Random(5)
  107.       else L := 1 + Random(GLim);
  108.   until GetLoc(G,L) = BLANK
  109. end; { of proc GenerateMoves }
  110.  
  111. end. { of unit Moves }
  112.