Information Technology Reference
In-Depth Information
Figure 10.9.
A game of tic-tac-toe with user input
?- play_user(o).
The first player is x and the initial state is
[-, -, -, -, -, -, -, -, -]
Player x chooses move 1 and the new state is
[x, -, -, -, -, -, -, -, -]
Enter user move (then a period): 2. % A bad move entered.
Player o chooses move 2 and the new state is
[x, o, -, -, -, -, -, -, -]
Player x chooses move 4 and the new state is
[x, o, -, x, -, -, -, -, -]
Enter user move (then a period): 7. % This is just fine.
Player o chooses move 7 and the new state is
[x, o, -, x, -, -, o, -, -]
Player x chooses move 5 and the new state is
[x, o, -, x, x, -, o, -, -]
Enter user move (then a period): 6. % This is fine, too.
Player o chooses move 6 and the new state is
[x, o, -, x, x, o, o, -, -]
Player x chooses move 9 and the new state is
[x, o, -, x, x, o, o, -, x]
-------- The winner is x
% But X still wins.
Yes
Figure 10.10.
The game of Boxes
There are eight dots aligned as in the diagram, making the
outline of three squares. At each turn, a player draws one
of the undrawn horizontal or vertical edges of a square
by connecting two adjacent dots. If this happens to be the
last undrawn edge of a square, the player owns the square,
and may optionally move again.
The first player to own two squares wins.
(There are no ties.)
Encoding the rules of this game in Prolog starts by deciding how to represent the
states and moves. The lines and squares can be named as shown in figure 10.12. Each
move will be a list of one or more lines drawn. (A list is needed here, since a player
may draw multiple lines in a single move.)
Search WWH ::




Custom Search