-
Notifications
You must be signed in to change notification settings - Fork 1
/
TicTacToe.py
151 lines (135 loc) · 4.42 KB
/
TicTacToe.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
143
144
145
146
147
148
149
150
151
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from random import randint
#Global Variables
ActivePlayer=1
p1=[] #what player one selected
p2=[] #what player two selected
root = Tk()
root.title("Tic Tac Toe :Player 1")
#style=ttk.Style()
#style.theme_use('classic')
#Add Buttons
bu1=ttk.Button(root,text=' ')
bu1.grid(row=0,column=0,sticky='snew',ipadx=40,ipady=40)
bu1.config(command=lambda: ButtonClick(1))
bu2=ttk.Button(root,text=' ')
bu2.grid(row=0,column=1,sticky='snew',ipadx=40,ipady=40)
bu2.config(command=lambda: ButtonClick(2))
bu3=ttk.Button(root,text=' ')
bu3.grid(row=0,column=2,sticky='snew',ipadx=40,ipady=40)
bu3.config(command=lambda: ButtonClick(3))
bu4=ttk.Button(root,text=' ')
bu4.grid(row=1,column=0,sticky='snew',ipadx=40,ipady=40)
bu4.config(command=lambda: ButtonClick(4))
bu5=ttk.Button(root,text=' ')
bu5.grid(row=1,column=1,sticky='snew',ipadx=40,ipady=40)
bu5.config(command=lambda: ButtonClick(5))
bu6=ttk.Button(root,text=' ')
bu6.grid(row=1,column=2,sticky='snew',ipadx=40,ipady=40)
bu6.config(command=lambda: ButtonClick(6))
bu7=ttk.Button(root,text=' ')
bu7.grid(row=2,column=0,sticky='snew',ipadx=40,ipady=40)
bu7.config(command=lambda: ButtonClick(7))
bu8=ttk.Button(root,text=' ')
bu8.grid(row=2,column=1,sticky='snew',ipadx=40,ipady=40)
bu8.config(command=lambda: ButtonClick(8))
bu9=ttk.Button(root,text=' ')
bu9.grid(row=2,column=2,sticky='snew',ipadx=40,ipady=40)
bu9.config(command=lambda: ButtonClick(9))
def ButtonClick(id):
#print("ID:{}".format(id))
global ActivePlayer
global p1
global p2
if(ActivePlayer==1):
SetLayout(id,"X")
p1.append(id)
root.title("Tic Tac Toe :Player 2")
ActivePlayer=2
print("P1:{}".format(p1))
AutoPlay()
elif(ActivePlayer==2):
SetLayout(id,"O")
p2.append(id)
root.title("Tic Tac Toe :Player 1")
ActivePlayer = 1
print("P2:{}".format(p2))
CheckWinner()
def SetLayout(id,PlayerSymbol):
if id==1:
bu1.config(text=PlayerSymbol)
bu1.state(['disabled'])
elif id==2:
bu2.config(text=PlayerSymbol)
bu2.state(['disabled'])
elif id == 3:
bu3.config(text=PlayerSymbol)
bu3.state(['disabled'])
elif id == 4:
bu4.config(text=PlayerSymbol)
bu4.state(['disabled'])
elif id == 5:
bu5.config(text=PlayerSymbol)
bu5.state(['disabled'])
elif id == 6:
bu6.config(text=PlayerSymbol)
bu6.state(['disabled'])
elif id == 7:
bu7.config(text=PlayerSymbol)
bu7.state(['disabled'])
elif id == 8:
bu8.config(text=PlayerSymbol)
bu8.state(['disabled'])
elif id == 9:
bu9.config(text=PlayerSymbol)
bu9.state(['disabled'])
def CheckWinner():
Winner=-1
if((1 in p1) and (2 in p1) and (3 in p1)):
Winner=1
if ((1 in p2) and (2 in p2) and (3 in p2)):
Winner=2
if ((4 in p1) and (5 in p1) and (6 in p1)):
Winner = 1
if ((4 in p2) and (5 in p2) and (6 in p2)):
Winner = 2
if ((7 in p1) and (8 in p1) and (9 in p1)):
Winner = 1
if ((7 in p2) and (8 in p2) and (9 in p2)):
Winner = 2
if ((1 in p1) and (4 in p1) and (7 in p1)):
Winner = 1
if ((1 in p2) and (4 in p2) and (7 in p2)):
Winner = 2
if ((2 in p1) and (5 in p1) and (8 in p1)):
Winner = 1
if ((2 in p2) and (5 in p2) and (8 in p2)):
Winner = 2
if ((3 in p1) and (6 in p1) and (9 in p1)):
Winner = 1
if ((3 in p2) and (6 in p2) and (9 in p2)):
Winner = 2
if ((1 in p1) and (5 in p1) and (9 in p1)):
Winner = 1
if ((1 in p2) and (5 in p2) and (9 in p2)):
Winner = 2
if ((3 in p1) and (5 in p1) and (7 in p1)):
Winner = 1
if ((3 in p2) and (5 in p2) and (7 in p2)):
Winner = 2
if Winner==1:
messagebox.showinfo(title="Congrats.",message="Player 1 is the winner")
exit()
elif Winner == 2:
messagebox.showinfo(title="Congrats.", message="Player 2 is the winner")
exit()
def AutoPlay():
EmptyCells=[]
for cell in range(9):
if(not(cell+1 in p1) or (cell+1 in p2)):
EmptyCells.append(cell+1)
RandIndex=randint(0,len(EmptyCells)-1)
ButtonClick(EmptyCells[RandIndex])
root.mainloop()