Practical 05
Practical 05
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.
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.
circle(1).
circle(6).
circle(7).
circle(9).
cross(2). cross(3).
cross(5). cross(8).
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
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?