Book

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Chapter 1

Introduction

Tic-tac-toe (American English), noughts and crosses (British English), or 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. A computer player ( O ) is made for the game Tic-Tac-Toe by
using First Order Logic. The program will allow a human user to play against the computer.

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.2 Background Theory


First-Order Logic in Artificial intelligence
In the topic of Propositional logic, we have seen that how to represent statements
using propositional logic. But unfortunately, in propositional logic, we can only represent the
facts, which are either true or false. PL is not sufficient to represent the complex sentences or
natural language statements. The propositional logic has very limited expressive power.
Consider the following sentence, which we cannot represent using PL logic.
 "Some humans are intelligent", or
 "Sachin likes cricket."
To represent the above statements, PL logic is not sufficient, so we required some more
powerful logic, such as first-order logic.
First-Order logic:
 First-order logic is another way of knowledge representation in artificial intelligence.
It is an extension to propositional logic.
 FOL is sufficiently expressive to represent the natural language statements in a
concise way.
 First-order logic is also known as Predicate logic or First-order predicate logic. First-
order logic is a powerful language that develops information about the objects in a
more easy way and can also express the relationship between those objects.
 First-order logic (like natural language) does not only assume that the world contains
facts like propositional logic but also assumes the following things in the world:
o Objects: A, B, people, numbers, colors, wars, theories, squares, pits,
wumpus, ......
o Relations: It can be unary relation such as: red, round, is adjacent, or n-any
relation such as: the sister of, brother of, has color, comes between
o Function: Father of, best friend, third inning of, end of, ......
 As a natural language, first-order logic also has two main parts:

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:

Figure 1 : Tic Tac Toe 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:

win(Board, Player) :- rowwin(Board, Player).


win(Board, Player) :- colwin(Board, Player).
win(Board, Player) :- diagwin(Board, Player).
rowwin(Board, Player) :- Board = [Player,Player,Player,_,_,_,_,_,_].
rowwin(Board, Player) :- Board = [_,_,_,Player,Player,Player,_,_,_].
rowwin(Board, Player) :- Board = [_,_,_,_,_,_,Player,Player,Player].
colwin(Board, Player) :- Board = [Player,_,_,Player,_,_,Player,_,_].
colwin(Board, Player) :- Board = [_,Player,_,_,Player,_,_,Player,_].
colwin(Board, Player) :- Board = [_,_,Player,_,_,Player,_,_,Player].
diagwin(Board, Player) :- Board = [Player,_,_,_,Player,_,_,_,Player].
diagwin(Board, Player) :- Board = [_,_,Player,_,Player,_,Player,_,_].

3.4 Predicates to support playing a game with the user


x_can_win_in_one(Board) :- move(Board, x, Newboard), win(Newboard, x).

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.

3.6 Define Move


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]).

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

The following translates the computer O’s move to a board transformation.

move([b,B,C,D,E,F,G,H,I], Player, [Player,B,C,D,E,F,G,H,I]).


move([A,b,C,D,E,F,G,H,I], Player, [A,Player,C,D,E,F,G,H,I]).
move([A,B,b,D,E,F,G,H,I], Player, [A,B,Player,D,E,F,G,H,I]).
move([A,B,C,b,E,F,G,H,I], Player, [A,B,C,Player,E,F,G,H,I]).
move([A,B,C,D,b,F,G,H,I], Player, [A,B,C,D,Player,F,G,H,I]).
move([A,B,C,D,E,b,G,H,I], Player, [A,B,C,D,E,Player,G,H,I]).
move([A,B,C,D,E,F,b,H,I], Player, [A,B,C,D,E,F,Player,H,I]).
move([A,B,C,D,E,F,G,b,I], Player, [A,B,C,D,E,F,G,Player,I]).
move([A,B,C,D,E,F,G,H,b], Player, [A,B,C,D,E,F,G,H,Player]).

7
Chapter 4
Implementation

Figure 2 : If user want to play game, user must introduce to ‘playo’

Figure 3: Start playing game

8
Figure 4 : When user (X) move 2 , computer (O) move 1 and when X move 5 , computer
move 8

Figrue5 :Compter (O) player ,AI win

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

% Predicates that define the winning conditions:


win(Board, Player) :- rowwin(Board, Player).
win(Board, Player) :- colwin(Board, Player).
win(Board, Player) :- diagwin(Board, Player).
rowwin(Board, Player) :- Board = [Player,Player,Player,_,_,_,_,_,_].
rowwin(Board, Player) :- Board = [_,_,_,Player,Player,Player,_,_,_].
rowwin(Board, Player) :- Board = [_,_,_,_,_,_,Player,Player,Player].
colwin(Board, Player) :- Board = [Player,_,_,Player,_,_,Player,_,_].
colwin(Board, Player) :- Board = [_,Player,_,_,Player,_,_,Player,_].
colwin(Board, Player) :- Board = [_,_,Player,_,_,Player,_,_,Player].
diagwin(Board, Player) :- Board = [Player,_,_,_,Player,_,_,_,Player].
diagwin(Board, Player) :- Board = [_,_,Player,_,Player,_,Player,_,_].
% Helping predicate for alternating play in a "self" game:
other(x,o).
other(o,x).
game(Board, Player) :- win(Board, Player), !, write([player, Player,
wins]).
game(Board, Player) :-
other(Player,Otherplayer),
move(Board,Player,Newboard),
!,
display(Newboard),
game(Newboard,Otherplayer).
move([b,B,C,D,E,F,G,H,I], Player, [Player,B,C,D,E,F,G,H,I]).
move([A,b,C,D,E,F,G,H,I], Player, [A,Player,C,D,E,F,G,H,I]).
move([A,B,b,D,E,F,G,H,I], Player, [A,B,Player,D,E,F,G,H,I]).
move([A,B,C,b,E,F,G,H,I], Player, [A,B,C,Player,E,F,G,H,I]).
move([A,B,C,D,b,F,G,H,I], Player, [A,B,C,D,Player,F,G,H,I]).
move([A,B,C,D,E,b,G,H,I], Player, [A,B,C,D,E,Player,G,H,I]).
move([A,B,C,D,E,F,b,H,I], Player, [A,B,C,D,E,F,Player,H,I]).
move([A,B,C,D,E,F,G,b,I], Player, [A,B,C,D,E,F,G,Player,I]).
move([A,B,C,D,E,F,G,H,b], Player, [A,B,C,D,E,F,G,H,Player]).
display([A,B,C,D,E,F,G,H,I]) :- write([A,B,C]),nl,write([D,E,F]),nl,
write([G,H,I]),nl,nl.
selfgame :- game([b,b,b,b,b,b,b,b,b],x).
% Predicates to support playing a game with the user:
x_can_win_in_one(Board) :- move(Board, x, Newboard), win(Newboard, x).
% The predicate orespond generates the computer’s (playing o)reponse
% from the current Board.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy