[go: up one dir, main page]

100% found this document useful (1 vote)
396 views25 pages

CS Project

This document contains information related to a tic-tac-toe game project completed by Shriyash.S for their 12th standard Computer Science practical exam at Alwin International Public School in Chennai, India. It includes a bonafide certificate signed by the principal and teachers to verify that the project was Shriyash's original work. The document outlines the project contents which are an abstract, requirement analysis, source code, outputs, and bibliography. It provides details of Shriyash's registration number, class, subject, and school information for the exam.

Uploaded by

School Info
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
100% found this document useful (1 vote)
396 views25 pages

CS Project

This document contains information related to a tic-tac-toe game project completed by Shriyash.S for their 12th standard Computer Science practical exam at Alwin International Public School in Chennai, India. It includes a bonafide certificate signed by the principal and teachers to verify that the project was Shriyash's original work. The document outlines the project contents which are an abstract, requirement analysis, source code, outputs, and bibliography. It provides details of Shriyash's registration number, class, subject, and school information for the exam.

Uploaded by

School Info
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/ 25

ALWIN INTERNATIONAL PUBLIC

SCHOOL
Zion Avenue, Padappai, Chennai – 601301

AISSCE 2021-2022
TIC TAC TOE

NAME: SHRIYASH.S
REGISTER NO : .................................
STD : XII SEC: "A"
SUBJECT : Computer Science – Project
ALWIN INTERNATIONAL PUBLIC
SCHOOL
Zion Avenue, Padappai, Chennai – 601301

BONAFIDE CERTIFICATE
This is to certify that the project titled ‘Tic tac
toe’ is a Bonafide work done by Shriyash.S in partial
fulfillment of the requirements in Computer Science
practical as prescribed by CBSE for AISSCE 2021-2022.

REGISTER NO. _____________

TEACHER-IN-CHARGE PRINCIPAL

INTERNAL EXAMINER EXTERNAL EXAMINER


ACKNOWLEDGEMENT :
I take this opportunity to express my gratitude to the people who have been
instrumental in the successful completion of this project. l'express a deep
sense of gratitude to almighty God for giving me strength for the successful
completion of the project..

I express my heartfelt gratitude to my parents for constant encouragement


while carrying out this project.

I express my deep sense of gratitude to Ms Suguna ,The Principal of


Alwin International Public School, Padappai, who has been continuously
motivating and extending their helping hand to us.

I express my sincere thanks to Mr Raman, The Vice Principal of Alwin


International Public School for constant encouragement and the guidance
provided during this project.

My sincere thanks to Mrs. Subashini, Teacher In-charge. A guide, who


critically reviewed my project and helped in solving each and every
problem that occurred during the implementation of the project. The
guidance and support received from all the members who contributed to
this project, was vital for the success of my project. I am grateful for their
constant support and help.
CONTENTS:
o ABSTRACT

o REQUIREMENT ANALYSIS
● HARDWARE AND SOFTWARE
REQUIREMENTS

o SOURCE CODE

o OUTPUTS

o BIBLIOGRAPHY
ABSTRACT:
(A) INTRODUCTION:
This project is a program on the game’ tic tac toe’.Tic-tac-toe is a two
player game who take turns marking the spaces in a three-by-three grid . These
two players alternately place the marks X and O in one of the nine spaces in the
grid. The player who succeeds in placing three of their marks in a horizontal,
vertical, or diagonal row is the winner.this game can lead to a forced draw
assuming best play from both players. The other player also has to make sure
that the other player is not able to complete a full row.

This program can be used for playing tic tac toe by entering the position and
can be played by Single player against computer or multiplayer with another
person sitting next to him..

(B) OBJECTIVES OF THE PROJECT:


The objective of this project is to allow the user to play tic tac Toe by
a python program.

This is done by using two main software programming languages


namely python and MySQL.

1. Write programs utilising modern software tools.


2. Apply object oriented programming principles effectively when
developing projects.
3. Write effective procedural code to solve small to medium-sized
problems.
4. To demonstrate a breadth of knowledge in computers, as
exemplified in the areas of system theory and software
development.
5. To demonstrate the ability to conduct research requiring writing
and presentation skills which exemplify scholarly style in
computer science.
REQUIREMENT ANALYSIS:
HARDWARE :

● Operating system: Windows 7 or Above.


● Hard disk /SSD:40 GB Required ( Minimum)
● RAM: 256 MB (Minimum)
● Processor: Pentium(R)Dual-core CPU or Above.

SOFTWARE :

● Python IDLE 3.10 or Sublime Text Editor 3.2.2


● MySQŁ Version 8.0.28 ( Preferable)
● Python Libraries :

1. mysql.connector

2. time

3. datetime

● A Text Editor to Generate Records if Necessary.


functions.py
#single
al = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
def show_board(al):
loop=0

for i in al:
print(*i, sep = " | ")
loop+=1
if loop<=2:
print("___|____|___")
else:
print(" | | ")
print()

computer_moves = [
[1, 7, 9],
[1, 3, 9],
[1, 3],
[1, 3, 7],
[1, 7, 3]
]
winner = []
check_winner ='''[
[al[0][0], al[0][1], al[0][2]],
[al[1][0], al[1][1], al[1][2]],
[al[2][0], al[2][1], al[2][2]],
[al[0][0], al[1][0], al[2][0]],
[al[0][1], al[1][1], al[2][1]],
[al[0][2], al[1][2], al[2][2]],
[al[0][0], al[1][1], al[2][2]],
[al[0][2], al[1][1], al[2][0]]
]'''

def check_win(check_winner, winner):


for i in eval(check_winner):
if i[0] == i[1] and i[1] == i[2]:
winner.append(i[0])
if len(winner) == 1 :
return True
else:
return False

c1mx = 0
c2mx = 0
def excuteinpx(ipx,c1mx, c2mx, al):
if ipx == 1 or ipx == 2 or ipx == 3:
c1mx = 0
if ipx == 4 or ipx == 5 or ipx == 6:
c1mx = 1
if ipx == 7 or ipx == 8 or ipx == 9:
c1mx = 2
if ipx == 1 or ipx == 4 or ipx == 7:
c2mx = 0
if ipx == 2 or ipx == 5 or ipx == 8:
c2mx = 1
if ipx == 3 or ipx == 6 or ipx == 9:
c2mx = 2
al[c1mx][c2mx] = "X"
c1mo = 0
c2mo = 0
def excuteinpo(ipo,c1mo, c2mo, al):
if ipo == 1 or ipo == 2 or ipo == 3:
c1mo = 0
if ipo == 4 or ipo == 5 or ipo == 6:
c1mo = 1
if ipo == 7 or ipo == 8 or ipo == 9:
c1mo = 2
if ipo == 1 or ipo == 4 or ipo == 7:
c2mo = 0
if ipo == 2 or ipo == 5 or ipo == 8:
c2mo = 1
if ipo == 3 or ipo == 6 or ipo == 9:
c2mo = 2
al[c1mo][c2mo] ="O"

def savwhr(check_winner):
for i in eval(check_winner):
if i[0] == i[1] and i[0] == "O" and i[2] != "X" and i[2] != "O":
return i[2]
if i[0] == i[2] and i[0] == "O" and i[1] != "X" and i[1] != "O":
return i[1]
if i[2] == i[1] and i[2] == "O" and i[0] != "X" and i[0] != "O":
return i[0]
def chksav(check_winner):
for i in eval(check_winner):
if i[0] == i[1] and i[0] == "O" and i[2] != "X" and i[2] != "O":
return True
if i[0] == i[2] and i[0] == "O" and i[1] != "X" and i[1] != "O":
return True
if i[2] == i[1] and i[2] == "O" and i[0] != "X" and i[0] != "O":
return True
def finwhr(check_winner):
for i in eval(check_winner):
if i[0] == i[1] and i[0] == "X" and i[2] != "X" and i[2] !="O":
return i[2]
if i[0] == i[2] and i[0] == "X" and i[1] !="X" and i[1] != "O":
return i[1]
if i[2] == i[1] and i[2] == "X" and i[0] != "X" and i[0] != "O":
return i[0]
def chkfin(check_winner):
for i in eval(check_winner):
if i[0] == i[1] and i[0] =="X" and i[2] != "X" and i[2] != "O":
return True
if i[0] == i[2] and i[0] == "X" and i[1] != "X" and i[1] != "O":
return True
if i[2] == i[1] and i[2] == "X" and i[0] != "X" and i[0] != "O":
return True
av = []
def avmov(av, al):
av.clear()
for i in al:
for j in i:
if j != "X" and j !='O':
av.append(j)
source code:
print("\t \t Tic-Tac-Toe Game Designed By Adarsh sharma and shriyash")

print('\n','\n')

import time as t
import os
import mysql.connector
from functions import *
from datetime import datetime as dt

time= dt.now()

dt=time.replace(microsecond = 0)
print()
print(dt)
print()

#mysql connection
try:
mydb=mysql.connector.connect(host="localhost",user="root",passwd="ad12ad1
2",database="tic_tac_toe")
myc=mydb.cursor()

if mydb.is_connected():
print('successfully connection made with sql')

print('========================================================
===========')
except:
print('connection.........ERROR')

while True:

print('========================================================
===========')
print("1.single")
print("2.multiplayer")
print('3.HISTORY')
print('4.EXIT')
choice=int(input('enter your choice:'))

print('========================================================
============')
if choice==1:
# the master piece

name=input("enter player name :")


player1='computer'
player2=name
show_board(al)
cm = 0
x_moves = 0

s=[]

moves = 0
cdd = 3
while True:
if chkfin(check_winner):
ipx = finwhr(check_winner)
elif chksav(check_winner):
ipx = savwhr(check_winner)
elif x_moves < cdd:
ipx = computer_moves[cm][x_moves]
else:
ipx = av[0]
print("Computer's turn...... ")
print()
print()
t.sleep(1)
excuteinpx(ipx, c1mx, c2mx, al)

x_moves += 1

s.append(ipx)

moves += 1
avmov(av, al)
show_board(al)
if check_win(check_winner, winner):
break
if moves == 9:
break

#User
while True:
print(name,"'s turn....")
t.sleep(1)
ipo = int(input("Enter the position for O: "))
if ipo in s:
print("please enter valid position.....")
print('\n')
continue
else:
break

excuteinpo(ipo, c1mo, c2mo, al)


s.append(ipo)
while x_moves == 1:
if ipo == 2 or ipo == 3:
cm = 0
break
if ipo == 4 or ipo == 7:
cm = 1
break
if ipo == 5:
cm = 2
cdd = 2
break
if ipo == 6 or ipo == 9:
cm = 3
break
if ipo == 8:
cm = 4
break
moves += 1
avmov(av, al)
if check_win(check_winner, winner):
break
show_board(al)

if len(winner) == 1 and winner[0] =="X":


print( "Computer WON!")
result= 'P1 WINS'
break
elif len(winner) == 1 and winner[0] !="X":
print( "You WON!")
result= 'P2 WINS'
break
else:
print( "D" + "R"+ "A" + "W")
result='DRAW'

query="insert into history_XO (player1,player2,result,played_time) values


('{}','{}','{}','{}')".format(player1,player2,result,dt)
myc.execute(query)
mydb.commit()

#multiplayer

if choice==2:

board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
player = 1

########win Flags##########
Win = 1
Draw = -1
Running = 0
Stop = 1
###########################
Game = Running
Mark = 'X'
result=''

#This Function Draws Game Board


def DrawBoard():
print(" %c | %c | %c " % (board[1],board[2],board[3]))
print("___|___|___")
print(" %c | %c | %c " % (board[4],board[5],board[6]))
print("___|___|___")
print(" %c | %c | %c " % (board[7],board[8],board[9]))
print(" | | ")

#This Function Checks position is empty or not


def CheckPosition(x):
if(board[x] == ' '):
return True
else:
return False

#This Function Checks player has won or not


def CheckWin():
global Game
#Horizontal winning condition
if(board[1] == board[2] and board[2] == board[3] and board[1] != ' '):
Game = Win
elif(board[4] == board[5] and board[5] == board[6] and board[4] != ' '):
Game = Win
elif(board[7] == board[8] and board[8] == board[9] and board[7] != ' '):
Game = Win
#Vertical Winning Condition
elif(board[1] == board[4] and board[4] == board[7] and board[1] != ' '):
Game = Win
elif(board[2] == board[5] and board[5] == board[8] and board[2] != ' '):
Game = Win
elif(board[3] == board[6] and board[6] == board[9] and board[3] != ' '):
Game=Win
#Diagonal Winning Condition
elif(board[1] == board[5] and board[5] == board[9] and board[5] != ' '):
Game = Win
elif(board[3] == board[5] and board[5] == board[7] and board[5] != ' '):
Game=Win
#Match Tie or Draw Condition
elif(board[1]!=' ' and board[2]!=' ' and board[3]!=' ' and board[4]!=' ' and
board[5]!=' ' and board[6]!=' ' and board[7]!=' ' and board[8]!=' ' and
board[9]!=' '):
Game=Draw
else:
Game=Running

player1=input("enter player name [X]:")


player2=input("enter player name [O]:")
print(player1,"[X]---" ,player2 ,"[O]\n")
print()
print()
print("Please Wait...")
t.sleep(1)
while(Game == Running):
#os.system('cls') #to open a procesing unit

DrawBoard()
if(player % 2 != 0):
print(player1,"'s chance")
Mark = 'X'
else:
print(player2,"'s chance")
Mark = 'O'
choice = int(input("Enter the position between [1-9] where you want to
mark : "))

if choice not in range(1,10):


print('please enter positions between [1-9]')
player+=2
continue
elif(CheckPosition(choice)):
board[choice] = Mark
player+=1
CheckWin()

#os.system('cls')
DrawBoard()
if(Game==Draw):
print("Game Draw")
elif(Game==Win):
player-=1
if(player%2!=0):
print(player1," Won")
else:
print(player2," Won")

def winner ():


global result
if (Game==Draw):
result= 'DRAW'
return('Draw')
elif(player%2!=0):
result= 'P1 WINS'
return (player1)
else:
result= 'P2 WINS'
return(player2)
wnn=winner()
query="insert into history_XO (player1,player2,result,played_time) values
('{}','{}','{}','{}')".format(player1,player2,result,dt)
myc.execute(query)
mydb.commit()

if choice==4:
break

if choice==3:
#HISTORY
try:
Q2='select * from history_xo'
myc.execute(Q2)
records=myc.fetchall()
for i in records:
print('player 1 [P1]=',i[0],end=' ')
print('player 2 [P2]=',i[1],end=' ')
print('result=', i[2],end=' ')
print('time=',i[3])
mydb.commit()
except:
print()

print("done.....")
OUTPUT:
mysql;
database name = ‘tic_tac_toe’

table name= ‘history_XO’

description of table:

WHEN YOU RUN THE PROGRAM;


WHEN YOU CHOOSE SINGLE PLAYER:
WHEN YOU CHOOSE MULTIPLAYER:
WHEN YOU CHOOSE HISTORY:

WHEN YOU WANT TO EXIT THE GAME :


Bibliography and Reference:
https://www.programiz.com/python-programming
https://www.geeksforgeeks.org/
https://www.google.co.in/
https://dev.mysql.com/doc/refman/8.0/en/data-typ
es.html
https://www.w3schools.com/

You might also like