-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
449 lines (383 loc) · 14.6 KB
/
main.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
439
440
441
442
443
444
445
446
447
448
449
from copy import deepcopy
from copy import copy
import random
EMPTY = ' '
BLACK = 'B'
WHITE = 'W'
max_closed_list={}
min_closed_list={}
def copy_board(board, size):
new_board = []
for i in range(size):
new_board.append([EMPTY] * size)
for j in range(len(board)):
new_board[i][j] = board[i][j]
return new_board
def compressing(board):
ret = ''
for i in board:
ret += ''.join(i)
return ret
board6 = [[100, -30, 2, 2, -30, 100], \
[-30, -40, -10, -10, -40, -30], \
[2, -10, 1, 1, -10, 2],\
[2, -10, 1, 1, -10, 2],\
[-30, -40, -10, -10, -40, -30], \
[100, -30, 2, 2, -30, 100]]
board8 = [[100, -30, 2, 2, 2, 2, -30, 100], \
[-30, -40, -30, -30, -30, -30, -40, -30], \
[2, -30, 1, 0, 0, 1, -30, 2],\
[2, -30, 0, 1, 1, 0, -30, 2],\
[2, -30, 0, 1, 1, 0, -30, 2],\
[2, -30, 1, 0, 0, 1, -30, 2],\
[-30, -40, -30, -30, -30, -30, -30, -30], \
[100, -30, 2, 2, 2, 2, -30, 100]]
board10 = [[100, -30, 2, 2, 2, 2, 2, 2, -30, 100], \
[-30, -40, -30, -30, -30, -30,-30, -30, -40, -30], \
[2, -30, 1, 1, 0, 0, 1, 1, -30, 2],\
[2, -30, 0, 0, 1, 1, 0, 0, -30, 2],\
[2, -30, 0, 0, 1, 1, 0, 0, -30, 2],\
[2, -30, 1, 1, 0, 0, 1, 1, -30, 2],\
[2, -30, 0, 0, 1, 1, 0, 0, -30, 2],\
[2, -30, 1, 1, 0, 0, 1, 1, -30, 2],\
[-30, -40, -30, -30, -30, -30, -30, -30, -40, -30], \
[100, -30, 2, 2, 2, 2, 2, 2, -30, 100]]
######################### INITIALIZATION OF BOARD ##################
def init_board(size):
mid = size/2
board = []
for i in range(size):
board.append([EMPTY] * size)
board[mid - 1][mid - 1] = BLACK
board[mid - 1][mid] = WHITE
board[mid][mid-1] = WHITE
board[mid][mid] = BLACK
return board
######################################################################
############################# DRAWING BOARD###########################
def draw_row(size):
ret = ' '
for column in range(size):
ret += '+-'
ret += '+\n'
return ret
def draw_col(size, board, row):
ret = str(row)
ret += '|'
for column in range(size):
ret += board[row][column]
ret += '|'
ret += '\n'
return ret
def print_board(board, size):
ret = ' '
for column in range(size):
ret += " " + str(column)
ret+= '\n'
for row in range(size):
ret += draw_row(size)
ret += draw_col(size, board, row)
ret += draw_row(size)
print ret
#######################################################################
############################ MAKING MOVE ##############################
def is_onboard(size, row, col):
return row >= 0 and row < size and col >= 0 and col < size
def is_in_range(board, size, row, col): #check if is on board and is empty
if is_onboard(size, row, col) == False or board[row][col] != EMPTY:
return False
return True
#checks if the pieces can be flipped and returns the flippable pieces
def get_flippable(board, size, player, row, column):
brd = copy_board(board, size)
brd[row][column] = player
opponent = BLACK if player == WHITE else WHITE
toFlip = []
for rdr, cdr in [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]:
r, c = row, column
r += rdr
c += cdr
if is_onboard(size,r, c) and board[r][c] == opponent:
r += rdr
c += cdr
if not is_onboard(size,r, c): #Check if it is a corner piece
continue
while board[r][c] == opponent:
r += rdr
c += cdr
if not is_onboard(size,r, c): #keep checking and break if boundary is reached before finding a piece
break
if not is_onboard(size,r, c): #because the break inside the while loop doesn't effect for loop
continue
if brd[r][c] == player:
while True:
r -= rdr
c -= cdr
if r == row and c == column:
break
toFlip.append([r, c])
if len(toFlip) == 0:
return False
return toFlip
#Flips the flippable pieces
def flip(board, row, col, player, toBeFlipped):
board[row][col] = player
for r, c in toBeFlipped:
board[r][c] = player
#makes move
def make_move(board, size, row, column, player):
# if is_onboard(size, row, column) == False or board[row][column] != EMPTY:
# return False
toBeFlipped = get_flippable(board, size, player, row, column)
if toBeFlipped == False:
return False
flip(board, row, column, player, toBeFlipped)
return True
#Returns the total score of a player
def score(board, player):
count = 0
for i in board:
count += i.count(player)
return count
#If there are zero flippable pieces or is out of range, returns false
def is_valid_move(board, size, row, col, player):
if is_in_range(board, size, row, col) == False:
return False
toBeFlipped = get_flippable(board, size, player, row, col)
if toBeFlipped == False:
return False
else:
return True
#Gets all the legal moves for a particular player's turn
def get_legal_moves(board,size, player):
legal_moves=[]
for i in range(size):
for j in range(size):
if is_valid_move(board, size, i, j, player):
legal_moves.append((i , j))
return legal_moves
#checks if the move is valid
def has_valid_move(board, size, player):
return len(get_legal_moves(board, size, player)) > 0
#returns true if both players do not have any valid moves to make
def game_over(board, size, player):
opponent = WHITE if player == BLACK else BLACK
if has_valid_move(board, size, player) or has_valid_move(board, size, opponent):
return False
else:
return True
#gets move from the player
def take_move(board, size, player):
print player, "'s turn\n"
row = input("Enter row: ")
col = input("Enter column: ")
while not is_valid_move(board, size, row, col, player):
print "Invalid Move! Please enter again"
row = input("Enter row: ")
col = input("Enter column: ")
make_move(board, size, row, col, player)
#######################################################################
######################### MAIN GAME ####################################
#runs a loop till game is over
def play_game(board, size, turn=WHITE):
opponent = BLACK if turn == WHITE else BLACK
while not game_over(board,size, turn):
if turn == WHITE:
print("WHITE's turn...")
if has_valid_move(board, size, turn):
move = random_move(board, size, turn)
print "move: ", move
make_move(board, size, move[0], move[1], turn)
#take_move(board, size, turn)
else:
print ("No valid moves")
else:
print("BLACK's turn...")
depth = 6
move = calculate(board, size, turn, depth)
#move = get_move(size, board, turn, time_left, opponent_time_left)
print "move: ", move
make_move(board, size, move[0], move[1], turn)
print_board(board, size)
opponent, turn = turn, opponent
print("Game Over!")
white_score = score(board, WHITE)
black_score = score(board, BLACK)
print("White:", white_score)
print("Black:", black_score)
if white_score > black_score:
print("Winner: WHITE")
elif white_score < black_score:
print("WINNER: BLACK")
else:
print("TIE")
############## HEURISTICS ######################################
def get_parity(board, player, opponent):
player_count = 0
opponent_count = 0
for i in board:
player_count = i.count(player)
opponent_count = i.count(opponent)
return 100 * (player_count - opponent_count)
def mcw(board, size, player):
opponent = BLACK if player == WHITE else WHITE
player_moves = get_legal_moves(board, size, player)
opponent_moves = get_legal_moves(board, size, opponent)
player_corners = 0
opponent_corners = 0
player_score = 0
opponent_score = 0
for i, j in ((0,0), (0, size - 1), (size - 1, 0), (size - 1, size - 1)):
if board[i][j] == player:
player_corners += 1
elif board[i][j] == opponent:
opponent_corners += 1
if size == 6:
for i, j in player_moves:
player_score += board6[i][j]
for i, j in opponent_moves:
opponent_score += board6[i][j]
elif size == 8:
for i, j in player_moves:
player_score += board8[i][j]
for i, j in opponent_moves:
opponent_score += board8[i][j]
return 100 * (len(player_moves) - len(opponent_moves)) + \
100 * (player_corners - opponent_corners) + \
(player_score - opponent_score)
def mobility(board, size, player):
opponent = BLACK if player == WHITE else WHITE
player_moves = len(get_legal_moves(board, size, player))
opponent_moves = len(get_legal_moves(board, size, opponent))
return 100 * (player_moves - opponent_moves)
def corners_captured(board, size, player):
opponent = BLACK if player == WHITE else WHITE
player_corners = 0
opponent_corners = 0
for i, j in ((0,0), (0, size - 1), (size - 1, 0), (size - 1, size - 1)):
if board[i][j] == player:
player_corners += 1
elif board[i][j] == opponent:
opponent_corners += 1
return 100 * (player_corners - opponent_corners)
def take_move_weight(board, size, player):
opponent = BLACK if player == WHITE else WHITE
player_score = 0
opponent_score = 0
player_moves = get_legal_moves(board, size, player)
opponent_moves = get_legal_moves(board, size, opponent)
if size == 6:
for i, j in player_moves:
player_score += board6[i][j]
for i, j in opponent_moves:
opponent_score += board6[i][j]
elif size == 8:
for i, j in player_moves:
player_score += board8[i][j]
for i, j in opponent_moves:
opponent_score += board8[i][j]
elif size == 10:
for i, j in player_moves:
player_score += board10[i][j]
for i, j in opponent_moves:
opponent_score += board10[i][j]
else:
return 0
return player_score - opponent_score
def heuristic(board, size, player):
# mob = mobility(board, size, player)
# cor = corners_captured(board, size, player)
# weight = take_move_weight(board, size, player)
# # print "Mobility: ", mob
# # print "Corner Score: ", cor
# return mob + cor + weight
return mcw(board, size, player)
def random_move(board, size, player):
moves = get_legal_moves(board,size,player)
if len(moves) == 0:
return (-1, -1)
else:
i = random.choice(moves)
return i
################################################################
def max(a, b):
return a if a >= b else b
def min(a, b):
return a if a <= b else b
def result(board, size, move, player):
new_board = copy_board(board, size)
make_move(new_board, size, move[0], move[1], player)
return new_board
def calculate(board, size, player, depth):
opponent = BLACK if player == WHITE else WHITE
value, final_move = alphabeta(board, size, player, opponent, depth, -100000000, 100000000, True, [-1, -1])
max_closed_list.clear
min_closed_list.clear
return final_move
def alphabeta(board, size, player, opponent, depth, alpha, beta, isMaximizing, current_move):
if depth == 0 or game_over(board, size, player):
if (game_over(board, size, player)):
return 100 * get_parity(board, player, opponent), current_move
return heuristic(board, size, player), current_move # [-1000, -1000]
if isMaximizing:
value = -10000000
final_move = None
movelist = get_legal_moves(board,size, player)
if len(movelist) < 1:
value = heuristic(board, size, player)
for move in movelist:
s1 = result(board, size, move, player)
compressed = compressing(s1)
if compressed in max_closed_list:
return max_closed_list[compressed][0], max_closed_list[compressed][1]
new_value, new_move = alphabeta(s1, size, player, opponent, depth - 1, alpha, beta, False, move)
if value <= new_value:
value = new_value
final_move = move
alpha = value if value > alpha else alpha
# alpha = max(alpha, value)
max_closed_list[compressed] = alpha, final_move
if alpha >= beta:
break
return value, final_move
else:
value = 10000000
final_move = None
movelist = get_legal_moves(board, size, opponent)
if len(movelist) < 1:
value = heuristic(board, size, player)
for move in movelist:
s1 = result(board, size, move, opponent)
compressed = compressing(s1)
if compressed in min_closed_list:
#print min_closed_list[compressed][0], min_closed_list[compressed][1]
return min_closed_list[compressed][0], min_closed_list[compressed][1]
new_value, new_move = alphabeta(s1, size, player, opponent, depth - 1, alpha, beta, True, move)
if value >= new_value:
value = new_value
final_move = move
beta = value if value < beta else beta
# beta = min(beta, value)
min_closed_list[compressed] = beta, final_move
if alpha >= beta:
break
return value, final_move
####################################################################
def get_move(board_size, board_state, turn, time_left, opponent_time_left):
if time_left > 140000:
return calculate(board_state, board_size, turn, 6)
elif time_left > 120000:
return calculate(board_state, board_size, turn, 4)
elif time_left > 80000:
return calculate(board_state, board_size, turn, 3)
else:
return calculate(board_state, board_size, turn, 2)
return (-1,-1)
####################################################################
if __name__ == '__main__':
size = int(input("Enter Size: "))
board = init_board(size)
print_board(board, size)
player = WHITE
play_game(board, size, player)
#######################################################################