forked from kartikeysingh6/kartik_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomTicTacToe.py
142 lines (132 loc) · 5.86 KB
/
RandomTicTacToe.py
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
132
133
134
135
136
137
138
139
140
141
142
from tkinter import*
from tkinter import messagebox
import random
root=Tk()
root.title("Tic Tac Toe")
user_cord=[]
comp_cord=[]
def clicked(r,c,m):
w=0
Button(root, text="X",padx=40,pady=40, bg='#fdff79', state=DISABLED).grid(row=r, column=c)
if(len(user_cord)!=5):
rr=random.randint(0,2)
rc=random.randint(0,2)
rm=3*rr + rc +1 #formula to conver m into rows and columns
if(rm!=m and (str(rm) not in comp_cord) and (str(rm) not in user_cord)):
comp_cord.append(str(rm))
user_cord.append(str(m))
print("USER Input: ")
print(user_cord)
print("COMPUTER Input: ")
print(comp_cord)
Button(root, text="O",padx=40,pady=40, bg='#99faff',state=DISABLED).grid(row=rr, column=rc)
#for player
if('1' in user_cord and '2' in user_cord and '3' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
w=1
disable()
elif('4' in user_cord and '5' in user_cord and '6' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
w=1
elif('7' in user_cord and '8' in user_cord and '9' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
w=1
elif('1' in user_cord and '4' in user_cord and '7' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
w=1
elif('2' in user_cord and '5' in user_cord and '8' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
w=1
elif('3' in user_cord and '6' in user_cord and '9' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
w=1
elif('1' in user_cord and '5' in user_cord and '9' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
w=1
elif('3' in user_cord and '5' in user_cord and '7' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
w=1
#for computer
if(w!=1):
if('1' in comp_cord and '2' in comp_cord and '3' in comp_cord):
messagebox.showwarning("O Won!", "Computer Won!")
disable()
elif('4' in comp_cord and '5' in comp_cord and '6' in comp_cord):
messagebox.showwarning("O Won!", "Computer Won!")
disable()
elif('7' in comp_cord and '8' in comp_cord and '9' in comp_cord):
messagebox.showwarning("O Won!", "Computer Won!")
disable()
elif('1' in comp_cord and '4' in comp_cord and '7' in comp_cord):
messagebox.showwarning("O Won!", "Computer Won!")
disable()
elif('2' in comp_cord and '5' in comp_cord and '8' in comp_cord):
messagebox.showwarning("O Won!", "Computer Won!")
disable()
elif('3' in comp_cord and '6' in comp_cord and '9' in comp_cord):
messagebox.showwarning("O Won!", "Computer Won!")
disable()
elif('1' in comp_cord and '5' in comp_cord and '9' in comp_cord):
messagebox.showwarning("O Won!", "Computer Won!")
disable()
elif('3' in comp_cord and '5' in comp_cord and '7' in comp_cord):
messagebox.showwarning("O Won!", "Computer Won!")
disable()
elif(len(user_cord)>3):
user_cord.append(str(m))
print(user_cord)
print(comp_cord)
#for player
if('1' in user_cord and '2' in user_cord and '3' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
elif('4' in user_cord and '5' in user_cord and '6' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
elif('7' in user_cord and '8' in user_cord and '9' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
elif('1' in user_cord and '4' in user_cord and '7' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
elif('2' in user_cord and '5' in user_cord and '8' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
elif('3' in user_cord and '6' in user_cord and '9' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
elif('1' in user_cord and '5' in user_cord and '9' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
elif('3' in user_cord and '5' in user_cord and '7' in user_cord):
messagebox.showwarning("X Won!", "Player Won!")
disable()
else:
clicked(r,c,m)
Button(root, text="",padx=40,pady=40, command=lambda: clicked(0,0,1)).grid(row=0, column=0)
Button(root, text="",padx=40,pady=40, command=lambda: clicked(0,1,2)).grid(row=0, column=1)
Button(root, text="",padx=40,pady=40, command=lambda: clicked(0,2,3)).grid(row=0, column=2)
Button(root, text="",padx=40,pady=40, command=lambda: clicked(1,0,4)).grid(row=1, column=0)
Button(root, text="",padx=40,pady=40, command=lambda: clicked(1,1,5)).grid(row=1, column=1)
Button(root, text="",padx=40,pady=40, command=lambda: clicked(1,2,6)).grid(row=1, column=2)
Button(root, text="",padx=40,pady=40, command=lambda: clicked(2,0,7)).grid(row=2, column=0)
Button(root, text="",padx=40,pady=40, command=lambda: clicked(2,1,8)).grid(row=2, column=1)
Button(root, text="",padx=40,pady=40, command=lambda: clicked(2,2,9)).grid(row=2, column=2)
def disable():
Button(root, text="",padx=40,pady=40, bg='#99ffa4', state=DISABLED).grid(row=0, column=0)
Button(root, text="",padx=40,pady=40, bg='#99ffa4', state=DISABLED).grid(row=0, column=1)
Button(root, text="",padx=40,pady=40, bg='#99ffa4', state=DISABLED).grid(row=0, column=2)
Button(root, text="",padx=40,pady=40, bg='#99ffa4', state=DISABLED).grid(row=1, column=0)
Button(root, text="",padx=40,pady=40, bg='#99ffa4', state=DISABLED).grid(row=1, column=1)
Button(root, text="",padx=40,pady=40, bg='#99ffa4', state=DISABLED).grid(row=1, column=2)
Button(root, text="",padx=40,pady=40, bg='#99ffa4', state=DISABLED).grid(row=2, column=0)
Button(root, text="",padx=40,pady=40, bg='#99ffa4', state=DISABLED).grid(row=2, column=1)
Button(root, text="",padx=40,pady=40, bg='#99ffa4', state=DISABLED).grid(row=2, column=2)
root.mainloop()