This repository was archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathtic-tac-toe.py
More file actions
135 lines (99 loc) · 4.62 KB
/
tic-tac-toe.py
File metadata and controls
135 lines (99 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
# coding: utf-8
# # Tic - Tac - Toe Game :
# In[5]:
print("---------------------------------------")
print("Welcome to tic tac toe")
print("---------------------------------------")
name1=input("Player 1 name:")
name2=input("Player 2 name:")
from tkinter import *
import tkinter.messagebox
tk = Tk()
tk.title("Tic Tac Toe game")
pa = StringVar()
playerb = StringVar()
p1 = StringVar()
p2 = StringVar()
player1_name = Entry(tk, textvariable=p1, bd=5)
player1_name.grid(row=1, column=1, columnspan=8)
player2_name = Entry(tk, textvariable=p2, bd=5)
player2_name.grid(row=2, column=1, columnspan=8)
bclick = True
flag = 0
def disableButton():
b1.configure(state=DISABLED)
b2.configure(state=DISABLED)
b3.configure(state=DISABLED)
b4.configure(state=DISABLED)
b5.configure(state=DISABLED)
b6.configure(state=DISABLED)
b7.configure(state=DISABLED)
b8.configure(state=DISABLED)
b9.configure(state=DISABLED)
def btnClick(bs):
global bclick, flag, player2_name, player1_name, playerb, pa
if bs["text"] == " " and bclick == True:
bs["text"] = "X"
bclick = False
playerb = p2.get() + " Wins!"
pa = p1.get() + " Wins!"
checkForWin()
flag += 1
elif bs["text"] == " " and bclick == False:
bs["text"] = "O"
bclick = True
checkForWin()
flag += 1
else:
tkinter.messagebox.showinfo("Tic-Tac-Toe", "Button has been already Clicked!")
def checkForWin():
if (b1['text'] == 'X' and b2['text'] == 'X' and b3['text'] == 'X' or
b4['text'] == 'X' and b5['text'] == 'X' and b6['text'] == 'X' or
b7['text'] =='X' and b8['text'] == 'X' and b9['text'] == 'X' or
b1['text'] == 'X' and b5['text'] == 'X' and b9['text'] == 'X' or
b3['text'] == 'X' and b5['text'] == 'X' and b7['text'] == 'X' or
b1['text'] == 'X' and b2['text'] == 'X' and b3['text'] == 'X' or
b1['text'] == 'X' and b4['text'] == 'X' and b7['text'] == 'X' or
b2['text'] == 'X' and b5['text'] == 'X' and b8['text'] == 'X' or
b7['text'] == 'X' and b6['text'] == 'X' and b9['text'] == 'X'):
disableButton()
tkinter.messagebox.showinfo("Tic-Tac-Toe", pa)
elif(flag == 8):
tkinter.messagebox.showinfo("Tic-Tac-Toe", "No winner, there is a tie")
elif (b1['text'] == 'O' and b2['text'] == 'O' and b3['text'] == 'O' or
b4['text'] == 'O' and b5['text'] == 'O' and b6['text'] == 'O' or
b7['text'] == 'O' and b8['text'] == 'O' and b9['text'] == 'O' or
b1['text'] == 'O' and b5['text'] == 'O' and b9['text'] == 'O' or
b3['text'] == 'O' and b5['text'] == 'O' and b7['text'] == 'O' or
b1['text'] == 'O' and b2['text'] == 'O' and b3['text'] == 'O' or
b1['text'] == 'O' and b4['text'] == 'O' and b7['text'] == 'O' or
b2['text'] == 'O' and b5['text'] == 'O' and b8['text'] == 'O' or
b7['text'] == 'O' and b6['text'] == 'O' and b9['text'] == 'O'):
disableButton()
tkinter.messagebox.showinfo("Tic-Tac-Toe", playerb)
bs = StringVar()
label = Label( tk, text="Player 1:", font='Times 20 bold', bg='white', fg='black', height=1, width=8)
label.grid(row=1, column=0)
label = Label( tk, text="Player 2:", font='Times 20 bold', bg='white', fg='black', height=1, width=8)
label.grid(row=2, column=0)
b1 = Button(tk, text=" ", font='Times 20 bold', bg='gray', fg='white', height=4, width=8, command=lambda: btnClick(b1))
b1.grid(row=3, column=0)
b2 = Button(tk, text=' ', font='Times 20 bold', bg='gray', fg='white', height=4, width=8, command=lambda: btnClick(b2))
b2.grid(row=3, column=1)
b3 = Button(tk, text=' ',font='Times 20 bold', bg='gray', fg='white', height=4, width=8, command=lambda: btnClick(b3))
b3.grid(row=3, column=2)
b4 = Button(tk, text=' ', font='Times 20 bold', bg='gray', fg='white', height=4, width=8, command=lambda: btnClick(b4))
b4.grid(row=4, column=0)
b5 = Button(tk, text=' ', font='Times 20 bold', bg='gray', fg='white', height=4, width=8, command=lambda: btnClick(b5))
b5.grid(row=4, column=1)
b6 = Button(tk, text=' ', font='Times 20 bold', bg='gray', fg='white', height=4, width=8, command=lambda: btnClick(b6))
b6.grid(row=4, column=2)
b7 = Button(tk, text=' ', font='Times 20 bold', bg='gray', fg='white', height=4, width=8, command=lambda: btnClick(b7))
b7.grid(row=5, column=0)
b8 = Button(tk, text=' ', font='Times 20 bold', bg='gray', fg='white', height=4, width=8, command=lambda: btnClick(b8))
b8.grid(row=5, column=1)
b9 = Button(tk, text=' ', font='Times 20 bold', bg='gray', fg='white', height=4, width=8, command=lambda: btnClick(b9))
b9.grid(row=5, column=2)
tk.mainloop()
# In[ ]: