-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
267 lines (220 loc) · 8.75 KB
/
player.py
File metadata and controls
267 lines (220 loc) · 8.75 KB
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
from random import randint
from lib import Constants
class Player:
def __init__(self, player):
self.player = player
self.pits = []
self.pockets = []
self.oppo_pits = []
self.oppo_pockets = []
# Beginning of Mini-Max
def minimax_decision(self, board):
max_value = -1
temp_board = []
node_count = 0
for i in board:
temp_board.append(i.getNumStones())
next_pit = None
for pit_index in self.pits:
if temp_board[pit_index] == 0:
continue
temp_state = self.get_state(pit_index, temp_board)
op_value, nd_count = self.mm_find_mini_value(temp_state, temp_board)
node_count = node_count + nd_count
if op_value > max_value:
max_value = op_value
next_pit = temp_state
elif op_value == max_value:
rand = randint(0, 1)
if rand == 0:
next_pit = temp_state
# make next available move
if next_pit is None:
for i in self.pits:
if not temp_board[i] == 0:
next_pit = self.get_state(i, temp_board)
new_board = self.updateBoard(next_pit, temp_board)
next_pit.setBoard(new_board)
next_pit.setNumStones(0)
break
return next_pit, node_count
def mm_find_max_value(self, state, board):
node_count = 1
if state.getDepth() >= Constants.MAX_DEPTH:
return state.getDiff(), node_count
value = - Constants.MAX_STONES * Constants.NUM_PITS
# self
temp_board = self.updateBoard(state, board)
state.setBoard(temp_board)
state.setNumStones(0)
for p_ind in self.pits:
if temp_board[p_ind] == 0:
continue
temp_val, nd_count = self.mm_find_mini_value(self.get_state(p_ind, temp_board, state), temp_board)
value = max(value, temp_val)
node_count = node_count + nd_count
if value == - Constants.MAX_STONES * Constants.NUM_PITS:
value = state.getDiff()
return value, node_count
def mm_find_mini_value(self, state, board):
node_count = 1
if state.getDepth() >= Constants.MAX_DEPTH:
return state.getDiff(), node_count
value = Constants.MAX_STONES * Constants.NUM_PITS
# oppo
temp_board = self.updateBoard(state, board)
state.setBoard(temp_board)
state.setNumStones(0)
for p_ind in self.oppo_pits:
if temp_board[p_ind] == 0:
continue
temp_val, nd_count = self.mm_find_max_value(self.get_state(p_ind, temp_board, state), temp_board)
value = min(value, temp_val)
node_count = node_count + nd_count
if value == Constants.MAX_STONES * Constants.NUM_PITS:
value = state.getDiff()
return value, node_count
def get_state(self, pit_index, board, parent=None):
self_points = 0
oppo_points = 0
for i in self.pockets:
self_points = self_points + board[i]
for i in self.oppo_pockets:
oppo_points = oppo_points + board[i]
if parent:
depth = parent.getDepth() + 1
else:
depth = 0
return State(parent, depth, pit_index, board[pit_index], board, self_points, oppo_points)
def updateBoard(self, state, board):
newBoard = []
# copy over
for i in range(len(board)):
newBoard.append(board[i])
num_stones = state.getNumStones()
newBoard[state.getPitIndex()] = 0
for i in range(num_stones):
# TODO: jump opponent's pockets
pit_index = (state.getPitIndex() + i + 1) % Constants.NUM_PITS
# put a stone in a pit
newBoard[pit_index] = newBoard[pit_index] + 1
return newBoard
# End of Mini-Max
# Beginning of Alpha-Beta
def alpha_beta_search(self, board):
max_value = -1
temp_board = []
node_count = 0
for i in board:
temp_board.append(i.getNumStones())
next_pit = None
for pit_index in self.pits:
if temp_board[pit_index] == 0:
continue
temp_state = self.get_state(pit_index, temp_board)
op_value, nd_count = self.ab_find_mini_value(
temp_state, temp_board, - Constants.MAX_STONES * Constants.NUM_PITS, Constants.MAX_STONES * Constants.NUM_PITS)
node_count = node_count + nd_count
if op_value > max_value:
max_value = op_value
next_pit = temp_state
elif op_value == max_value:
rand = randint(0, 1)
if rand == 0:
next_pit = temp_state
# make next available move
if next_pit is None:
for i in self.pits:
if not temp_board[i] == 0:
next_pit = self.get_state(i, temp_board)
new_board = self.updateBoard(next_pit, temp_board)
next_pit.setBoard(new_board)
next_pit.setNumStones(0)
break
return next_pit, node_count
def ab_find_max_value(self, state, board, a, b):
node_count = 1
if state.getDepth() >= Constants.MAX_DEPTH:
return Constants.NUM_STONES * Constants.NUM_PITS / 2 - state.getSelfPoints(), node_count
value = - Constants.MAX_STONES * Constants.NUM_PITS
# self
temp_board = self.updateBoard(state, board)
state.setBoard(temp_board)
state.setNumStones(0)
for p_ind in self.pits:
if temp_board[p_ind] == 0:
continue
temp_val, nd_count = self.ab_find_mini_value(self.get_state(p_ind, temp_board, state), temp_board, a, b)
value = max(value, temp_val)
node_count = node_count + nd_count
if value >= b:
return value, node_count
a = max(a, value)
if value == - Constants.MAX_STONES * Constants.NUM_PITS:
value = Constants.NUM_STONES * Constants.NUM_PITS / 2 - state.getSelfPoints()
return value, node_count
def ab_find_mini_value(self, state, board, a, b):
node_count = 1
if state.getDepth() >= Constants.MAX_DEPTH:
return Constants.NUM_STONES * Constants.NUM_PITS / 2 - state.getSelfPoints(), node_count
value = Constants.MAX_STONES * Constants.NUM_PITS
# oppo
temp_board = self.updateBoard(state, board)
state.setBoard(temp_board)
state.setNumStones(0)
for p_ind in self.oppo_pits:
if temp_board[p_ind] == 0:
continue
temp_val, nd_count = self.ab_find_max_value(self.get_state(p_ind, temp_board, state), temp_board, a, b)
value = min(value, temp_val)
node_count = node_count + nd_count
if value <= a:
return value, node_count
b = min(b, value)
if value == Constants.MAX_STONES * Constants.NUM_PITS:
value = Constants.NUM_STONES * Constants.NUM_PITS / 2 - state.getSelfPoints()
return value, node_count
# End of Alpha-Beta
def setPits(self, pit_list):
self.pits = pit_list
def setPockets(self, pocket_list):
self.pockets = pocket_list
def setOppoPits(self, oppo_pit_list):
self.oppo_pits = oppo_pit_list
def setOppoPockets(self, oppo_pocket_list):
self.oppo_pockets = oppo_pocket_list
def getPits(self):
return self.pits
def getPockets(self):
return self.pockets
def getOppoPockets(self):
return self.oppo_pockets
class State:
def __init__(self, parent, depth, pit_index, num_stones, board, self_points=-1, oppo_points=-1):
self.parent = parent
self.depth = depth
self.pit_index = pit_index
self.num_stones = num_stones
self.board = board
self.self_points = self_points
self.oppo_points = oppo_points
def getDepth(self):
return self.depth
def getDiff(self):
return self.self_points - self.oppo_points
def getNumStones(self):
return self.num_stones
def getPitIndex(self):
return self.pit_index
def getBoard(self):
return self.board
def getSelfPoints(self):
return self.self_points
def setNumStones(self, newNum):
self.num_stones = newNum
def setSelfPoints(self, newPoints):
self.self_points = newPoints
def setOppoPoints(self, newPoints):
self.oppo_points = newPoints
def setBoard(self, newBoard):
self.board = newBoard