-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectfour.py
307 lines (217 loc) · 8.72 KB
/
connectfour.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
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
import turtle
import time
class GameLogic:
def __init__(self):
self.logoTurtle = turtle.Turtle()
self.drawingTurtle = turtle.Turtle()
self.selectionCursor = turtle.Turtle()
self.columnnumSelection = 3
self.cursorY = 162.5
self.turnInProgress = False
self.logicInProgress = False
self.gameboard = None
# Loser goes first but black starts. Black is Even turn numbers.
self.playerTurn = 1
self.gameover = False
self.beginGame()
def beginGame(self):
# Start the game
self.gameover = False
self.playerTurn += 1
# Reset the turtles for drawing and pointing
self.logoTurtle.reset()
self.logoTurtle.hideturtle()
self.logoTurtle.color("black")
self.logoTurtle.penup()
self.logoTurtle.goto(0, 230)
self.drawingTurtle.reset()
self.drawingTurtle.hideturtle()
self.selectionCursor.reset()
self.selectionCursor.hideturtle()
self.selectionCursor.penup()
self.selectionCursor.pensize(30)
self.selectionCursor.seth(270)
# Create an empty board
self.gameboard = [[0 for i in range(7)] for j in range(6)]
# Draw the board and begin the first turn
self.drawBoard()
self.beginTurn()
def drawBoard(self):
# Draw Connect 4 at the top of the thing
self.logoTurtle.write("Connect 4!", align="center", move=False,
font=('Arial', 40, 'normal'))
# Draw outer box
self.drawingTurtle.speed(0)
self.drawingTurtle.penup()
self.drawingTurtle.goto(-262.5, 125)
self.drawingTurtle.pendown()
self.drawingTurtle.goto(262.5, 125)
self.drawingTurtle.goto(262.5, -175)
self.drawingTurtle.goto(-262.5, -175)
self.drawingTurtle.goto(-262.5, 125)
# Draw all empty circles
self.drawingTurtle.seth(180)
for rowNum in range(6):
for columnNum in range(7):
self.fillInSpace(rowNum, columnNum, self.gameboard[rowNum][columnNum])
def beginTurn(self):
self.columnnumSelection = 3
self.updateCursorPosition()
self.selectionCursor.color("black" if self.playerTurn % 2 == 0 else "red")
self.selectionCursor.showturtle()
def endTurn(self):
self.selectionCursor.hideturtle()
self.updateCursorPosition()
self.playerTurn += 1
def moveCursorLeft(self):
if self.logicInProgress:
return
self.logicInProgress = True
if self.columnnumSelection > 0:
self.columnnumSelection -= 1
self.updateCursorPosition()
self.logicInProgress = False
def moveCursorRight(self):
if self.logicInProgress:
return
self.logicInProgress = True
if self.columnnumSelection < 6:
self.columnnumSelection += 1
self.updateCursorPosition()
self.logicInProgress = False
def submitMove(self):
if self.logicInProgress:
return
self.logicInProgress = True
# Hide the cursor
self.selectionCursor.hideturtle()
# Update the self.gameboard with the newly dropped piece
try:
self.dropPiece()
except RuntimeError as err:
print(err)
self.selectionCursor.showturtle()
self.logicInProgress = False
return
# Stop here if the game is already over
if self.gameover:
# This allows for a fun overkill mode after winning where you place your pieces wherever
self.logicInProgress = False
self.selectionCursor.showturtle()
return
# check for wins
if self.checkForWins():
self.winSequence()
self.logicInProgress = False
return
# end the turn
self.endTurn()
# conditional start of a new turn
self.beginTurn()
self.logicInProgress = False
def dropPiece(self):
"""
Checks every row in the currently selected column bottom up to find an open spot to drop the piece.
-Finds the lowest spot to drop the game piece in the self.columnnumSelection column
-Updates self.gameboard with the correct color for that space
-Calls fillInSpace to draw it to the screen
:return: None
"""
playerColorNumber = 1 if self.playerTurn % 2 == 1 else 2
for rowNumber in range(5, -1, -1):
if self.gameboard[rowNumber][self.columnnumSelection] == 0:
self.gameboard[rowNumber][self.columnnumSelection] = playerColorNumber
self.fillInSpace(rowNumber, self.columnnumSelection, playerColorNumber)
return
raise RuntimeError("Column is full of pieces.")
def fillInSpace(self, row, column, colornumber):
if colornumber == 0:
self.drawingTurtle.pencolor("black")
self.drawingTurtle.fillcolor("white")
elif colornumber == 1:
self.drawingTurtle.color("red")
elif colornumber == 2:
self.drawingTurtle.color("black")
else:
raise ValueError(f"Color number {colornumber} is not 0, 1, or 2")
if row > 5:
raise ValueError(f"Row number {row} is too high. Must be < 5.")
if column > 6:
raise ValueError(f"Column number {column} is too high. Must be < 6.")
self.drawingTurtle.penup()
self.drawingTurtle.goto(-225 + column * 75, 110 + row * -50)
self.drawingTurtle.pendown()
self.drawingTurtle.begin_fill()
self.drawingTurtle.circle(10)
self.drawingTurtle.end_fill()
def updateCursorPosition(self):
self.selectionCursor.goto(-225 + 75 * self.columnnumSelection, self.cursorY)
def checkForWins(self):
for rownumber in range(6):
for columnnumber in range(7):
hwin = self.checkHorizontalWin(rownumber, columnnumber)
vwin = self.checkVerticalWin(rownumber, columnnumber)
drwin = self.checkRightDiagonalWin(rownumber, columnnumber)
dlwin = self.checkLeftDiagonalWin(rownumber, columnnumber)
if hwin or vwin or drwin or dlwin:
return True
return False
def checkHorizontalWin(self, startingrow, startingcolumn):
if startingcolumn > 3:
return False
currentPlayerNumber = 2 if self.playerTurn % 2 == 0 else 1
for i in range(4):
if self.gameboard[startingrow][startingcolumn + i] != currentPlayerNumber:
return False
return True
def checkVerticalWin(self, startingrow, startingcolumn):
if startingrow > 2:
return False
currentPlayerNumber = 2 if self.playerTurn % 2 == 0 else 1
for i in range(4):
if self.gameboard[startingrow + i][startingcolumn] != currentPlayerNumber:
return False
return True
def checkRightDiagonalWin(self, startingrow, startingcolumn):
if startingrow > 2 or startingcolumn > 3:
return False
currentPlayerNumber = 2 if self.playerTurn % 2 == 0 else 1
for i in range(4):
if self.gameboard[startingrow + i][startingcolumn + i] != currentPlayerNumber:
return False
return True
def checkLeftDiagonalWin(self, startingrow, startingcolumn):
if startingrow > 2 or startingcolumn < 3:
return False
currentPlayerNumber = 2 if self.playerTurn % 2 == 0 else 1
for i in range(4):
if self.gameboard[startingrow + i][startingcolumn - i] != currentPlayerNumber:
return False
return True
def winSequence(self):
winningPlayerColor = "black" if self.playerTurn % 2 == 0 else "red"
self.logoTurtle.clear()
self.logoTurtle.color(winningPlayerColor)
self.logoTurtle.write(f"{winningPlayerColor.upper()} Wins!", align="center", move=False, font=('Arial', 30, 'bold'))
self.selectionCursor.showturtle()
self.gameover = True
def startNewGame(self):
if self.logicInProgress:
return
self.logicInProgress = True
if self.gameover:
self.beginGame()
self.logicInProgress = False
def main():
# Initlize the game screen
screen = turtle.Screen()
screen.title("Can you Connect 4?")
game = GameLogic()
turtle.onkeypress(game.moveCursorLeft, "Left")
turtle.onkeypress(game.moveCursorRight, "Right")
turtle.onkeypress(game.submitMove, "Down")
turtle.onkeypress(game.startNewGame, "space")
turtle.listen()
screen.mainloop()
if __name__ == "__main__":
main()