0% found this document useful (0 votes)
69 views13 pages

Computer Science Tic Tac Toe Project

The document describes a tic-tac-toe game project completed by three students. It provides the rules of tic-tac-toe, strategies for the game, and examples of draws and wins. It also includes the Python code used to recreate the game on a computer.

Uploaded by

kmafsar2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views13 pages

Computer Science Tic Tac Toe Project

The document describes a tic-tac-toe game project completed by three students. It provides the rules of tic-tac-toe, strategies for the game, and examples of draws and wins. It also includes the Python code used to recreate the game on a computer.

Uploaded by

kmafsar2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

computer project

TIC TAC TOE

DONE BY ;

S.Mohideen

Nitish Kumar

Muthu ram
ABOUT TIC TAC TOE ;

Games played on three-in-a-row boards


can be traced back to ancient Egypt,[5]
where such game boards have been
found on roo ng tiles dating from around
1300 BC.[6]
An early variation of tic-tac-toe was
played in the Roman Empire, around the
rst century BC. It was called terni lapilli
(three pebbles at a time) and instead of
having any number of pieces, each
player had only three; thus, they had to
move them around to empty spaces to
keep playing.[7] The game's grid
markings have been found chalked all
over Rome. Another closely related
ancient game is three men's morris
which is also played on a simple grid
and requires three pieces in a row to
nish,[8] and Picaria, a game of the
Puebloans.
fi
fi
fi
Rules and regulations

Consider a board with the nine positions numbered


as follows:

1 2 3
4 5 6
7 8 9
When X plays 1 as their opening move, then O
should take 5. Then X takes 9 (in this situation, O
should not take 3 or 7, O should take 2, 4, 6 or 8):
• X1 → O5 → X9 → O2 → X8 → O7 → X3 →
O6 → X4, this game will be a draw.
or 6 (in this situation, O should not take 4 or 7, O
should take 2, 3, 8 or 9. In fact, taking 9 is the best
move, since a non-perfect player X may take 4, then
O can take 7 to win).
• X1 → O5 → X6 → O2 → X8, then O should
not take 3, or X can take 7 to win, and O
should not take 4, or X can take 9 to win, O
should take 7 or 9.
◦ X1 → O5 → X6 → O2 → X8 → O7 →
X3 → O9 → X4, this game will be a
draw.
◦ X1 → O5 → X6 → O2 → X8 → O9 →
X4 (7) → O7 (4) → X3, this game will be
a draw.
• X1 → O5 → X6 → O3 → X7 → O4 → X8 (9)
→ O9 (8) → X2, this game will be a draw.
• X1 → O5 → X6 → O8 → X2 → O3 → X7 →
O4 → X9, this game will be a draw.
• X1 → O5 → X6 → O9, then X should not take
4, or O can take 7 to win, X should take 2, 3, 7
or 8.
◦ X1 → O5 → X6 → O9 → X2 → O3 →
X7 → O4 → X8, this game will be a
draw.
◦ X1 → O5 → X6 → O9 → X3 → O2 →
X8 → O4 (7) → X7 (4), this game will be
a draw.
◦ X1 → O5 → X6 → O9 → X7 → O4 →
X2 (3) → O3 (2) → X8, this game will be
a draw.
◦ X1 → O5 → X6 → O9 → X8 → O2 (3,
4, 7) → X4/7 (4/7, 2/3, 2/3) → O7/4 (7/4,
3/2, 3/2) → X3 (2, 7, 4), this game will
be a draw.
In both of these situations (X takes 9 or 6 as the
second move), X has a
1
/
3
property to win.
If X is not a perfect player, X may take 2 or 3 as a
second move. Then this game will be a draw, X
cannot win.
• X1 → O5 → X2 → O3 → X7 → O4 → X6 →
O8 (9) → X9 (8), this game will be a draw.
• X1 → O5 → X3 → O2 → X8 → O4 (6) → X6
(4) → O9 (7) → X7 (9), this game will be a
draw.
If X plays 1 opening move, and O is not a perfect
player, the following may happen:
Although O takes the only good position (5) as the
rst move, O takes a bad position as the second
move:
• X1 → O5 → X9 → O3 → X7, then X can take
4 or 8 to win.
• X1 → O5 → X6 → O4 → X3, then X can take
7 or 9 to win.
• X1 → O5 → X6 → O7 → X3, then X can take
2 or 9 to win.
Although O takes good positions in the rst two
moves, O takes a bad position in the third move:
• X1 → O5 → X6 → O2 → X8 → O3 → X7,
then X can take 4 or 9 to win.
• X1 → O5 → X6 → O2 → X8 → O4 → X9,
then X can take 3 or 7 to win.
O takes a bad position as rst move (except of 5, all
other positions are bad):
fi
fi
fi
• X1 → O3 → X7 → O4 → X9, then X can take
5 or 8 to win.
• X1 → O9 → X3 → O2 → X7, then X can take
4 or 5 to win.
• X1 → O2 → X5 → O9 → X7, then X can take
3 or 4 to win.
• X1 → O6 → X5 → O9 → X3, then X can take
2 or 7 to win.

This game is loved by all the children to make it


more fun. we recreated this game to be played in
computers . Children’s are playing this game in a
blank of shit with the help of Finn. It’s really hard to
draw the box again and again and to play. And from
now on there is no need to rub and draw it again
because we made it simple by creating a code that
is convenient to the players after nishing the game
the box automatically clear The OXs in the boxes
they played. This really made them to play in
different environment and give them a better
experience

For reference we attached the code in the following


pages
fi
Code for tic tac toe …

import random

class TicTacToe:

def __init__(self):
self.board = []

def create_board(self):
for i in range(3):
row = []
for j in range(3):
row.append('-')
self.board.append(row)

def get_random_ rst_player(self):


return random.randint(0, 1)

def x_spot(self, row, col, player):


self.board[row][col] = player
fi
fi
def is_player_win(self, player):
win = None

n = len(self.board)

# checking rows
for i in range(n):
win = True
for j in range(n):
if self.board[i][j] != player:
win = False
break
if win:
return win

# checking columns
for i in range(n):
win = True
for j in range(n):
if self.board[j][i] != player:
win = False
break
if win:
return win

# checking diagonals
win = True
for i in range(n):
if self.board[i][i] != player:
win = False
break
if win:
return win

win = True
for i in range(n):
if self.board[i][n - 1 - i] != player:
win = False
break
if win:
return win
return False

for row in self.board:


for item in row:
if item == '-':
return False
return True

def is_board_ lled(self):


for row in self.board:
for item in row:
if item == '-':
return False
return True

def swap_player_turn(self, player):


return 'X' if player == 'O' else 'O'

def show_board(self):
for row in self.board:
for item in row:
print(item, end=" ")
print()

def start(self):
self.create_board()
fi
player = 'X' if
self.get_random_ rst_player() == 1 else
'O'
while True:
print(f"Player {player} turn")

self.show_board()

# taking user input


row, col = list(
map(int, input("Enter row and
column numbers to x spot: ").split()))
print()

# xing the spot


self. x_spot(row - 1, col - 1,
player)

# checking whether current


player is won or not
if self.is_player_win(player):
print(f"Player {player} wins the
game!")
fi
fi
fi
fi
break

# checking whether the game is


draw or not
if self.is_board_ lled():
print("Match Draw!")
break

# swapping the turn


player =
self.swap_player_turn(player)

# showing the nal view of board


print()
self.show_board()

# starting the game


tic_tac_toe = TicTacToe()
tic_tac_toe.start()
fi
fi
Thank
You…

You might also like