-
Notifications
You must be signed in to change notification settings - Fork 2
/
Master.py
313 lines (241 loc) · 9.4 KB
/
Master.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
307
308
309
310
311
312
313
from Board import Board
from Display import Display
from AIs.Vaapad import Vaapad
from AIs.Wildfire import Wildfire
from AIs.Human import Human
from AIs.Brute import Brute
import pygame
class Master:
""" controls everything going on """
# constructor
def __init__(self):
""" Creates display and initial menu """
self.b = Board()
self.d = None
self.n = 0
self.forced = False
self.wins = [0, 0]
self.AIList = [None, Vaapad, Human, Wildfire, Brute]
def main(self):
""" Main controller of everything, likeyado """
displayOn = True
self.d = Display(self.b, self.AIList, [None, None, None])
while displayOn:
players = self.d.mainMenu()
currentSet = True
while currentSet:
self.playGame(players)
inExit = True
while inExit:
choice = self.d.playAgain()
if choice == "Play Again":
inExit = False
# tally up who won and display that somehow
elif choice == "Switch Players":
inExit = False
currentSet = False
elif choice == "View Replay":
self.viewReplay(players)
elif choice == "Quit":
inExit, currentSet, displayOn = (False,)*3
pygame.quit()
quit(0)
def presetBoard(self, players):
self.b = Board()
self.d = Display(self.b, self.AIList, players)
self.d.initializeBoard()
continueGame = True
self.forced = False
self.n = 1
while (continueGame) and self.d.preset:
titleText = "Player " + str(self.n) + "'s Turn"
self.d.changeTitleText(titleText)
self.d.displayProgress("", 0)
if self.forced:
self.d.checkPoint(self.b.otherNumber(self.n))
self.d.updateBoard(self.b)
for i in range(10):
self.d.displayBoard()
pygame.time.wait(10)
if not self.d.preset:
break
nextMove = players[0].move(self.b, self.n, self.d)
if not self.d.preset:
break
noProblem = self.b.move(self.n, nextMove)
if not noProblem:
print "move", nextMove, "failed for player", self.n
break
continueGame = self.checkBoard(nextMove)
self.n = self.b.otherNumber(self.n)
def playGame(self, players):
"""
starts game between players 1 and 2
players holds both players, whether AI or real
player 1 is X's, player 2 is O's
"""
self.b = Board()
self.d = Display(self.b, self.AIList, players)
self.d.initializeBoard()
continueGame = True
self.forced = False
self.n = 1
if players[0]:
self.presetBoard(players)
while (continueGame):
titleText = "Player " + str(self.n) + "'s Turn"
self.d.changeTitleText(titleText)
self.d.displayProgress("", 0)
if self.forced:
self.d.checkPoint(self.b.otherNumber(self.n))
self.d.updateBoard(self.b)
i = 0
while i < 10:
self.d.displayBoard()
pygame.time.wait(10)
i += 1 # WHOOOPS FORGOT THIS
nextMove = players[self.n].move(self.b, self.n, self.d)
noProblem = self.b.move(self.n, nextMove)
if not noProblem:
print "move", nextMove, "failed for player", self.n
break
continueGame = self.checkBoard(nextMove)
self.n = self.b.otherNumber(self.n)
def viewReplay(self, players):
"""
starts game between players 1 and 2
players holds both players, whether AI or real
player 1 is X's, player 2 is O's
"""
self.fakeB = Board()
self.fakeB.copyAll(self.b)
moveList = self.b.moveList
self.d = Display(self.fakeB, self.AIList, players)
self.d.initializeBoard()
self.d.displayProgress("", 0)
self.forced = False
self.n = 1
moveNumber = len(moveList) - 1
self.checkBoardForReplay(moveList[moveNumber][0])
while (moveNumber < len(moveList)) and moveNumber >= -1:
self.d.displayReplayBoard()
pygame.time.wait(10)
direction = self.d.checkReplayControl()
if direction != 0:
currentMove = None
if direction == 1:
moveNumber += 1
if moveNumber < len(moveList):
currentMove = moveList[moveNumber][0]
self.n = moveList[moveNumber][1]
self.fakeB.move(self.n, currentMove)
elif direction == -1:
if moveNumber >= 0:
currentMove = moveList[moveNumber][0]
self.n = self.b.otherNumber(moveList[moveNumber][1])
self.fakeB.clearPoint(currentMove)
moveNumber -= 1
self.d.updateBoard(self.fakeB)
if moveNumber in range(len(moveList)):
self.checkBoardForReplay(currentMove)
def checkBoardForReplay(self, move):
""" check board for wins and checks after a replayed move """
numMoves = 64 - len(self.fakeB.openPoints())
wins = self.fakeB.openLinesForPoint(self.n, move, 4)
checkMate = self.checkCheckmates(True)
checks = self.fakeB.findLines(self.n, 3)
if len(wins) > 0:
self.forced = False
self.d.changeTitleText("Player " + str(self.n) +
" Wins! They got 4 in a row!")
self.d.setWinningMove(move)
self.d.setFlashLines(wins)
elif checkMate:
self.forced = False
self.d.changeTitleText("Player " + str(self.n) +
" Wins! They got checkmate!")
self.d.setWinningMove(move)
else:
self.forced = False
self.d.setWinningMove([])
self.d.changeTitleText("Player " + str(self.b.otherNumber(self.n))
+ "'s Turn")
if len(checks) > 0:
self.d.setFlashLines(checks)
self.forced = True
self.d.checkPoint(self.n)
if numMoves == 64:
self.d.changeTitleText("Wow! You filled the board!")
def checkBoard(self, move):
""" check board for wins and checks after a move """
continueGame = True # can be assumed given that we got here
numMoves = 64 - len(self.b.openPoints())
wins = self.b.openLinesForPoint(self.n, move, 4)
checkMate = self.checkCheckmates(False)
checks = self.b.findLines(self.n, 3)
if len(wins) > 0:
continueGame = False
self.forced = False
self.d.displayProgress("", 0)
self.d.changeTitleText("Player " + str(self.n) +
" Wins! They got 4 in a row!")
self.d.setWinningMove(move)
self.d.setFlashLines(wins)
self.d.updateBoard(self.b)
i = 0
while i < 17+36:
self.d.displayBoard()
pygame.time.wait(10)
i += 1 # WHOOOPS FORGOT THIS
elif checkMate:
self.forced = False
continueGame = False
self.d.displayProgress("", 0)
self.d.changeTitleText("Player " + str(self.n) +
" Wins! They got checkmate!")
self.d.setWinningMove(move)
self.d.updateBoard(self.b)
i = 0
while i < 17+36:
self.d.displayBoard()
pygame.time.wait(10)
i += 1 # WHOOOPS FORGOT THIS
elif len(checks) > 0:
self.d.setFlashLines(checks)
self.d.checkPoint(self.n)
self.forced = True
else:
self.forced = False
self.d.setFlashLines([])
if numMoves == 64:
continueGame = False
self.d.displayProgress("", 0)
self.d.changeTitleText("Wow! You filled the board!")
self.d.updateBoard(self.b)
i = 0
while i < 17+36:
self.d.displayBoard()
pygame.time.wait(10)
i += 1 # WHOOOPS FORGOT THIS
return continueGame
def checkCheckmates(self, fake):
""" check board for checkmates after a move """
checkMate = False
checkLines = []
board = self.b
if fake:
board = self.fakeB
points = board.myPoints(self.n)
for p in points:
checks = board.openLinesForPoint(self.n, p, 3)
if len(checks) > 1:
checkMate = True
checkLines += checks
self.d.setFlashLines(checkLines)
return checkMate
tryTo = Master()
tryTo.main()
# tryTo.d = Display(tryTo.b)
# player1 = Wildfire()
# player2 = Wildfire()
# tryTo.playGame([None,player1,player2],1)