Book
Book
Book
Introduction
1
Chapter 2
2.1 Objectives
1. To play Tic-Tac-Toe game
2. To create the computer player
3. To implement the First Order Logic
2
o Syntax
o Semantics
Syntax of FOL: Basic elements
Constant symbols KingJohn, 2, UniversityofMaryland, . . .
Predicate symbols Brother, >, . . .
Function symbols Sqrt, LeftLegOf , . . .
Variable symbols x, y, a, b, . . .
Connectives ∧∨¬⇒⇔
Equality =
Quantifiers ∀∃
Punctuation (),
3
Chapter 3
3.1 Tic Tac Toe
Xs and Os is a paper-and-pencil game for two players, X and O, who take turns
marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in
a horizontal, vertical, or diagonal row is the winner.
The following example game is won by the player, O:
4
3.2 Strategy
A player can play a perfect game of tic-tac-toe (to win or at least, draw) if each time it
is their turn to play, they choose the first available move from the following list, as used in
tic-tac-toe program.
● Win: If the player has two in a row, they can place a third to get three in a row.
● Block: If the opponent has two in a row, the player must play the third themselves to
block the opponent.
● Fork: Create an opportunity where the player has two ways to win.
● Blocking an opponent's fork: If there is only one possible fork for the opponent, the
player should block it. Otherwise, the player should block all forks in any way that
simultaneously allows them to create two in a row. Otherwise, the player should
create a two in a row to force the opponent into defending, as long as it doesn't result
in them creating a fork. For example, if "X" has two opposite corners and "O" has the
center, "O" must not play a corner in order to win. (Playing a corner in this scenario
creates a fork for "X" to win.)
● Center: A player marks the center. (If it is the first move of the game, playing on a
corner gives the second player more opportunities to make a mistake and may
therefore be the better choice; however, it makes no difference between perfect
players.)
● Opposite corner: If the opponent is in the corner, the player plays the opposite corner.
● Empty corner: The player plays in a corner square.
● Empty side: The player plays in a middle square on any of the 4 sides.
5
3.3 Winning Condition
A player wins if he completes 3 marks in a row or a column or a diagonal. Same as a classical
tic tac toe game. Predicates that define the winning conditions:
3.5 Predicate orespond generates the computer’s (playing o) response from the current
Board
orespond(Board,Newboard) :-move(Board, o, Newboard),win(Newboard, o),!.
orespond(Board,Newboard) :-move(Board,o,
Newboard),not(x_can_win_in_one(Newboard)).
orespond(Board,Newboard) :-move(Board, o, Newboard).
orespond(Board,Newboard):-not(member(b,Board)) , ! , write(’Cats game!’), nl,
Newboard=board.
xmove([b,B,C,D,E,F,G,H,I], 1, [x,B,C,D,E,F,G,H,I]).
xmove([A,b,C,D,E,F,G,H,I], 2, [A,x,C,D,E,F,G,H,I]).
6
xmove([A,B,b,D,E,F,G,H,I], 3, [A,B,x,D,E,F,G,H,I]).
xmove([A,B,C,b,E,F,G,H,I], 4, [A,B,C,x,E,F,G,H,I]).
xmove([A,B,C,D,b,F,G,H,I], 5, [A,B,C,D,x,F,G,H,I]).
xmove([A,B,C,D,E,b,G,H,I], 6, [A,B,C,D,E,x,G,H,I]).
xmove([A,B,C,D,E,F,b,H,I], 7, [A,B,C,D,E,F,x,H,I]).
xmove([A,B,C,D,E,F,G,b,I], 8, [A,B,C,D,E,F,G,x,I]).
xmove([A,B,C,D,E,F,G,H,b], 9, [A,B,C,D,E,F,G,H,x]).
xmove(Board, _N, Board) :- write(’Illegal move.’), nl
7
Chapter 4
Implementation
8
Figure 4 : When user (X) move 2 , computer (O) move 1 and when X move 5 , computer
move 8
9
Chapter 5
Conclusion
As the conclusion, we understand about the tic tac toe game. First, we knew the rule
of game. Second we implemented game by using First Order Logic. Now, Game AI is very
development such as Dota 2 AI, called OpenAI Five, learned by playing over 10,000 years of
games against itself. Finally, we created the small computer player. But it do not optimize
move by using minimax, alphabeta….
10
Chapter 6
Appendix
11
orespond(Board,Newboard) :-
move(Board, o, Newboard),
win(Newboard, o),
!.
orespond(Board,Newboard) :-
move(Board, o, Newboard),
not(x_can_win_in_one(Newboard)).
orespond(Board,Newboard) :-
move(Board, o, Newboard).
orespond(Board,Newboard) :-
not(member(b,Board)),!,write('Cats game!'), nl,Newboard = Board.
% The following translates from an integer description
% of x’s move to a board transformation.
xmove([b,B,C,D,E,F,G,H,I], 1, [x,B,C,D,E,F,G,H,I]).
xmove([A,b,C,D,E,F,G,H,I], 2, [A,x,C,D,E,F,G,H,I]).
xmove([A,B,b,D,E,F,G,H,I], 3, [A,B,x,D,E,F,G,H,I]).
xmove([A,B,C,b,E,F,G,H,I], 4, [A,B,C,x,E,F,G,H,I]).
xmove([A,B,C,D,b,F,G,H,I], 5, [A,B,C,D,x,F,G,H,I]).
xmove([A,B,C,D,E,b,G,H,I], 6, [A,B,C,D,E,x,G,H,I]).
xmove([A,B,C,D,E,F,b,H,I], 7, [A,B,C,D,E,F,x,H,I]).
xmove([A,B,C,D,E,F,G,b,I], 8, [A,B,C,D,E,F,G,x,I]).
xmove([A,B,C,D,E,F,G,H,b], 9, [A,B,C,D,E,F,G,H,x]).
xmove(Board, _N, Board) :- write('Illegal move.'), nl.
% The 0-place predicate playo starts a game with the user.
playo :- explain, playfrom([b,b,b,b,b,b,b,b,b]).
explain :-
write('Yoexplaiu play X by entering integer positions followed by a
period.'),
nl,
display([1,2,3,4,5,6,7,8,9]).
playfrom(Board) :- win(Board, x), write('You win!').
playfrom(Board) :- win(Board, o), write('AI win!').
playfrom(Board) :- read(N),
xmove(Board, N, Newboard),
display(Newboard),
orespond(Newboard, Newnewboard),
display(Newnewboard),
playfrom(Newnewboard).
12