Sie sind auf Seite 1von 19

 YANKEE CHAUDHARY

 PRIYANSHU MAURYA
 CHAND
OF CLASS 12TH SC.

MISS. SUSHMA NEGI


2
3

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.

Signature of Students Signature of Teacher


Yankee Chaudhary MISS.SUSHMA NEGI
Priyanshu Maurya
(PGT COMP.SCI)
Chand
___________ ________________
___________
___________
4

ACKNOWLEDGEMENT
 We take this opportunity to
express our gratitude to our
worthy principal Mr. Jugal
Kishore for his support
throughout the course.

 We also express our sincere


thanks to PGT Computer
Science Miss.Shushma Negi
for her valuable guidance and
support .

 Finally, we thanks all our


friends involved directly or
indirectly in the completion of
this project.
5

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.

 Here are some rules for the game:


 The players with symbol ‘X’ goes first.
 Players alternate placing X s or O s on the board
until either one player has three in a row,
horizontally, vertically or diagonally; or all nine
squares are filled.
 The player who can draw three X s or three O s in
a row wins.
 If all nine squares are filled and none of the
players have three in a row, the game is a draw.
8

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 insertBoard(letter, pos):


global board
board[pos] = letter

def spaceIsFree(pos):
return board[pos] == ' ‘

def isWinner(bo, le):


# Given a board and a player’s letter, this function returns True if
that player has won.
# We use bo instead of board and le instead of letter so we don’t
have to type as much.
return((bo[7] ==le and bo[8] ==le and bo[9] ==le) or #across the top
(bo[4] ==le and bo[5] ==le and bo[6] ==le) or #across the middle
(bo[1] ==le and bo[2] ==le and bo[3] ==le) or #across the bottom
(bo[7] == le and bo[4] ==le and bo[1] ==le) or #down the left side
(bo[8] ==le and bo[5] ==le and bo[2] ==le) or #down the middle
(bo[9] ==le and bo[6] ==le and bo[3] ==le) or #down the right side
(bo[7] ==le and bo[5] ==le and bo[3] ==le) or #diagonal
(bo[9] ==le and bo[5] ==le and bo[1] ==le)) #diagonal

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

Introduction Of Difficulty Levels:


One option to select the difficulty levels of the game
can be added. We would also like to create a
computer AI(artificial intelligence) that offered
increasing resistance as the study level increased.
User Profiles And Scores:
One of the other enhancements could be that users
can create their profiles and save their scores.
IN-Game Assistant:
An option to include an Assistant which will provide
hints and suggestions to the user when playing
against a bot.
Introduction Of Artificial Intelligence
An option of playing against the computer(machine)
can be added too. An algorithm can be implemented
for the machine based on actual rules player uses
to fill the TIC TAC TOE board. This can pose a
challenge to the user when playing.
19

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

Das könnte Ihnen auch gefallen