CS Project
CS Project
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.
TEACHER-IN-CHARGE PRINCIPAL
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..
SOFTWARE :
1. mysql.connector
2. time
3. datetime
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]]
]'''
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
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
#multiplayer
if choice==2:
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
player = 1
########win Flags##########
Win = 1
Draw = -1
Running = 0
Stop = 1
###########################
Game = Running
Mark = 'X'
result=''
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 : "))
#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")
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’
description of table: