Skip to content

Commit 5b4d34a

Browse files
ankit0905t2013anurag
authored andcommitted
Add TicTacToe in python (hacktoberfest17#953)
1 parent c3ea2c4 commit 5b4d34a

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

tic_tac_toe/python/tictactoe.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from tkinter import *
2+
import random
3+
4+
# Initialize Variables
5+
grid = [[0 for i in range(3)] for j in range(3)]
6+
numClicks = 0
7+
isDone = False
8+
isXTurn = True
9+
10+
# Function to restart the Game
11+
def resetGame():
12+
global numClicks
13+
global isDone
14+
global isXTurn
15+
for rw in range(0,3):
16+
for col in range(0,3):
17+
grid[rw][col].config(text=" ")
18+
labelStatus.config(text="X's turn")
19+
numClicks = 0
20+
isDone = False
21+
isXTurn = True
22+
23+
# Function to mark the Box appropriately
24+
def markSpace(rw, col):
25+
global numClicks
26+
global isDone
27+
global isXTurn
28+
if isDone == True:
29+
return
30+
space = grid[rw][col].cget('text')
31+
if space == " ":
32+
if isXTurn == True:
33+
grid[rw][col].config(text="X", fg="red")
34+
labelStatus.config(text="X's Turn")
35+
else:
36+
grid[rw][col].config(text="O", fg="blue")
37+
labelStatus.config(text="O's Turn")
38+
else:
39+
labelStatus.config(text='Invalid Move')
40+
return
41+
isXTurn = not isXTurn
42+
numClicks += 1
43+
gameOver(rw, col)
44+
45+
# Function to check if Game is Over
46+
def gameOver(rw, col) :
47+
global numClicks
48+
global isDone
49+
global isXTurn
50+
winner = ' '
51+
52+
if(grid[0][0].cget('text') == grid[1][1].cget('text') and grid[1][1].cget('text') == grid[2][2].cget('text')) :
53+
winner = grid[0][0].cget('text')
54+
elif(grid[2][0].cget('text') == grid[1][1].cget('text') and grid[1][1].cget('text') == grid[0][2].cget('text')) :
55+
winner = grid[2][1].cget('text')
56+
57+
else :
58+
for r in range(0, 3) :
59+
if(grid[r][0].cget('text') != ' ' and grid[r][0].cget('text') == grid[r][1].cget('text') and grid[r][1].cget('text') == grid[r][2].cget('text')):
60+
winner = grid[r][0].cget('text')
61+
elif(grid[0][r].cget('text') != ' ' and grid[0][r].cget('text') == grid[1][r].cget('text') and grid[1][r].cget('text') == grid[2][r].cget('text')):
62+
winner = grid[0][r].cget('text')
63+
64+
isDone = True
65+
if(winner == ' ' and numClicks >= 9) :
66+
labelStatus.config(text = 'Tie Game')
67+
elif(winner != ' ') :
68+
labelStatus.config(text = winner + ' Wins!')
69+
else :
70+
labelStatus.config(text = 'X\'s Turn' if isXTurn else 'O\'s Turn')
71+
isDone = False
72+
73+
74+
# Setup the main GUI
75+
root = Tk()
76+
root.title("Tic-Tac-Toe")
77+
root.geometry("355x420")
78+
topFrame = Frame(root, width=320, height=40)
79+
topFrame.place(x=12, y=12)
80+
labelStatus = Label(topFrame, text="X's Turn", bg="yellow", fg="blue", font=("serif"), width=30)
81+
labelStatus.pack(side="top")
82+
mainFrame = Frame(root, width=315, height=300, bg="black")
83+
mainFrame.place(x=10, y=42)
84+
85+
for rw in range(0,3):
86+
for col in range(0,3):
87+
grid[rw][col] = Button(mainFrame, text=" ", relief="solid", command=lambda rw=rw, col=col: markSpace(rw,col))
88+
grid[rw][col].config(font="monospace 36 bold", fg="red", height=1, width=2)
89+
grid[rw][col].place(x=rw*105+10, y=col*105+10)
90+
91+
btnRestart = Button(root, text="Restart", command = resetGame, width=30)
92+
btnRestart.place(x=45, y=380)
93+
root.mainloop()

0 commit comments

Comments
 (0)