Beruflich Dokumente
Kultur Dokumente
PRIYANSHU MAURYA
CHAND
OF CLASS 12TH SC.
CERTIFICATE
The project entitled ‘Tic Tac Toe’ toe is a
bonafide work done by Priyanshu Maurya,
Yankee Chaudhary and Chand of class XII
science session 2019-20 in partial fulfillment
of CBSE’S AISSCE Examination 2020 and
has been carried out under the direct
supervision and guidance of our computer
teacher MISS.SUSHMA NEGI (PGT
COMP.SC.). This project is found worthy of
acceptance as final project for the subject
computer science of class XII.
ACKNOWLEDGEMENT
We take this opportunity to
express our gratitude to our
worthy principal Mr. Jugal
Kishore for his support
throughout the course.
INDEX
S.NO. TITLE PAGE
NO.
1. ABSTRACT 6
2. INTODUCTION 7
3. DESIGNED SOLUTION 8
10
4. SOURCE CODE
5. REPORTS 14
6. LEARNING OUTCOMES 17
7. BIBLIOGRAPHY 19
6
ABSTRACT
The game TIC TAC TOE had an early variant
which began in the first century in the Roman
Empire . During that time , it was called Terni
Lapilli where the players only had three pieces
and had to move around the empty spaces to
play. The actual game of TIC TAC TOE could be
traced back to ancient Egypt. The game was
also known during that time as “Three Men’s
Morries ”.The first reference to Noughts and
crosses was made in 1864 in a British novel
called “Can You Forgive Her”. For many years
the game was referred to as noughts and
crosses but was changed in the 20th century by
the United States to TIC TAC TOE.
The purpose of this
documentation is to
capture all the
requirements by which
the user can play a
game of TIC TAC TOE
in Graphical User
Interface as well as
they can develop a
logic that what is
actually happening.
7
INTRODUCTION
One of the most universally played childhood
game is TIC TAC TOE game is developed where
a player will be able to play against computer in
a suitable GUI by using proper mouse
movements. This game will start by showing a
simple display, prompt the user for a move and
then prints the new board. The board looks like
a large hash symbol(#) with nine slots that can
each contain either an X, an O or a blank. There
is one player (the X player) playing against
machine (the O player) . By default the X player
takes the initiative. The game will end in two
situations: a win, when one player gets three in
a row, horizontally, vertically or diagonally or a
draw, when there are no remaining places to
choose and neither of them has won.
DESIGNED SOLUTION
SYSTEM FLOW DIAGRAM
9
1 2 3
4 5 6
7 8 9
10
SOURCE CODE
board = [' ' for x in range(10)]
def spaceIsFree(pos):
return board[pos] == ' ‘
def selectRandom(li):
import random
ln = len(li)
r = random.randrange(0, ln)
return li[r]
def isBoardFull(board):
if board.count(' ') > 1:
return False
else:
return True
11
def playerMove():
run = True
while run:
move = input('Please select a position to place an
\'X\' (1-9): ')
try:
move = int(move)
if move > 0 and move < 10:
if spaceIsFree(move):
run = False
insertBoard('X', move)
else:
print('This postion is already occupied!')
else:
print('Please type a number within the range!')
except:
print('Please type a number!')
def compMove():
possibleMoves = [x for x, letter in enumerate(board) if
letter == ' '
and x != 0]
move = 0
#Check for possible winning move to take or to block
opponents winning move.
for let in ['O','X']:
for i in possibleMoves:
boardCopy = board[:]
boardCopy[i] = let
if isWinner(boardCopy, let):
move = i
return move
12
#Try to take one of the corners
cornersOpen = []
for i in possibleMoves:
if i in [1,3,7,9]:
cornersOpen.append(i)
if len(cornersOpen) > 0:
move = selectRandom(cornersOpen)
return move
#Try to take the center
if 5 in possibleMoves:
move = 5
return move
#Take any edge
edgesOpen = []
for i in possibleMoves:
if i in [2,4,6,8]:
edgesOpen.append(i)
if len(edgesOpen) > 0:
move = selectRandom(edgesOpen)
return move
def printBoard():
# "board" is a list of 10 strings representing the board
(ignore index 0)
print(' | |')
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
print(' | |')
13
def main():
#Main game loop
print('Welcome to Tic Tac Toe, to win, complete a
straight line of your letter(Diagonal, Horizontal, Vertical).
The board has positions 1-9 starting at the top left.')
printBoard()
while not(isBoardFull(board)):
if not(isWinner(board, 'O')):
playerMove()
printBoard()
else:
print('O\\\'s win this time...')
break
if not(isWinner(board, 'X')):
move = compMove()
if move == 0:
print('Game is a Tie! No more spaces left to
move.')
else:
insertBoard('O', move)
print('Computer placed an \\\'O\\\' in position',
move)
printBoard()
else:
print(‘you win, good job!')
break
main()
while True:
answer = input('Do you want to play again? (Y/N)')
if answer.lower() == 'y' or answer.lower == 'yes':
board = [' ' for x in range(10)]
print('-----------------------------------')
main()
else:
break
14
REPORTS
15
16
17
Learning outcomes
Allow The Players To Choose The Mode
Of The Game:
An option to select the mode of the game that is
whether the player wants to play the game with
his/her friends or with the machine.
Allow The Players To Define The
Metrics:
An option to increase the board to size to n * n,
where n can be any positive number. This will
increase the complexity of the code and when
added with artificial intelligence will greatly make
the game rebust . Also, we can add a feature
where k<n consecutive marks/dots (filled by the
user) is a points/score. Or we can go even further
and use a range of number n.
18
BIBLIOGRAPHY
BOOKS REFERENCES
Computer Science with Python - Textbook for
Class 12th - SUMITA ARORA
Computer Science with Python - Textbook for
Class 11th - SUMITA ARORA
WEBSITES
https://youtu.be/5s_IGC2sxwq
https://youtu.be/jAaJZLqryTI