-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimax.py
366 lines (297 loc) · 12.5 KB
/
minimax.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
########################################################################################################################
# WELCOME TO THE DOTS AND SQUARES PLAYING ARTIFICIAL INTELLIGENCE
#
# THE RULES:
# - 2 player game.
# - Game is initialized in a x by x grid.
# - The goal is to capture as many squares on the grid as possible.
# - Player -1 (human) starts by marking one border.
# - The player with the most captured squares wins.
# - You capture a square when you mark the last border of the square.
# - When you capture a square, you get to go again. Other player has to wait.
#
########################################################################################################################
import time
import math
import random
class Game:
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
self.board = [0] * ((self.rows * 2 + 1) * self.cols + self.rows)
self.gameEnded = False
self.currentPlayer = -1
self.positions = [i for i, n in enumerate(self.board)]
self.players = {
1: 0,
-1: 0
}
# Define the excluded cells and directions.
self.exclude_top = None
self.exclude_bottom = None
self.exclude_left = None
self.exclude_right = None
self.border_directions = None
self.get_excluded_positions()
self.define_border_directions()
def initialize_game(self):
self.board = [0] * ((self.rows * 2 + 1) * self.cols + self.rows)
self.currentPlayer = 1
self.positions = [i for i, n in enumerate(self.board)]
self.gameEnded = False
def changePlayer(self):
self.currentPlayer *= -1
def get_excluded_positions(self):
self.exclude_top = self.positions[0:self.cols]
self.exclude_bottom = self.positions[-self.cols:]
self.exclude_left = [i for i in range(self.cols, len(self.board), self.cols * 2 + 1)]
self.exclude_right = [i for i in range(self.cols * 2, len(self.board), self.cols * 2 + 1)]
def define_border_directions(self):
self.border_directions = [0] * ((self.rows * 2 + 1) * self.cols + self.rows)
for i in range(0, len(self.board), self.cols * 2 + 1):
self.border_directions[i:i + self.cols] = [1] * self.cols
def check_horizontal_tiles(self, border):
if border in self.exclude_left:
right = [border, border - self.cols, border + 1, border + 1 + self.cols]
left = None
elif border in self.exclude_right:
left = [border, border + self.cols, border - 1, border - 1 - self.cols]
right = None
else:
right = [border, border - self.cols, border + 1, border + 1 + self.cols]
left = [border, border + self.cols, border - 1, border - 1 - self.cols]
return left, right
def check_vertical_tiles(self, border):
"""
If the input border is a horizontal border, the tiles to the top and bottom need to be checked.
If the border is at the top or bottom of the playing field, either the top or bottom has to be exluded.
:param border: The index of the border played
:return: A list with the indices of the surrounding tiles
"""
if border in self.exclude_top:
top = None
bottom = [border, border + self.cols, border + self.cols + 1, border + self.cols * 2 + 1]
elif border in self.exclude_bottom:
top = [border, border - self.cols, border - self.cols - 1, border - self.cols * 2 - 1]
bottom = None
else:
top = [border, border - self.cols, border - self.cols - 1, border - self.cols * 2 - 1]
bottom = [border, border + self.cols, border + self.cols + 1, border + self.cols * 2 + 1]
return top, bottom
def getPossibleActions(self):
return [i for i, n in enumerate(self.board) if n == 0]
def print_board(self):
"""
Print the current boards state. A 1 indicates player 1, a 0 indicated player 2.
:return:
"""
idx = 0
for i in range(self.rows * 2 + 1):
# row number is even:
if i % 2 == 0:
out = ''
for j in range(self.cols):
if self.board[idx] == 0:
out += '-----'
elif self.board[idx] == 1:
out += 'AAAAA'
elif self.board[idx] == -1:
out += 'HHHHH'
idx += 1
print(out)
# row number is uneven
else:
out = ''
for j in range(self.cols + 1):
if self.board[idx] == 0:
out += '| '
elif self.board[idx] == 1:
out += 'A '
elif self.board[idx] == -1:
out += 'H '
idx += 1
print(out)
# Determines if the made move is a legal move
def is_valid(self, action, possibleActions):
print(action, possibleActions)
if action in possibleActions:
return True
else:
return False
# Checks if the game has ended and returns the winner in each case
def is_end(self):
if len(self.getPossibleActions()) == 0:
print('Current endgame state: ', self.players)
return 1 if self.players[1] > self.players[-1] else -1 if self.players[-1] > self.players[1] else 0
else:
return None
def evaluateScores(self):
"""
Get the current value of the game state. This is the number of squares captured. A negative number means that
minimizing player is in favor, and vice versa.
:return: the net squares captured, either positive or negative
"""
# Square the score difference, to promote more square captures over fewer (as it could also mean tie)
score = self.players[1] - self.players[-1]
if score < 0:
score = score ** 2 * -1
else:
score = score ** 2
# if score is not 0:
# print(f'player 1: {self.players[1]} and player -1: {self.players[-1]}')
# print('board value: ', score)
return score
def play(self, action):
# Update the board
self.board[action] = self.currentPlayer
# Check the direction of the border
direction = self.border_directions[action]
# Horizontal borders
if direction == 1:
top, bottom = self.check_vertical_tiles(action)
available_top_borders = 4
available_bottom_borders = 4
if top is not None:
for border in top:
if self.board[border] != 0:
available_top_borders -= 1
if bottom is not None:
for border in bottom:
if self.board[border] != 0:
available_bottom_borders -= 1
if available_bottom_borders == 0:
self.players[self.currentPlayer] += 1
if available_top_borders == 0:
self.players[self.currentPlayer] += 1
if available_top_borders == 0 or available_bottom_borders == 0:
return
self.changePlayer()
# Vertical borders
elif direction == 0:
left, right = self.check_horizontal_tiles(action)
available_left_borders = 4
available_right_borders = 4
if left is not None:
for border in left:
if self.board[border] != 0:
available_left_borders -= 1
if right is not None:
for border in right:
if self.board[border] != 0:
available_right_borders -= 1
if available_left_borders == 0:
self.players[self.currentPlayer] += 1
if available_right_borders == 0:
self.players[self.currentPlayer] += 1
if available_right_borders == 0 or available_left_borders == 0:
return
self.changePlayer()
def minimax(self, depth, alpha, beta, player):
if depth == 0 or len(self.getPossibleActions()) == 0:
# TODO: Maybe need to use a - sign?
return self.evaluateScores()
moves = self.getPossibleActions()
# AI is the maximizing (player 1)
if player == 1:
highestValue = -999
for move in moves:
# Save current game state
currentScore = self.players.copy()
currentPlayer = self.currentPlayer
currentBoard = self.board.copy()
# Play move, and move down the tree and retrieve the
self.play(move)
if self.currentPlayer == 1:
value = self.minimax(depth - 1, alpha, beta, 1)
else:
value = self.minimax(depth - 1, alpha, beta, -1)
highestValue = max(highestValue, value)
alpha = max(alpha, value)
# Undo the move
self.players = currentScore
self.currentPlayer = currentPlayer
self.board = currentBoard
if alpha >= beta:
break
return highestValue
# Human is minimizing (player -1)
else:
lowestValue = 999
for move in moves:
# Save current game state
currentScore = self.players.copy()
currentPlayer = self.currentPlayer
currentBoard = self.board.copy()
# Play the move, and move down the tree
self.play(move)
if self.currentPlayer == 1:
value = self.minimax(depth-1, alpha, beta, 1)
else:
value = self.minimax(depth-1, alpha, beta, -1)
lowestValue = min(lowestValue, value)
beta = min(beta, value)
# Undo the move
self.players = currentScore
self.currentPlayer = currentPlayer
self.board = currentBoard
if alpha >= beta:
break
return lowestValue
# TODO: check why the player is passed in this function. Maybe this root can be skipped
def minimaxRoot(self, depth, player):
moves = self.getPossibleActions()
bestMove = -999
bestMovesFound = None
for move in moves:
# Save current game state
currentScore = self.players.copy()
currentPlayer = self.currentPlayer
currentBoard = self.board.copy()
# Play the move
self.play(move)
if self.currentPlayer == -1:
value = self.minimax(depth - 1, -999, 999, -1)
else:
value = self.minimax(depth - 1, -999, 999, 1)
# Undo the move, and restore to saved game state
self.players = currentScore
self.currentPlayer = currentPlayer
self.board = currentBoard
if value >= bestMove:
bestMove = value
bestMovesFound = move
# else:
# bestMovesFound.append(move)
return bestMovesFound, bestMove
def main():
g = Game(3, 3)
while True:
print(f'Current score: {g.players}')
g.print_board()
g.result = g.is_end()
if g.result is not None:
if g.result == -1:
print('Human wins!')
elif g.result == 1:
print('AI wins!')
elif g.result == 0:
print("It's a tie!")
g.initialize_game()
return
if g.currentPlayer == -1:
while True:
action = int(input('Insert the X coordinate: '))
if g.is_valid(action, g.getPossibleActions()):
g.play(action)
break
else:
print('The move is not valid! Try again.')
else:
# Run the minimax algorithm to look ahead, and return a list of moves with the best value
print('Computer is thinking...')
move, value = g.minimaxRoot(6, 1)
# Pick a random choice of the available moves and play
# move = random.choice(moves)
g.play(move)
if __name__ == "__main__":
main()