Information Technology Reference
In-Depth Information
Among other things, this shows that player X can choose to move to position 4 and
still be guaranteed a win.
Let us step back one move from this state to see what player O should have done
differently when X had a single mark on the board:
?- win_move([x,-,-,-,-,-,-,-,-],o,M).
No
?- tie_move([x,-,-,-,-,-,-,-,-],o,M).
M=5 ;
No
There is nothing O can do to guarantee a win after X chooses position 1. However,
there is a move (and only one move) that will be sufficient to ensure that she does not
lose, and that is the center square, position 5.
What X should do in the initial state:
?- win_move([-,-,-,-,-,-,-,-,-],x,M).
No
?- tie_move([-,-,-,-,-,-,-,-,-],x,M).
M=1 ;
M=2 ;
M=3 ;
M=4 ; M=5 ;
M=6 ;
M=7 ;
M=8 ;
M=9
As we might already know from actually playing tic-tac-toe, there is nothing that the
first player can do to guarantee a win at the outset. Not so well known is the fact that
any first move is as good as any other in terms of making sure that the player does not
lose. So even with a game as simple as tic-tac-toe, there are perhaps some surprises
that can be revealed by the game-playing program.
10.2.5 Playing an entire game
Although this adds nothing to the thinking behind game playing, it is fun to play an
entire game from the initial state to a final state rather than just making a single next
best move. A Prolog program that does this is shown in figure 10.7.
This program will work with the general game player and any game. The main
predicate here is play_user , and its argument is used to indicate which of the two
players in the game will be played by the user (that is, will not be played by the
computer). So for tic-tac-toe, the query play_user(x) would play a game where the
user is player X (plays first), and play_user(nobody) would play a game where the
computer plays both sides.
 
Search WWH ::




Custom Search