Skip to content

Commit 3b335e9

Browse files
authored
PR #60: Updated Tic-Tac-Toe Project
Updated tic-tac-toe Project Merge pull request #60 from Grow-with-Open-Source/tic-tac-toe
2 parents 24c9977 + c6fc940 commit 3b335e9

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

TIC_TAC_TOE/TIC_TAC_TOE.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
from tkinter import *
22
import tkinter.messagebox
3+
34
tk = Tk()
45
tk.title("Tic Tac Toe")
56
tk.configure(bg='yellow')
7+
68
p1 = StringVar()
79
p2 = StringVar()
10+
811
player1_name = Entry(textvariable=p1, bd=5, bg='white', width=40)
912
player1_name.grid(row=1, column=1, columnspan=8)
13+
1014
player2_name = Entry(tk, textvariable=p2, bd=5, bg='white', width=40)
1115
player2_name.grid(row=2, column=1, columnspan=8)
16+
1217
bclick = True
1318
flag = 0
1419
current_player_name = p1.get() if p1.get() else 'X'
20+
21+
1522
def disableButton():
1623
for i in range(3):
1724
for j in range(3):
1825
buttons[i][j].configure(state=DISABLED)
26+
27+
1928
def checkForWin():
2029
for i in range(3):
2130
if buttons[i][0]["text"] == buttons[i][1]["text"] == buttons[i][2]["text"] != " ":
2231
buttons[i][0].config(bg="green")
2332
buttons[i][1].config(bg="green")
2433
buttons[i][2].config(bg="green")
2534
disableButton()
26-
winner_name = p1.get() if buttons[i][0]['text'] == 'X' else p2.get()
35+
winner_name = p1.get(
36+
) if buttons[i][0]['text'] == 'X' else p2.get()
2737
if not winner_name:
2838
winner_name = 'Player 1' if buttons[i][0]['text'] == 'X' else 'Player 2'
2939
tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!")
@@ -33,7 +43,8 @@ def checkForWin():
3343
buttons[1][i].config(bg="green")
3444
buttons[2][i].config(bg="green")
3545
disableButton()
36-
winner_name = p1.get() if buttons[0][i]['text'] == 'X' else p2.get()
46+
winner_name = p1.get(
47+
) if buttons[0][i]['text'] == 'X' else p2.get()
3748
if not winner_name:
3849
winner_name = 'Player 1' if buttons[0][i]['text'] == 'X' else 'Player 2'
3950
tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!")
@@ -60,6 +71,8 @@ def checkForWin():
6071
return
6172
if flag == 8:
6273
tkinter.messagebox.showinfo("Tic-Tac-Toe", "It's a Tie")
74+
75+
6376
def resetGame():
6477
global bclick, flag, current_player_name
6578
for i in range(3):
@@ -69,9 +82,16 @@ def resetGame():
6982
bclick = True
7083
flag = 0
7184
current_player_name = p1.get() if p1.get() else 'X'
72-
Label(tk, text="Player 1:", font='Times 20 bold', bg='yellow', fg='black', height=1, width=8).grid(row=1, column=0)
73-
Label(tk, text="Player 2:", font='Times 20 bold', bg='yellow', fg='black', height=1, width=8).grid(row=2, column=0)
74-
buttons = [[Button(tk, text=' ', font='Times 20 bold', bg='black', fg='white', height=4, width=8) for _ in range(3)] for _ in range(3)]
85+
86+
87+
Label(tk, text="Player 1:", font='Times 20 bold', bg='yellow',
88+
fg='black', height=1, width=8).grid(row=1, column=0)
89+
Label(tk, text="Player 2:", font='Times 20 bold', bg='yellow',
90+
fg='black', height=1, width=8).grid(row=2, column=0)
91+
buttons = [[Button(tk, text=' ', font='Times 20 bold', bg='black',
92+
fg='white', height=4, width=8) for _ in range(3)] for _ in range(3)]
93+
94+
7595
def btnClick(button):
7696
global bclick, flag
7797
if button["text"] == " ":
@@ -84,10 +104,14 @@ def btnClick(button):
84104
checkForWin()
85105
else:
86106
tkinter.messagebox.showinfo("Tic-Tac-Toe", "Button already Clicked!")
107+
108+
87109
for i in range(3):
88110
for j in range(3):
89-
buttons[i][j].configure(command=lambda row=i, col=j: btnClick(buttons[row][col]))
111+
buttons[i][j].configure(command=lambda row=i,
112+
col=j: btnClick(buttons[row][col]))
90113
buttons[i][j].grid(row=i + 3, column=j)
91-
reset_button = Button(tk, text="Reset Game", font='Times 16 bold', bg='white', fg='black', command=resetGame)
114+
reset_button = Button(tk, text="Reset Game", font='Times 16 bold',
115+
bg='white', fg='black', command=resetGame)
92116
reset_button.grid(row=6, column=0, columnspan=3)
93-
tk.mainloop()
117+
tk.mainloop()

0 commit comments

Comments
 (0)