-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmancala.py
executable file
·288 lines (239 loc) · 8.5 KB
/
mancala.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
#!/usr/bin/env python3
import argparse
import sys
from time import time
import multiprocessing
from io import StringIO
DEPTH = 5
DONT_SCORE_ONE = True
def compute(x):
move_sequence, board = x
return [x + 1 for x in move_sequence], board.mini_max(DEPTH)
class Board:
PLAYER_SCORE_HOLDER = 7
def __str__(self, *args, **kwargs):
return str(self.board)
def __repr__(self, *args, **kwargs):
return "Board%s" % self.__str__()
@property
def player_points(self):
if self.no_more_moves():
return sum(self.board[1:8])
else:
return self.board[7]
@property
def opponent_points(self):
if self.no_more_moves():
return self.board[0] + sum(self.board[8:])
else:
return self.board[0]
def __init__(self, board=None):
if board is not None:
self.board = board.board[:]
self.reversed = board.reversed
else:
self.board = [0, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4]
self.reversed = False
def make_player_move(self, n):
assert n < 6
n += 1
tokens = self.board[n]
assert tokens > 0
self.board[n] = 0
while tokens:
tokens -= 1
n += 1
if n >= len(self.board):
n = 1
self.board[n] += 1
if n == self.PLAYER_SCORE_HOLDER:
return True
if self.board[n] == 1 and 0 < n < 7:
oponent_pos = len(self.board) - n
if DONT_SCORE_ONE is False or (
DONT_SCORE_ONE is True and self.board[oponent_pos] != 0
):
self.board[n] = 0
self.board[self.PLAYER_SCORE_HOLDER] += 1 + self.board[oponent_pos]
self.board[oponent_pos] = 0
return False
def possible_player_moves(self):
for i, a in enumerate(self.board[1:7]):
if a > 0:
yield i
def get_player_moves(self, pos, seq, moves):
assert self.board[1 + pos] != 0
new_board = Board(self)
move_continue = new_board.make_player_move(pos)
if move_continue and list(new_board.possible_player_moves()):
for i in new_board.possible_player_moves():
new_board.get_player_moves(i, seq + [pos], moves)
else:
moves.append((seq + [pos], new_board))
return
def find_all_moves(self):
all_moves = []
for i in self.possible_player_moves():
self.get_player_moves(i, [], all_moves)
return all_moves
def get_opponent_board(self):
b = Board()
b.board = self.board[7:] + self.board[:7]
b.reversed = not self.reversed
return b
def no_more_moves(self):
if any(self.board[8:]) == False or any(self.board[1:7]) == False:
return True
return False
def mini_max(self, depth=2, maximizing_player=False):
if depth == 0 or self.no_more_moves():
return self.get_heurestic_score()
if maximizing_player:
best_value = -999
for move, board in self.get_opponent_board().find_all_moves():
val = board.mini_max(depth - 1, not maximizing_player)
best_value = max(best_value, val)
return best_value
else:
best_value = 999
for move, board in self.get_opponent_board().find_all_moves():
val = board.mini_max(depth - 1, not maximizing_player)
best_value = min(best_value, val)
return best_value
def mini_max_alpha_beta(
self, depth=2, alpha=-999, beta=+999, maximizing_player=False
):
if depth == 0 or self.no_more_moves():
return self.get_heurestic_score()
if maximizing_player:
best_value = -999
for move, board in self.get_opponent_board().find_all_moves():
best_value = max(
best_value,
board.mini_max(depth - 1, alpha, beta, not maximizing_player),
)
alpha = max(alpha, best_value)
if beta <= alpha:
break
return best_value
else:
best_value = 999
for move, board in self.get_opponent_board().find_all_moves():
best_value = min(
best_value, board.mini_max(depth - 1, not maximizing_player)
)
beta = min(beta, best_value)
if beta <= alpha:
break
return best_value
def find_best_move(self):
print("Calculating best move...")
t = time()
def moves():
with multiprocessing.Pool(multiprocessing.cpu_count()) as pool:
yield from pool.map(compute, list(self.find_all_moves()))
result = sorted(moves(), key=lambda x: x[1], reverse=True)[:1]
print("Calculated in %.1fs" % (time() - t))
return result
def print(self):
print(" ", end="")
print(*["%2d" % x for x in reversed(self.board[8:])], sep="|")
print(
"AI --> %2d %2d <-- You"
% (self.opponent_points, self.player_points)
)
print(" ", end="")
print(*["%2d" % x for x in self.board[1:7]], sep="|")
print("")
print(" ^ ^ ^ ^ ^ ^")
print("moves: 1 2 3 4 5 6")
def string(self):
result = StringIO()
print(" ", end="", file=result)
print(*["%2d" % x for x in reversed(self.board[8:])], sep="|", file=result)
print(
"AI --> %2d %2d <-- You"
% (self.opponent_points, self.player_points),
file=result,
)
print(" ", end="", file=result)
print(*["%2d" % x for x in self.board[1:7]], sep="|", file=result)
return result.getvalue()
def get_heurestic_score(self):
if not self.reversed:
return self.player_points - self.opponent_points
else:
return self.opponent_points - self.player_points
def player_move(board):
has_move = True
while has_move:
command = input("Player move: ").split()
if not command:
continue
if command[0] == "q":
sys.exit(0)
try:
c = int(command[0])
has_move = board.make_player_move(c - 1)
board.print()
except:
print("Wrong move: ", command[0])
continue
return board
def opponent_move(board):
board = board.get_opponent_board()
has_move = True
while has_move:
command = input("Opponent move: ").split()
if not command:
continue
if command[0] == "q":
sys.exit(0)
try:
c = int(command[0])
has_move = board.make_player_move(c - 1)
board.get_opponent_board().print()
except:
print("Wrong move: ", command[0])
continue
return board.get_opponent_board()
def run_game(initial_board=None, player_starts=True):
board = Board()
if initial_board is not None: # Instantiate a board
board.board = initial_board
board.print() # Show the user the starting board
while 1:
if player_starts: # Player means the AI
for (
best_move
) in (
board.find_best_move()
): # Calcualte the best move and show it to the user
print(best_move)
board = player_move(board)
board = opponent_move(board)
else:
board = opponent_move(board)
for best_move in board.find_best_move():
print(best_move)
board = player_move(board)
if board.no_more_moves():
print("Games ended")
break
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Mancala AI")
parser.add_argument(
"-b",
"--board",
type=int,
nargs=14,
default=None,
help="Board layout, e.g 0 4 4 4 4 4 4 0 4 4 4 4 4 4",
)
parser.add_argument("-d", "--depth", type=int, default=5)
parser.add_argument("-o", "--opponent-starts", default=False, action="store_true")
parser.add_argument("--dont-score-one", default=False, action="store_true")
args = parser.parse_args()
DEPTH = args.depth
DONT_SCORE_ONE = args.dont_score_one
run_game(args.board, not args.opponent_starts)