-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarkPlayer.py
332 lines (310 loc) · 12.4 KB
/
darkPlayer.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
import sys
sys.path.append("./")
import gameBoard
import copy
from random import randint
import math
from Node import *
import util
DARKPLAYER = 1
LIGHTPLAYER = 0
DARK = 'X'
LIGHT = 'O'
EMPTY = '.'
MAX = 1
MIN = 0
POSSIBLE_FIRST_MOVE_DARK = [(8,8),(1,1),(5,5),(4,4)]
class Player:
def __init__(self, identity, round, total_eval=0, cutoffs=0, branch_factor=[]):
'''
identity : int (DARKPLAYER or LIGHTPLAYER)
round : int
'''
self.identity = identity
self.round = round
self.total_eval = total_eval
self.cutoffs = cutoffs
self.branch_factor = branch_factor
def minimax_alpha_beta(self, identity, gameBoard, depth_limit, alpha = -9999, beta = 9999, move=None,level=0 ):
'''
Minimax algorithm with alpha-beta pruning
identity : int (DARKPLAYER or LIGHTPLAYER)
gameBoard : gameBoard
depth_limit : int
move : (int,int)
level : int
'''
if identity:
cell = DARK
else:
cell = LIGHT
if level == depth_limit:
self.total_eval += 1
return self.evaluation(gameBoard,identity), move
possibleMoves = self.availableMoves(gameBoard, identity)
frontier = util.Queue()
currentState = Node(gameBoard,None,level,move)
branch = 0
for start in possibleMoves.keys():
for end in possibleMoves[start]:
if end:
branch += 1
game = copy.deepcopy(gameBoard)
game.board = game.updateBoard(start,end,cell)
newNode = Node(game, currentState, currentState.level+1,(start,end))
frontier.push(newNode)
self.branch_factor.append(branch)
if self.tellMinMax(level):
bestMove = currentState.move
while not frontier.isEmpty():
currentNode = frontier.pop()
if identity:
newID = LIGHTPLAYER
else:
newID = DARKPLAYER
if self.lose(currentState.gameBoard):
bestValue = -9998
else:
bestValue, move = self.minimax_alpha_beta(newID, currentNode.gameBoard,depth_limit, alpha, beta, currentNode.move,currentNode.level)
move = currentNode.move
if bestValue > alpha:
alpha = bestValue
bestMove = move
if alpha >= beta:
self.cutoffs += 1
return beta, bestMove
return alpha, bestMove
else:
bestMove = currentState.move
while not frontier.isEmpty():
currentNode = frontier.pop()
if identity:
newID = LIGHTPLAYER
else:
newID = DARKPLAYER
if self.lose(currentNode.gameBoard):
bestValue = -9998
else:
bestValue, move = self.minimax_alpha_beta(newID,currentNode.gameBoard,depth_limit,alpha, beta,currentNode.move,currentNode.level)
move = currentNode.move
if bestValue < beta:
beta = bestValue
bestMove = move
if beta <= alpha:
self.cutoffs += 1
return alpha, bestMove
return beta, bestMove
def minimax(self, identity, gameBoard,depth_limit, move=None,level=0 ):
if identity:
cell = DARK
else:
cell = LIGHT
if level == depth_limit:
self.total_eval += 1
return self.evaluation(gameBoard,identity), move
possibleMoves = self.availableMoves(gameBoard, identity)
frontier = util.Queue()
currentState = Node(gameBoard,None,level,move)
branch = 0
for start in possibleMoves.keys():
for end in possibleMoves[start]:
if end:
branch += 1
game = copy.deepcopy(gameBoard)
game.board = game.updateBoard(start,end,cell)
newNode = Node(game, currentState, currentState.level+1,(start,end))
frontier.push(newNode)
self.branch_factor.append(branch)
if self.tellMinMax(level+1) :
currentBestValue = -9999
bestMove = currentState.move
while not frontier.isEmpty():
currentNode = frontier.pop()
if identity:
newID = LIGHTPLAYER
else:
newID = DARKPLAYER
bestValue, move = self.minimax(newID, currentNode.gameBoard,depth_limit, currentNode.move,currentNode.level)
move = currentNode.move
if currentBestValue < bestValue:
currentBestValue = bestValue
bestMove = move
return currentBestValue, bestMove
else:
currentBestValue = 9999
bestMove = currentState.move
while not frontier.isEmpty():
currentNode = frontier.pop()
if identity:
newID = LIGHTPLAYER
else:
newID = DARKPLAYER
bestValue, move = self.minimax(newID,currentNode.gameBoard,depth_limit,currentNode.move,currentNode.level)
move =currentNode.move
if currentBestValue > bestValue:
currentBestValue = bestValue
bestMove = move
return currentBestValue, bestMove
def tellMinMax(self, level):
'''
Given level, return FALSE if MIN level, and TRUE if MAX level
'''
if level % 2 == 0:
return MIN
return MAX
def evaluation(self, gameBoard,identity):
'''
Evaluate a gameBoard based on identity. Evaluation based on remaining dark and light pieces.
'''
darkCells = gameBoard.getDarkCell()
lightCells = gameBoard.getLightCell()
darkScore = len(darkCells)
lightScore = len(lightCells)
if not identity:
return darkScore - lightScore
else:
return lightScore - darkScore
def generateFirstMove_Dark(self):
'''
Generate the first move for DARKPLAYER
'''
i = randint(0,3)
return POSSIBLE_FIRST_MOVE_DARK[i]
def generateFirstMove_Light(self, darkMove, gameBoard):
'''
Generate the first move for LIGHTPLAYER. Must be piece adjacent to the piece DARKPLAYER removed
'''
adjs = [1,-1]
light_moves = []
light_move = ()
for adj in adjs:
if (not darkMove[0] + adj == gameBoard.width + 1) and darkMove[0] + adj:
light_move = (darkMove[0]+adj, darkMove[1])
light_moves.append(light_move)
if (not darkMove[1] + adj == gameBoard.width + 1) and darkMove[1] + adj:
light_move = (darkMove[0], darkMove[1]+adj)
light_moves.append(light_move)
i = randint(0,len(light_moves)-1)
return light_moves[i]
def win(self, gameBoard):
'''
Given gameBoard, determine who wins.
'''
if self.identity:
newID = LIGHTPLAYER
else:
newID = DARKPLAYER
otherP_possibleMoves = self.availableMoves(gameBoard, newID)
for move in otherP_possibleMoves.keys():
if otherP_possibleMoves[move] :
return False
return True
def lose(self, gameBoard):
'''
Given gameBoard, determine if agent loses (no moves left)
'''
possibleMoves = self.availableMoves(gameBoard, self.identity)
for move in possibleMoves.keys():
if possibleMoves[move] :
return False
return True
def getEast (self, gameBoard, move):
if not move[1] == gameBoard.width:
eastMove = (move[0], move[1]+1)
return gameBoard.getCellInfo(eastMove), eastMove
return None, None
def getWest (self, gameBoard, move):
if not move[1] == 1:
westMove = (move[0], move[1]-1)
return gameBoard.getCellInfo(westMove), westMove
return None, None
def getNorth(self, gameBoard, move):
if not move[0] == 1:
northMove = (move[0]-1, move[1])
return gameBoard.getCellInfo(northMove), northMove
return None, None
def getSouth(self, gameBoard, move):
if not move[0] == gameBoard.width:
southMove = (move[0]+1, move[1])
return gameBoard.getCellInfo(southMove), southMove
return None, None
def availableMoves(self, gameBoard, identity):
'''
Given gameBoard and identity, returns dictionary of legal moves.
'''
result = {}
if identity:
moveable = gameBoard.getDarkCell()
jumpOver = LIGHT
else:
moveable = gameBoard.getLightCell()
jumpOver = DARK
for move in moveable:
eastMove = []
westMove = []
northMove = []
southMove = []
eastMove = self.jumpToEast(eastMove, gameBoard, identity, jumpOver, move)
westMove = self.jumpToWest(westMove, gameBoard, identity, jumpOver, move)
northMove = self.jumpToNorth(northMove, gameBoard, identity, jumpOver, move)
southMove = self.jumpToSouth(southMove, gameBoard, identity, jumpOver, move)
result[move] = eastMove + westMove + northMove + southMove
return result
def jumpToEast(self, result, gameBoard, identity, jumpOver, moveable):
game = copy.deepcopy(gameBoard)
eastCell, eastPosition = self.getEast(gameBoard, moveable)
#if the cell in the east is opponent's check
if eastCell == jumpOver:
emptyCell, emptyPosition = self.getEast(gameBoard, eastPosition)
if emptyCell == EMPTY:
result.append(emptyPosition)
game.updateBoard(eastPosition, emptyPosition, identity)
self.jumpToEast(result, game, identity, jumpOver, emptyPosition)
return result
def jumpToWest (self, result, gameBoard, identity, jumpOver, moveable):
game = copy.deepcopy(gameBoard)
westCell, westPosition = self.getWest(gameBoard, moveable)
#if the cell in the east is opponent's check
if westCell == jumpOver:
emptyCell, emptyPosition = self.getWest(gameBoard, westPosition)
if emptyCell == EMPTY:
result.append(emptyPosition)
game.updateBoard(westPosition, emptyPosition, identity)
self.jumpToWest(result, game, identity, jumpOver, emptyPosition)
return result
def jumpToNorth(self, result, gameBoard, identity, jumpOver, moveable):
game = copy.deepcopy(gameBoard)
northCell, northPosition = self.getNorth(gameBoard, moveable)
#if the cell in the east is opponent's check
if northCell == jumpOver:
emptyCell, emptyPosition = self.getNorth(gameBoard, northPosition)
if emptyCell == EMPTY:
result.append(emptyPosition)
game.updateBoard(northPosition, emptyPosition, identity)
self.jumpToNorth(result, game, identity, jumpOver, emptyPosition)
return result
def jumpToSouth(self, result, gameBoard, identity, jumpOver, moveable):
game = copy.deepcopy(gameBoard)
southCell, southPosition = self.getSouth(gameBoard, moveable)
#if the cell in the east is opponent's check
if southCell == jumpOver:
emptyCell, emptyPosition = self.getSouth(gameBoard, southPosition)
if emptyCell == EMPTY:
result.append(emptyPosition)
game.updateBoard(southPosition, emptyPosition, identity)
self.jumpToSouth(result, game, identity, jumpOver, emptyPosition)
return result
def testLegalMove(self, gameBoard, identity, start, end):
'''
Returns whether a given move is legal or not.
Used to test the move of the other player.
'''
availableMoves = self.availableMoves(gameBoard, identity)
if start in availableMoves.keys():
if end in availableMoves[start]:
return True
return False
def roundIncrement(self):
self.round += 1
def getRound(self):
return self.round