forked from rrmckeever0319/Python-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTictactoe.py
More file actions
340 lines (256 loc) · 10.3 KB
/
Copy pathTictactoe.py
File metadata and controls
340 lines (256 loc) · 10.3 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from Tkinter import *
master = Tk()
gameboard = Canvas(master, width=300, height=300) #Window size
gameboard.pack()
gameboard.create_line(100, 0, 100, 300) #Tic tac to board
gameboard.create_line(200, 0, 200, 300)
gameboard.create_line(0, 100, 300, 100)
gameboard.create_line(0, 200, 300, 200)
# variable for determining turn
playerTurn = (2)
square1 = (0)
square2 = (0)
square3 = (0)
square4 = (0)
square5 = (0)
square6 = (0)
square7 = (0)
square8 = (0)
square9 = (0)
#For determining a win for X
Xa = (0)
Xb = (0)
Xc = (0)
Xa1 = (0)
Xb1 = (0)
Xc1 = (0)
Xa2 = (0)
Xb2 = (0)
Oa = (0)
Ob = (0)
Oc = (0)
Oa1 = (0)
Ob1 = (0)
Oc1 = (0)
Oa2 = (0)
Ob2 = (0)
def oxMoves (playerMove):
global playerTurn
global square1 #for not allowing double placements
global square2
global square3
global square4
global square5
global square6
global square7
global square8
global square9
#for determining a win for X's
global Xa
global Xb
global Xc
global Xa1
global Xb1
global Xc1
global Xa2
global Xb2
#for determining a win for O's
global Oa # for a win straight across the top
global Ob #for a win straight across the midlle
global Oc #for a win straight across the bottom
global Oa1 #for a win straight down the left
global Ob1 #for a win straight down the middle
global Oc1 #for a win straight down the right
global Oa2 #for a win diagonally from the upper left to the bottom right
global Ob2 #for a win diagonally from the bottom left to the upper right
#while loop for O's
while (playerTurn == 0):
playerTurn = (playerTurn + 1)
#while loop for X's
while (playerTurn == 3):
playerTurn = (playerTurn - 1)
#OMoves
#O position left 1
if playerTurn == (1) and playerMove.x < 100 and playerMove.y < 100 and square1 == (0) :
gameboard.create_oval(5, 5, 95, 95, outline="Blue")
playerTurn = (playerTurn + 2)
square1 = (square1 + 1)
Oa = (Oa + 1)
Oa1 = (Oa1 + 1)
Oa2 = (Oa2 + 1)
#O position left 2
elif playerTurn == (1) and playerMove.x < 100 and playerMove.y > 100 and playerMove.y < 200 and square2 == (0):
gameboard.create_oval(5, 105, 95, 195, outline="Blue")
playerTurn = (playerTurn + 2)
square2 = (square2 + 1)
Oa = (Oa + 1)
Ob1 = (Ob1 + 1)
#O position left 3
elif playerTurn == (1) and playerMove.x < 100 and playerMove.y > 200 and playerMove.y < 300 and square3 == (0):
gameboard.create_oval(5, 205, 95, 295, outline="Blue")
playerTurn = (playerTurn + 2)
square3 = (square3 + 1)
Oa = (Oa + 1)
Oc1 = (Oc1 + 1)
Ob2 = (Ob2 + 1)
#O position middle 1
elif playerTurn == (1) and playerMove.x > 100 and playerMove.x < 200 and playerMove.y < 100 and square4 == (0):
gameboard.create_oval(105, 5, 195, 95, outline="Blue")
playerTurn = (playerTurn + 2)
square4 = (square4 + 1)
Ob = (Ob + 1)
Oa1 = (Oa1 + 1)
#O position middle 2
elif playerTurn == (1) and playerMove.x > 100 and playerMove.x < 200 and playerMove.y > 100 and playerMove.y < 200 and square5 == (0):
gameboard.create_oval(105, 105, 195, 195, outline="Blue")
playerTurn = (playerTurn + 2)
square5 = (square5 + 1)
Ob = (Ob + 1)
Ob1 = (Ob1 + 1)
Oa2 = (Oa2 + 1)
Ob2 = (Ob2 + 1)
#O position middle 3
elif playerTurn == (1) and playerMove.x > 100 and playerMove.x < 200 and playerMove.y > 200 and playerMove.y < 300 and square6 == (0) :
gameboard.create_oval(105, 205, 195, 295, outline="Blue")
playerTurn = (playerTurn + 2)
square6 = (square6 + 1)
Ob = (Ob + 1)
Oc1 = (Oc1 + 1)
#O position right 1
elif playerTurn == (1) and playerMove.x > 200 and playerMove.x < 300 and playerMove.y < 100 and playerMove.y > 0 and square7 == (0):
gameboard.create_oval(205, 5, 295, 95, outline="Blue")
playerTurn = (playerTurn + 2)
square7 = (square7 + 1)
Oc = (Oc + 1)
Oa1 = (Oa1 + 1)
Ob2 = (Ob2 + 1)
#O position right 2
elif playerTurn == (1) and playerMove.x > 200 and playerMove.x < 300 and playerMove.y < 200 and playerMove.y > 100 and square8 == (0):
gameboard.create_oval(205, 105, 295, 195, outline="Blue")
playerTurn = (playerTurn + 2)
square8 = (square8 + 1)
Oc = (Oc + 1)
Ob1 = (Ob1 + 1)
#O position right 3
elif playerTurn == (1) and playerMove.x > 200 and playerMove.x < 300 and playerMove.y < 300 and playerMove.y > 200 and square9 == (0):
gameboard.create_oval(205, 205, 295, 295,outline="Blue")
playerTurn = (playerTurn + 2)
square9 = (square9 + 1)
Oc = (Oc + 1)
Oc1 = (Oc1 + 1)
Oa2 = (Oa2 + 1)
# Xmoves
#X position left 1
elif playerTurn == (2) and playerMove.x < 100 and playerMove.y < 100 and square1 == (0):
gameboard.create_line(100, 0, 0, 100, fill="Red")
gameboard.create_line(0, 0, 100, 100, fill="Red")
playerTurn = (playerTurn - 1)
square1 = (square1 + 1)
Xa = (Xa + 1)
Xa1 = (Xa1 + 1)
Xa2 = (Xa2 + 1)
#X position left 2
elif playerTurn == (2) and playerMove.x < 100 and playerMove.y > 100 and playerMove.y < 200 and square2 == (0):
playerTurn = (playerTurn - 1)
gameboard.create_line(100, 100, 0, 200, fill="Red")
gameboard.create_line(0, 100, 100, 200, fill="Red")
square2 = (square2 + 1)
Xa = (Xa + 1)
Xb1 = (Xb1 + 1)
#X position left 3
elif playerTurn == (2) and playerMove.x < 100 and playerMove.y > 200 and playerMove.y < 300 and square3 == (0):
playerTurn = (playerTurn - 1)
gameboard.create_line(100, 200, 0, 300, fill="Red")
gameboard.create_line(0, 200, 100, 300, fill="Red")
square3 = (square3 + 1)
Xa = (Xa + 1)
Xc1 = (Xc1 + 1)
Xb2 = (Xb2 + 1)
#X position middle 1
elif playerTurn == 2 and playerMove.x > 100 and playerMove.x < 200 and playerMove.y < 100 and square4 == (0):
playerTurn = (playerTurn - 1)
gameboard.create_line(100, 0, 200, 100, fill="Red")
gameboard.create_line(100, 100, 200, 0, fill="Red")
square4 = (square4 + 1)
Xb = (Xb + 1)
Xa1 = (Xa1 + 1)
#X position middle 2
elif playerTurn == 2 and playerMove.x > 100 and playerMove.x < 200 and playerMove.y > 100 and playerMove.y < 200 and square5 == (0):
gameboard.create_line(100, 100, 200, 200, fill="Red")
gameboard.create_line(100, 200, 200, 100, fill="Red")
playerTurn = (playerTurn - 1)
square5 = (square5 + 1)
Xb = (Xb + 1)
Xb1 = (Xb1 + 1)
Xa2 = (Xa2 + 1)
Xb2 = (Xb2 + 1)
#X position middle 3
elif playerTurn == 2 and playerMove.x > 100 and playerMove.x < 200 and playerMove.y > 200 and playerMove.y < 300 and square6 == (0):
gameboard.create_line(100, 200, 200, 300, fill="Red")
gameboard.create_line(100, 300, 200, 200, fill="Red")
playerTurn = (playerTurn - 1)
square6 = (square6 + 1)
Xb = (Xb + 1)
Xc1 = (Xc1 + 1)
#X position right 1
elif playerTurn == 2 and playerMove.x > 200 and playerMove.x < 300 and playerMove.y < 100 and playerMove.y > 0 and square7 == (0):
gameboard.create_line(200, 0, 300, 100, fill="Red")
gameboard.create_line(200, 100, 300, 0, fill="Red")
playerTurn = (playerTurn - 1)
square7 = (square7 + 1)
Xc = (Xc + 1)
Xa1 = (Xa1 + 1)
Xb2 = (Xb2 + 1)
#X position right 2
elif playerTurn == 2 and playerMove.x > 200 and playerMove.x < 300 and playerMove.y < 200 and playerMove.y > 100 and square8 == (0):
gameboard.create_line(200, 100, 300, 200, fill="Red")
gameboard.create_line(200, 200, 300, 100, fill="Red")
playerTurn = (playerTurn - 1)
square8 = (square8 + 1)
Xc = (Xc + 1)
Xb1 = (Xb1 + 1)
#X position right 3
elif playerTurn == 2 and playerMove.x > 200 and playerMove.x < 300 and playerMove.y < 300 and playerMove.y > 200 and square9 == (0):
gameboard.create_line(200, 200, 300, 300, fill="Red")
gameboard.create_line(200, 300, 300, 200, fill="Red")
playerTurn = (playerTurn - 1)
square9 = (square9 + 1)
Xc = (Xc + 1)
Xc1 = (Xc1 + 1)
Xa2 = (Xa2 + 1)
#Printing the TicTacToe line if you get three in a row with X's
if Xa == 3:
gameboard.create_line(50, 0, 50, 300, width="2")
if Xb == 3:
gameboard.create_line(150, 0, 150, 300, width="2")
if Xc == 3:
gameboard.create_line(250, 0, 250, 300, width="2")
if Xa1 == 3:
gameboard.create_line(0, 50, 300, 50, width="2")
if Xb1 == 3:
gameboard.create_line(0, 150, 300, 150, width="2")
if Xc1 == 3:
gameboard.create_line(0, 250, 300, 250, width="2")
if Xa2 == 3:
gameboard.create_line(0, 0, 300, 300, width="2")
if Xb2 == 3:
gameboard.create_line(0, 300, 300, 0, width="2")
#Printing the TicTacToe line if you get three in a row with O's
if Oa == 3:
gameboard.create_line(50, 0, 50, 300, width="2")
if Ob == 3:
gameboard.create_line(150, 0, 150, 300, width="2")
if Oc == 3:
gameboard.create_line(250, 0, 250, 300, width="2")
if Oa1 == 3:
gameboard.create_line(0, 50, 300, 50, width="2")
if Ob1 == 3:
gameboard.create_line(0, 150, 300, 150, width="2")
if Oc1 == 3:
gameboard.create_line(0, 250, 300, 250, width="2")
if Oa2 == 3:
gameboard.create_line(0, 0, 300, 300, width="2")
if Ob2 == 3:
gameboard.create_line(0, 300, 300, 0, width="2")
gameboard.bind("<Button-1>", oxMoves)
mainloop()