Information Technology Reference
In-Depth Information
Figure 10.2.
A Prolog program for Race to 21
raceto21.pl
% This is the Prolog definition of the game Race to 21.
player(max). player(min). % The two players.
initial_state(21,max). % Player Max goes first.
game_over(0,max,min). % If it is Max's turn to play,
game_over(0,min,max). % then Min wins, and vice versa.
legal_move(OldState,_,Move,NewState) :-
small_number(Move),
% A move is a small number.
OldState >= Move,
NewState is OldState - Move.
small_number(1).
% A small number is either
small_number(2).
% the number 1 or 2.
To characterize this game using the four predicates, the programmer has to decide
how to represent the states and the moves , just as was done for planning.
A state in this game is a number between 0 and 21, representing how many chips
are left on the table:
The initial state is 21.
The final state is 0. The player whose turn it is to move loses. (In Race to 21, there
is never a tie.)
A move in Race to 21 is a number, either 1 or 2. A move is legal provided there are at
least that many chips remaining on the table. (A player cannot remove two chips in
a state where there is only one chip remaining.) The new state after making a move
is the difference between the current state and the move. As shown in figure 10.2, this
can be encoded directly in a Prolog program, for two players called Max and Min,
where Max plays first. (See section 10.3.1 for the reason the players have the names
Max and Min.) This defines the game and how it is played according to the rules.
The next section turns to the issue of playing games well.
10.2 Finding the best move
To see how to play a game well, no matter what game it is, observe the following:
 
Search WWH ::




Custom Search