Console Play

The game search is located in the Prolog text tictac.p. The Prolog text console.p is then re-sponsible for an online interactive game play based on an ASCII console. In an imperative lan-guage the game play can be viewed as a while loop along the following lines, whereby we al-ternate between a user move and a computer move:

while not /* complete */ do
/* user move */
/* computer move */
end

We realize the same responsibility in the Prolog programming language and replace the while loop by a tail recursive loop. The realization is made declarative in that no side effect is used to store the board, which is also supported by the game search which represents a board as a Prolog term. We get the following sketch:

loop(S) :-
\+ complete(S),
user_move(S, S2),
computer_move(S2, S3),
loop(S3).

The Tic-Tac-Tow game is complete when either of the players has won, or when there is a draw. This might also happen after the user move and before the computer move, in which case the computer will not compute a move anymore, but show a message with the actual out-come of the game. For more details see the code listing in the appendix.

Kommentare