-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
266 lines (225 loc) · 6.76 KB
/
__main__.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
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
from turtle import Turtle, Screen
import time
baseX = -75
baseY = -75
gridlength = 50
boardState = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
buttonStr = ["Play Again", "Exit"]
buttonLoc = []
al = Turtle()
al.shape("turtle")
screen = Screen()
al.speed(7)
playerOneTurn = True
gameOver = False
errorText = ""
def createBoard():
"""Establish the backend and frontend boards"""
global boardState
global playerOneTurn
global gameOver
boardState = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
playerOneTurn = True
gameOver = False
for x in range(3):
for y in range(3):
__createBox(x, y)
__createLabels()
__createButtons()
whoseTurn()
def __createBox(col, row):
"""Creating a box at (col, row) location"""
x = baseX + (col * 50)
y = baseY + (row * 50)
al.up()
al.goto(x, y)
al.down()
al.seth(90)
for _ in range(4):
al.forward(gridlength)
al.right(90)
al.up()
al.goto(x + (gridlength / 2), y + (gridlength / 2.5))
al.down()
al.write(str((col + 1) + (row * 3)))
al.up()
def __createLabels():
"""Creates the labels for whose turn it is"""
al.up()
al.goto(0, baseY - (gridlength * 2))
al.write("Whose Turn Is It?", False, align="center")
al.goto(0, baseY - (gridlength * 2.25))
al.down()
al.seth(270)
al.forward(gridlength * 2)
al.up()
al.goto(-1 * gridlength, baseY - (gridlength * 3.5))
al.write("X")
al.up()
al.goto(gridlength, baseY - (gridlength * 3.5))
al.write("O")
al.up()
al.goto(0, baseY - (gridlength * 3.75))
def __createButtons():
"""Creates the play again and quit buttons"""
al.fillcolor("black")
al.pencolor("white")
al.up()
al.goto(baseX - gridlength, baseY - gridlength * 5)
for i in range(len(buttonStr)):
__drawRectangle(buttonStr[i])
al.up()
al.setx(al.xcor() + gridlength * 3)
buttonLoc.append((al.xcor(), al.ycor()))
al.color("black")
def __drawRectangle(text):
"""Draw a rectangle with 'text' inside"""
al.seth(0)
al.down()
al.begin_fill()
for _ in range(2):
al.forward(gridlength * 2)
al.right(90)
al.forward(gridlength)
al.right(90)
al.end_fill()
al.up()
currX = al.xcor()
currY = al.ycor()
al.goto(currX + gridlength, currY - gridlength * .6)
al.write(text, False, "center")
al.goto(currX, currY)
def checkClick(x, y):
"""Determine whether the click is inside the board"""
print(f"Click registered at {x}, {y}")
if not gameOver and ((x >= baseX and x <= baseX + (3 * gridlength)) and
(y >= baseY and y <= baseY + (3 * gridlength))):
block = __determineBlock(x, y)
takeTurn(block[0], block[1])
elif y >= baseY - (gridlength * 6) and y <= baseY - (gridlength * 5) and (
(x >= baseX - gridlength) and (x <= baseX + gridlength)):
al.clear()
createBoard()
elif (x >= baseX + (gridlength * 2)) and x <= baseX + (gridlength * 4):
al.up()
al.goto(0, gridlength * 3)
al.down()
al.write("Thanks for playing", align="center")
al.up()
al.sety(al.ycor() - gridlength // 4)
time.sleep(1)
exit()
def __determineBlock(x, y):
"""Find out which block was clicked at (x,y) position"""
row = -1
col = -1
for i in range(len(boardState)):
if y >= (baseY
+ (i * gridlength)) and y <= baseY + ((i + 1) * gridlength):
row = i
break
for j in range(len(boardState[0])):
if x >= (baseX
+ (j * gridlength)) and x <= baseX + ((j + 1) * gridlength):
col = j
break
print(f"Col: {col}, Row: {row}")
return (col, row)
def takeTurn(x, y):
"""Current player takes their turn if the click is valid"""
if checkSpot(x, y):
validMove(x, y)
def checkSpot(x, y):
"""Determine if the square has been moved in"""
return boardState[y][x] == 0
def validMove(x, y):
"""Make a move on the expected square"""
global gameOver
global playerOneTurn
boardState[y][x] = 1 if playerOneTurn else -1
addMarker(x, y)
if checkWin(x, y):
turn = 1 if playerOneTurn else 2
__writeWinner(turn)
gameOver = True
elif tieChecker():
__writeWinner(-1)
gameOver = True
else:
playerOneTurn = not playerOneTurn
print(boardState)
whoseTurn()
def checkWin(x, y):
"""Check if the turn player has won"""
turn = 1 if playerOneTurn else -1
horiz = __checkRow(y, turn)
vert = __checkCol(x, turn)
if not((x == 1 and y != 1) or (x != 1 and y == 1)):
# Only check diagonals if the space is not on a middle edge
diag_down = __checkDiagDown(turn)
diag_up = __checkDiagUp(turn)
else:
diag_down, diag_up = False, False
return horiz or vert or diag_down or diag_up
def __checkRow(y, turn):
"""Determines if a row is full for winning"""
for i in range(len(boardState)):
if boardState[y][i] != turn:
return False
return True
def __checkCol(x, turn):
"""Determines if a column is full for winning"""
for i in range(len(boardState)):
if boardState[i][x] != turn:
return False
return True
def __checkDiagUp(turn):
"""Determines if the upward diagonal is full for winning"""
i = 0
for i, row in enumerate(boardState):
if row[-(i+1)] != turn:
return False
return True
def __checkDiagDown(turn):
"""Determines if the downward diagonal is full for winning"""
for i, row in enumerate(boardState):
if row[i] != turn:
return False
return True
def __writeWinner(turn):
"""The turtle writes who won or tied"""
whoseTurn()
if turn == -1:
al.setx(0)
al.write("It's a tie!", align="center")
else:
al.sety(al.ycor() + gridlength / 3)
al.write("Winner!", align="center")
def tieChecker():
"""Check if all squares have been filled"""
for y in range(len(boardState)):
for x in range(len(boardState[0])):
if boardState[y][x] == 0:
return False
return True
def addMarker(x, y):
"""Visually put a marker on the moved-to square"""
midX = (baseX + (.5 * gridlength)) + (x * gridlength)
locY = (baseY + (y * gridlength))
al.up()
al.goto(midX, locY)
if playerOneTurn:
al.down()
al.circle(gridlength * .5)
else:
al.sety(al.ycor() + (gridlength * .5))
al.down()
al.stamp()
al.up()
def whoseTurn():
"""Move the turtle to the correct side in the turn marker"""
al.goto(gridlength * (-1 if playerOneTurn else 1),
baseY - (gridlength * 2.75))
createBoard()
screen.onclick(checkClick)
screen.mainloop()