-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
444 lines (361 loc) · 16.6 KB
/
board.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import Tkinter, time, copy, datetime
import constants
class Board:
def __init__(self):
self.moveXY = None
self.moveMade = False
self.curPlayer = 'W'
self.dirs = copy.copy(constants.DIRECTIONS)
self.board = [None] * constants.BRD_SIZE
for i in xrange(constants.BRD_SIZE):
self.board[i] = ['G'] * constants.BRD_SIZE
b = int(constants.BRD_SIZE / 2)
a = b-1
self.board[a][a] = 'W'
self.board[b][b] = 'W'
self.board[a][b] = 'B'
self.board[b][a] = 'B'
self.flipBoxes = []
self.createGUI()
self.on = True
def __str__(self):
sstr = ''
for i in xrange(constants.BRD_SIZE):
for j in xrange(constants.BRD_SIZE):
if self.board[i][j] == 'W': sstr += 'W '
elif self.board[i][j] == 'B': sstr += 'B '
elif self.board[i][j] == 'G': sstr += '_ '
else: assert False, 'board is not W, B or G'
sstr += "\n"
sstr += self.curPlayer + '\n'
return sstr
def curPlayerColorStr(self):
if self.curPlayer == 'W': return 'White'
if self.curPlayer == 'B': return 'Black'
return 'Error: no curPlayer'
################################### GUI ####################################
def createGUI(self):
self.guiOn = True
self.guiTk = Tkinter.Tk()
self.guiCanvas = Tkinter.Canvas(self.guiTk, bg=constants.BRD_COLOR, height=constants.BRD_HW, width=constants.BRD_HW)
self.guiMsg = Tkinter.StringVar()
self.curBox = self.guiCanvas.create_rectangle(0, 0,
constants.CELL_HW*constants.BRD_SIZE,
constants.CELL_HW*constants.BRD_SIZE,
outline=constants.CURBOX_COLOR,
width=constants.CURBOX_BORDER)
Tkinter.Label(self.guiTk, textvariable=self.guiMsg, font=(constants.FONT_TYPE, constants.FONT_SIZE)).pack()
self.guiTk.protocol('WM_DELETE_WINDOW', self.guiExit) #callback to exit game
self.guiCanvas.bind('<Button-1>', self.guiClick) #callback for mouse click
self.guiBoard = [None] * constants.BRD_SIZE
for i in range(constants.BRD_SIZE):
self.guiBoard[i] = [None] * constants.BRD_SIZE
for i in xrange(1,constants.BRD_SIZE):
self.guiCanvas.create_line(0, i*constants.CELL_HW, constants.BRD_HW, i*constants.CELL_HW) #draw horizontal line
self.guiCanvas.create_line(i*constants.CELL_HW, 0, i*constants.CELL_HW, constants.BRD_HW) #draw vertical line
self.guiCanvas.pack()
self.guiCanvas.focus_set()
self.guiTk.update()
def guiExit(self):
self.on = False
self.guiTk.destroy()
def guiClick(self, event):
self.moveXY = (event.y/constants.CELL_HW, event.x/constants.CELL_HW)
self.moveMade = True
def guiDrawBoard(self, curMove, flips):
for i in range(constants.BRD_SIZE):
for j in range(constants.BRD_SIZE):
color = self.board[i][j]
if color == 'B': cellColor = 'black'
elif color == 'W': cellColor = 'white'
else:
self.guiBoard[i][j] = None
continue
if self.guiBoard[i][j] == None:
self.guiBoard[i][j] = self.guiMakePiece(i,j,cellColor)
else:
self.guiCanvas.itemconfig(self.guiBoard[i][j],fill=cellColor)
if curMove:
self.guiCanvas.coords(self.curBox,
curMove[1]*constants.CELL_HW+1,
curMove[0]*constants.CELL_HW+1,
(curMove[1]+1)*constants.CELL_HW-1,
(curMove[0]+1)*constants.CELL_HW-1)
else:
self.guiCanvas.coords(self.curBox,0,0,0,0)
if flips:
for box in self.flipBoxes:
self.guiCanvas.delete(box)
self.flipBoxes = []
for flip in flips:
box = self.guiCanvas.create_rectangle(0, 0, 0, 0,
outline=constants.FLIP_COLOR,
width=constants.FLIP_BORDER)
self.guiCanvas.coords(box,
flip[1]*constants.CELL_HW+1,
flip[0]*constants.CELL_HW+1,
(flip[1]+1)*constants.CELL_HW-1,
(flip[0]+1)*constants.CELL_HW-1)
self.flipBoxes.append(box)
def guiMakePiece(self, i, j, color):
return self.guiCanvas.create_oval( j*constants.CELL_HW+2, i*constants.CELL_HW+2,
(j+1)*constants.CELL_HW-2, (i+1)*constants.CELL_HW-2,
fill=color)
############################### GAME LOGIC #################################
@staticmethod
def addToPos(pos,ddir):
return (pos[0]+ddir[0], pos[1]+ddir[1])
@staticmethod
def validPos(pos):
return pos[0] >= 0 and pos[0] < constants.BRD_SIZE and \
pos[1] >= 0 and pos[1] < constants.BRD_SIZE
@staticmethod
def oppositeColor(color):
if color == 'W': return 'B'
if color == 'B': return 'W'
assert False, 'Color is neither W or B'
@staticmethod
def getTime():
return time.time() #use this for wall-clock time across OSes
#return time.clock()
def computeScore(self):
w = b = 0
for i in range(constants.BRD_SIZE):
for j in range(constants.BRD_SIZE):
color = self.board[i][j]
if color == 'W': w += 1
elif color == 'B': b += 1
return w, b
def hasMove(self, color):
oppColor = Board.oppositeColor(color)
moves = self.findAllMovesHelper(color, oppColor, checkHasMoveOnly=True)
return len(moves) > 0
def isEndGame(self):
for color, oppColor in (('B','W'), ('W','B')):
moves = self.findAllMovesHelper(color, oppColor, checkHasMoveOnly=True)
if len(moves) > 0: return False
return True
def findAllMoves(self):
color = self.curPlayer
oppColor = Board.oppositeColor(color)
return self.findAllMovesHelper(color, oppColor)
def findAllMovesHelper(self, color, oppColor, checkHasMoveOnly=False):
moves = []
for i in xrange(constants.BRD_SIZE):
for j in xrange(constants.BRD_SIZE):
if self.board[i][j] != 'G': continue
for ddir in self.dirs:
if self.validMove((i,j), ddir, color, oppColor):
moves.append((i,j))
if checkHasMoveOnly: return moves
break
return moves
def makeMove(self, pos):
flips = []
if pos == None: return flips
color = self.curPlayer
self.board[pos[0]][pos[1]] = color
oppColor = Board.oppositeColor(color)
for ddir in self.dirs:
if self.validMove(pos, ddir, color, oppColor):
newPos = Board.addToPos(pos, ddir)
while self.board[newPos[0]][newPos[1]] == oppColor:
self.board[newPos[0]][newPos[1]] = color
flips.append(newPos)
newPos = Board.addToPos(newPos, ddir)
return flips
def validMove(self, pos, ddir, color, oppColor):
newPos = Board.addToPos(pos, ddir)
if not Board.validPos(newPos): return False
if self.board[newPos[0]][newPos[1]] != oppColor: return False
while self.board[newPos[0]][newPos[1]] == oppColor:
newPos = Board.addToPos(newPos, ddir)
if not Board.validPos(newPos): break
if Board.validPos(newPos) and self.board[newPos[0]][newPos[1]] == color:
return True
return False
def validMoveInSomeDir(self, pos, color, oppColor):
for ddir in self.dirs:
if self.validMove(pos, ddir, color, oppColor):
return True
return False
def playGame(self, white, black):
whiteSec = 0.0
blackSec = 0.0
whiteMoves = 0
blackMoves = 0
prevMove = None
numNoneMoves = 0
self.curPlayer = 'W'
message = ''
timeExceeded = False
memExceeded = False
log = open(datetime.datetime.now().strftime("gamelog-%Y-%m-%d_%H-%M-%S.txt"), 'w')
self.guiDrawBoard(None, None)
self.guiTk.configure(cursor='watch')
self.guiTk.update()
while not self.isEndGame():
boardCopy = copy.deepcopy(self.board)
timeExceeded = False
memExceeded = False
if self.curPlayer == 'W': player , timeSec = white, whiteSec
else: player , timeSec = black, blackSec
memUsedMB = player.getMemoryUsedMB()
if timeSec > constants.TIME_LIMIT_SEC: timeExceeded = True
if memUsedMB > constants.MEMORY_LIMIT_MB: memExceeded = True
diffSec = 0.0
if not timeExceeded and not memExceeded and self.hasMove(player.getColor()):
startSec = Board.getTime()
move = player.chooseMove(boardCopy, prevMove)
diffSec = Board.getTime() - startSec
else:
move = None
if move == None:
numNoneMoves += 1
if numNoneMoves == 2:
whiteExceedTime = whiteSec > constants.TIME_LIMIT_SEC
blackExceedTime = blackSec > constants.TIME_LIMIT_SEC
whiteMem = white.getMemoryUsedMB()
blackMem = black.getMemoryUsedMB()
whiteExceedMem = whiteMem > constants.MEMORY_LIMIT_MB
blackExceedMem = blackMem > constants.MEMORY_LIMIT_MB
message = 'No moves for two consecutive plies.\n'
if whiteExceedTime: message += 'White exceeded time limit!\n'
if blackExceedTime: message += 'Black exceeded time limit!\n'
if whiteExceedMem: message += 'White exceeded memory limit!\n'
if blackExceedMem: message += 'Black exceeded memory limit!\n'
log.write('M: Both players have no valid moves\n')
log.write('M: W exceeded time = %d (%f secs); B exceeded time = %d (%f secs)\n' % \
(int(whiteExceedTime), whiteSec, int(blackExceedTime), blackSec) )
log.write('M: W exceeded mem = %d (%f MB); B exceeded time = %d (%f MB)\n' % \
(int(whiteExceedMem), whiteMem, int(blackExceedMem), blackMem) )
break
else:
numNoneMoves = 0
prevMove = move
if self.curPlayer == 'W':
whiteSec += diffSec
if move: whiteMoves += 1
else:
blackSec += diffSec
if move: blackMoves += 1
if move != None and not self.validMoveInSomeDir(move, self.curPlayer, Board.oppositeColor(self.curPlayer)):
message = '%s has provided an invalid move %s! Terminating...\n' % (self.curPlayerColorStr(), move)
log.write('M: ' + message)
break
flips = self.makeMove(move)
if move == None:
if self.curPlayer == 'W': timeSec, memMB = whiteSec, white.getMemoryUsedMB()
else: timeSec, memMB = blackSec, black.getMemoryUsedMB()
msg = '%s has no moves. ' % self.curPlayerColorStr()
log.write('%s: -1 -1' % self.curPlayer)
if timeExceeded:
msg += '\n%s EXCEEDED TIME LIMIT! (%f secs).\n' % (self.curPlayerColorStr(), timeSec)
log.write(' ; time exceeded (%f secs)' % timeSec)
if memExceeded:
msg += '%s EXCEEDED MEMORY LIMIT! (%f MB).\n' % (self.curPlayerColorStr(), memMB)
log.write(' ; memory exceeded (%f MB)' % memMB)
log.write('\n')
else:
msg = ''
log.write('%s: %d %d\n' % (self.curPlayer, move[0], move[1]))
wpieces, bpieces = self.computeScore()
self.curPlayer = self.oppositeColor(self.curPlayer)
msg += "(W: %d , B: %d). %s's turn..." % (wpieces, bpieces, self.curPlayerColorStr())
self.guiMsg.set(msg )
self.guiDrawBoard(move, flips)
self.guiTk.configure(cursor='watch')
self.guiTk.update()
time.sleep(constants.MOVE_SEC)
boardCopy = copy.deepcopy(self.board)
white.gameEnd(boardCopy)
black.gameEnd(boardCopy)
whiteMB = white.getMemoryUsedMB()
blackMB = black.getMemoryUsedMB()
wscore, bscore = self.computeScore()
message += 'White: %d Moves: %d MB: %f Secs: %f\n' % \
(wscore, whiteMoves, whiteMB, whiteSec)
message += 'Black: %d Moves: %d MB: %f Secs: %f\n' % \
(bscore, blackMoves, blackMB, blackSec)
whiteSec = round(whiteSec,1)
blackSec = round(blackSec,1)
whiteMB = round(whiteMB, 1)
blackMB = round(blackMB, 1)
if wscore > bscore: message += 'White wins!\n'
elif wscore < bscore: message += 'Black wins!\n'
elif whiteSec < blackSec: message += 'White wins (by using less time)!\n'
elif whiteSec > blackSec: message += 'Black wins (by using less time)!\n'
elif whiteMB < blackMB: message += 'White wins (by using less memory)!\n'
elif whiteMB > blackMB: message += 'Black wins (by using less memory)!\n'
else: message += "It's a draw!\n"
self.guiMsg.set(message)
self.guiTk.configure(cursor="X_cursor")
for box in self.flipBoxes:
self.guiCanvas.delete(box)
self.flipBoxes = []
log.write('M: GAME ENDS\n'+message+'\n')
log.close()
while self.on:
self.guiTk.update()
time.sleep(constants.SLEEP_SEC)
######################### HUMAN PLAYER ####################################
def chooseMove(self, boardCopy, prevMove):
'''
Modeling a human player. Takes inputs from mouse click on board
'''
for box in self.flipBoxes:
self.guiCanvas.delete(box)
self.flipBoxes = []
self.moveXY = None
while self.on:
self.moveMade = False
self.guiDrawBoard(None, None)
self.guiCanvas.focus_force()
self.guiTk.configure(cursor='target')
while self.on and not self.moveMade:
self.guiTk.update()
time.sleep(constants.SLEEP_SEC)
if not self.on: break
assert self.moveMade, 'ERROR: moveMade should be True'
allMoves = self.findAllMoves()
if self.moveXY not in allMoves:
self.guiTk.bell()
continue
return self.moveXY
return self.moveXY
def gameEnd(self, boardCopy):
'''
Modeling a human player. No clean up to do, so simply return.
'''
pass
def getColor(self):
return self.curPlayer
def getMemoryUsedMB(self):
return 0.0
if __name__ == '__main__':
board = Board()
#two human players using mouse clicks
#white = board
#black = board
#two automated players using random move
#import randomplayer
#white = randomplayer.RandomPlayer('W')
#black = randomplayer.RandomPlayer('B')
#white: human player, black: player using random move
#import randomplayer
#white = board
#black = randomplayer.RandomPlayer('B')
#white: player using random move, black: human player
import playerv3
black = playerv3.Player('B')
white = board
#two automated players connecting via TCP/IP
#import server
#white = server.Server(1)
#black = server.Server(2)
#play from log file
#import fileplayer
#logFile = 'gamelog-2015-06-06_20-40-29.txt'
#white = fileplayer.FilePlayer('W', logFile)
#black = fileplayer.FilePlayer('B', logFile)
board.playGame(white, black)