Department of: Subject of:
Information Technology Artificial Intelligence
Quaid-e-Awam University of Year 4TH Semester 8TH
Engineering, Science &
Technology Batch 19IT Duration 03
Hours
Nawabshah
PRACTICAL 05
TO DESIGN TIC-TAC-TOE GAME IN PROLOG
----------------------------------------------------------
OBJECTIVES
• Tic-tac-toe game
• Prolog program
REQUIREMENTS
• PC with windows
• SWI Prolog
5.1 Tic-Tac-Toe Game
Tic-tac-toe, also called noughts and crosses (in the British Commonwealth
countries) and X's and O's in the Republic of Ireland, is a pencil-and-paper
game for two players, X and O, who take turns marking the spaces in a 3×3
grid. The X player usually goes first. The player who succeeds in placing marks
in a horizontal, vertical, or diagonal row wins the game.
The following example game is won by the first player, X:
Players soon discover that best play from both parties leads to a draw (often
referred to as cat or cat's game). Hence, tic-tac-toe is most often played by
young children.
5.2 Players of the game
There are only two players in the game. One will choose the called circles. In
PROLOG this could be represented as fact:
player(string,string)
Here the predicate player contains two string arguments, first tells the name
of the player and the second tell whether the player chooses crosses or circles.
For example if Ali chooses crosses and Kashif chooses circles then the facts
can be written as:
player(“ali”,”crosses”). player(“kashif”,”circles”).
5.3 State of boxes
The tic-tac-toe contains 9 square boxes numbered as shown in below figure.
Each of the boxes will either contain circle (O), cross(X) or can be empty. We
will maintain the state of tic-tac-toe by creating two predicates:
circle(integer) cross(integer)
Both predicates contain one argument that is the box number in which cross
or circle is placed. For the state of tic-tac-toe shown in second figure the facts
can be written as:
circle(1).
circle(6).
circle(7).
circle(9).
cross(2). cross(3).
cross(5). cross(8).
5.4 Condition for crosses to win
The crosses will win if any one of the crosses positions is present in the
current state of the game:
So we can write a rule crossesWin() as follows:
crossesWin():-cross(1),cross(2),cross(3);
cross(4),cross(5),cross(6); cross(7),cross(8),cross(9);
cross(1),cross(4),cross(7); cross(2),cross(5),cross(8);
cross(3),cross(6),cross(9);
cross(1),cross(5),cross(9); cross(3),cross(5),cross(7).
5.5 Condition for circles to win
The circles will win if any one of the circles positions is present in the current
state of the game:
So we can write a rule circlesWin() as follows:
circlesWin():-circle(1),circle(2),circle(3);
circle(4),circle(5),circle(6); circle(7),circle(8),circle(9);
circle(1),circle(4),circle(7);
circle(2),circle(5),circle(8); circle(3),circle(6),circle(9);
circle(1),circle(5),circle(9);
circle(3),circle(5),circle(7).
5.6 Conditions for players to win
In order to determine which player wins we must know two things: First,
whether player has chosen crosses or circles. Second, whether cross wins or
circle wins.
If circles win then the player who has chosen circles will win.
If crosses win then the player who has chosen crosses will win.
This rule can be written in prolog as:
wins(Player):-crossesWin(),player(Player,”crosses”).
wins(Player):-circlesWin(),player(Player,”circles”).
5.7 Prolog Program
PREDICATES
player(string,string)
circle(integer)
cross(integer)
circlesWin()
crossesWin()
wins(string)
CLAUSES
player(“ali”,”circles”).
player(“kashif”,”crosses”).
cross(1).
cross(3).
cross(4).
cross(7).
circle(2).
circle(5).
circle(6).
circle(9).
crossesWin():-cross(1),cross(2),cross(3);
cross(4),cross(5),cross(6);
cross(7),cross(8),cross(9);
cross(1),cross(4),cross(7);
cross(2),cross(5),cross(8);
cross(3),cross(6),cross(9);
cross(1),cross(5),cross(9);
cross(3),cross(5),cross(7).
circlesWin():-circle(1),circle(2),circle(3);
circle(4),circle(5),circle(6);
circle(7),circle(8),circle(9);
circle(1),circle(4),circle(7);
circle(2),circle(5),circle(8);
circle(3),circle(6),circle(9);
circle(1),circle(5),circle(9);
circle(3),circle(5),circle(7).
wins(Player):-crossesWin(),player(Player,”crosses”).
wins(Player):-circlesWin(),player(Player,”circles”).
GOAL
wins(Who).
EXERCISE
1. Suppose there are two players: Player 1= Akhter, he chooses circles
and Player 2= Shahid, he chooses crosses. The current state of the
game is shown below. Write a PROLOG program to find who wins?
2. Suppose there are two players: Player 1= Ali, he chooses circles and
Player 2= Kashif, he chooses crosses. Now we extend our game to 4 X
4 square boxes. The current state of the game is shown below. Write
a PROLOG program to find who wins?