-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckers.py
554 lines (512 loc) · 21.5 KB
/
checkers.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
from math import inf
from time import time
from copy import deepcopy
from console_color import Color
class State(object):
def __init__(self, board, move=None):
self.board = board
self.move = move
def get_children(self, mandatory_jump, player_turn):
current_state = deepcopy(self.board)
children = []
available_moves = Checkers.find_moves(current_state, mandatory_jump, player_turn)
for i in range(len(available_moves)):
state = deepcopy(current_state)
Checkers.make_a_move(state, available_moves[i])
children.append(State(state, available_moves[i]))
return children
def get_board(self):
return self.board
def set_board(self, board):
self.board = board
def get_move(self):
return self.move
def set_move(self, move):
self.move = move
class Checkers(object):
def __init__(self):
self.matrix = [[], [], [], [], [], [], [], []]
self.player_pieces = 12
self.computer_pieces = 12
self.player_turn = True
self.mandatory_jump = False
for row in self.matrix:
for i in range(8):
row.append(" . ")
self.position_computer()
self.position_player()
def position_computer(self):
for i in range(3):
for j in range(8):
if (i + j) % 2 == 0:
self.matrix[i][j] = "b" + str(i) + str(j)
def position_player(self):
for i in range(5, 8):
for j in range(8):
if (i + j) % 2 == 0:
self.matrix[i][j] = "w" + str(i) + str(j)
@staticmethod
def print_matrix(board, player_turn, mandatory_jump):
available_moves = []
if player_turn:
available_moves = Checkers.find_moves(board, mandatory_jump, True)
k = 0
print()
for i in range(8):
if i == 0:
print(" 0", end=" ")
else:
print(i, end=" ")
print()
for row in board:
print(k, end=" |")
k += 1
j = 0
for elem in row:
j += 1
if player_turn and Checkers.is_movable(board, k - 1, j - 1, available_moves):
print(Color.BLUE + elem + Color.RESET, end=" ")
else:
print(elem, end=" ")
print("| ", k - 1)
for i in range(8):
if i == 0:
print(" 0", end=" ")
else:
print(i, end=" ")
print("\n")
@staticmethod
def print_second_matrix(board, moves, pawn):
k = 0
print()
for i in range(8):
if i == 0:
print(" 0", end=" ")
else:
print(i, end=" ")
print()
for row in board:
print(k, end=" |")
k += 1
j = 0
for elem in row:
j += 1
if pawn in elem:
print(Color.BLUE + pawn + Color.RESET, end=" ")
else:
for move in moves:
if move[3] == k - 1 and move[4] == j - 1:
print(Color.PURPLE_BACKGROUND + str(k - 1) + str(j - 1) + Color.RESET, end=" ")
break
else:
print(elem, end=" ")
print("| ", k - 1)
for i in range(8):
if i == 0:
print(" 0", end=" ")
else:
print(i, end=" ")
print("\n")
@staticmethod
def is_movable(board, m, n, available_moves):
for move in available_moves:
if board[m][n] == move[0]:
return True
return False
@staticmethod
def get_depth(available_moves):
if len(available_moves) == 1:
return 0
elif len(available_moves) > 6:
return 3
else:
return 5
def computer_move(self):
t1 = time()
current_state = State(self.matrix)
available_moves = current_state.get_children(self.mandatory_jump, False)
if len(available_moves) == 0:
print(Color.GREEN + "Computer has no moves available! YOU WIN!" + Color.RESET)
game_over()
depth = Checkers.get_depth(available_moves)
dictionary = {}
for i in range(len(available_moves)):
child = available_moves[i]
value = Checkers.minimax(child.get_board(), -inf, inf, depth, False, self.mandatory_jump)
dictionary[value] = child
new_board = dictionary[max(dictionary.keys())].get_board()
self.matrix = new_board
t2 = time()
time_taken = t2 - t1
move = dictionary[max(dictionary.keys())].get_move()
print("Computer moved ", move[0], " from ", move[1], move[2], " to ", move[3], move[4])
if abs(move[1] - move[3]) >= 2:
self.player_pieces -= abs(move[1] - move[3]) // 2
print(Color.BOLD + "Computer took your pawn!" + Color.RESET)
print("The move took %.6f seconds" % time_taken)
def player_move(self):
available_moves = Checkers.find_moves(self.matrix, self.mandatory_jump, self.player_turn)
if len(available_moves) == 0:
print(Color.RED + "No moves available! YOU LOSE!" + Color.RESET)
game_over()
move = Checkers.choose_move(self.matrix, available_moves, False)
pawn = move[0]
move = move[1]
for moves in available_moves:
if pawn in moves[0] and move == str(moves[3]) + str(moves[4]):
Checkers.make_a_move(self.matrix, moves)
if Checkers.is_jump(available_moves, move, pawn):
self.computer_pieces -= 1
while True:
jump = Checkers.double_jump(self.matrix, moves, self.mandatory_jump)
if jump:
Checkers.make_a_move(self.matrix, jump)
self.computer_pieces -= 1
continue
break
break
@staticmethod
def double_jump(board, move, mandatory_jump):
available_jumps = []
if Checkers.available_jump(board, "w", move[3], move[4],
move[3] - 1, move[4] - 1, move[3] - 2, move[4] - 2):
available_jumps.append([board[move[3]][move[4]], move[3], move[4], move[3] - 2, move[4] - 2])
if Checkers.available_jump(board, "w", move[3], move[4],
move[3] - 1, move[4] + 1, move[3] - 2, move[4] + 2):
available_jumps.append([board[move[3]][move[4]], move[3], move[4], move[3] - 2, move[4] + 2])
if board[move[3]][move[4]][0] == "W":
if Checkers.available_jump(board, "w", move[3], move[4],
move[3] + 1, move[4] - 1, move[3] + 2, move[4] - 2):
available_jumps.append([board[move[3]][move[4]], move[3], move[4], move[3] + 2, move[4] - 2])
if Checkers.available_jump(board, "w", move[3], move[4],
move[3] + 1, move[4] + 1, move[3] + 2, move[4] + 2):
available_jumps.append([board[move[3]][move[4]], move[3], move[4], move[3] + 2, move[4] + 2])
if len(available_jumps) == 0:
return False
state = deepcopy(board)
Checkers.print_second_matrix(state, available_jumps, str(move[3]) + str(move[4]))
if not mandatory_jump:
while True:
choice = input("Do you want to make another jump? [Y/N]: ")
if choice.lower() == "y":
break
elif choice.lower() == "n":
return False
elif choice == "":
print(Color.YELLOW + "Game ended!" + Color.RESET)
game_over()
elif choice.lower() == "s":
while True:
choice = input("Are you sure you want to surrender? [Y/N]: ")
if choice.lower() == "y":
print(Color.RED + "You surrendered! Coward move..." + Color.RESET)
game_over()
elif choice.lower() == "n":
break
else:
print(Color.BOLD + "Invalid choice! Please try again." + Color.RESET)
else:
print(Color.BOLD + "Invalid choice! Please try again." + Color.RESET)
pawn = str(move[3]) + str(move[4])
jump = Checkers.choose_move(board, available_jumps, True)
for moves in available_jumps:
if pawn == str(moves[1]) + str(moves[2]) and jump == str(moves[3]) + str(moves[4]):
return moves
@staticmethod
def is_jump(available_moves, move, pawn):
for moves in available_moves:
if pawn in moves[0] and move == str(moves[3]) + str(moves[4]):
if abs(moves[1] - moves[3]) == 2:
return True
return False
@staticmethod
def choose_pawn(board, available_moves):
while True:
pawn = input("Choose a pawn you want to move [format [row column] no spaces]: ")
if Checkers.is_valid_pawn(available_moves, pawn):
break
if pawn == "":
print(Color.YELLOW + "Game ended!" + Color.RESET)
game_over()
new_board = deepcopy(board)
pawn_moves = []
for moves in available_moves:
if pawn in moves[0]:
pawn_moves.append(moves)
Checkers.print_second_matrix(new_board, pawn_moves, pawn)
return pawn
@staticmethod
def choose_move(board, available_moves, double_jump):
pawn = None
while True:
if not double_jump:
pawn = Checkers.choose_pawn(board, available_moves)
while True:
move = input("Choose a move [format [row column] no spaces] [x to choose another pawn]: ")
if Checkers.is_valid_move(available_moves, move):
break
if move.lower() == "x":
Checkers.print_matrix(board, True, False)
continue
break
if double_jump:
return move
return [pawn, move]
@staticmethod
def is_valid_pawn(available_moves, pawn):
for moves in available_moves:
if len(pawn) == 2 and pawn in moves[0]:
return True
else:
if Checkers.is_enter(pawn):
return True
if Checkers.is_surrender(pawn):
if Checkers.to_surrender():
game_over()
return False
print(Color.BOLD + "Invalid choice! Please try again." + Color.RESET)
return False
@staticmethod
def is_valid_move(available_moves, move):
for moves in available_moves:
if move == str(moves[3]) + str(moves[4]):
return True
else:
if Checkers.is_enter(move):
print(Color.YELLOW + "Game ended!" + Color.RESET)
game_over()
if Checkers.is_surrender(move):
if Checkers.to_surrender():
game_over()
return False
if Checkers.is_exchange(move):
return True
print(Color.BOLD + "Invalid choice! Please try again." + Color.RESET)
return False
@staticmethod
def is_enter(prompt):
return prompt == ""
@staticmethod
def is_surrender(prompt):
return prompt.lower() == "s"
@staticmethod
def to_surrender():
while True:
choice = input("Are you sure you want to surrender? [Y/N]: ")
if choice.lower() == "y":
print(Color.RED + "You surrendered! Coward move..." + Color.RESET)
return True
elif choice.lower() == "n":
return False
else:
print(Color.BOLD + "Invalid choice! Please try again." + Color.RESET)
@staticmethod
def is_exchange(prompt):
return prompt.lower() == "x"
@staticmethod
def find_moves(board, mandatory_jump, player_turn):
if player_turn:
letter = "w"
else:
letter = "b"
available_moves = []
available_jumps = []
for m in range(8):
for n in range(8):
if (player_turn and board[m][n][0].lower() == "w") or (not player_turn and board[m][n][0] == "B"):
if Checkers.available_move(board, letter, m, n, m - 1, n + 1):
available_moves.append([board[m][n], m, n, m - 1, n + 1])
if Checkers.available_move(board, letter, m, n, m - 1, n - 1):
available_moves.append([board[m][n], m, n, m - 1, n - 1])
if Checkers.available_jump(board, letter, m, n, m - 1, n + 1, m - 2, n + 2):
available_jumps.append([board[m][n], m, n, m - 2, n + 2])
if Checkers.available_jump(board, letter, m, n, m - 1, n - 1, m - 2, n - 2):
available_jumps.append([board[m][n], m, n, m - 2, n - 2])
if (player_turn and board[m][n][0] == "W") or (not player_turn and board[m][n][0].lower() == "b"):
if Checkers.available_move(board, letter, m, n, m + 1, n - 1):
available_moves.append([board[m][n], m, n, m + 1, n - 1])
if Checkers.available_move(board, letter, m, n, m + 1, n + 1):
available_moves.append([board[m][n], m, n, m + 1, n + 1])
if Checkers.available_jump(board, letter, m, n, m + 1, n - 1, m + 2, n - 2):
available_jumps.append([board[m][n], m, n, m + 2, n - 2])
if Checkers.available_jump(board, letter, m, n, m + 1, n + 1, m + 2, n + 2):
available_jumps.append([board[m][n], m, n, m + 2, n + 2])
if mandatory_jump is False:
available_moves += available_jumps
return available_moves
else:
if len(available_jumps) == 0:
return available_moves
return available_jumps
@staticmethod
def available_move(board, letter, m, n, new_m, new_n):
if new_m < 0 or new_m > 7 or new_n < 0 or new_n > 7:
return False
if board[m][n][0].lower() != letter:
return False
if board[new_m][new_n] != " . ":
return False
return True
@staticmethod
def available_jump(board, letter, m, n, by_m, by_n, new_m, new_n):
if letter.lower() == "w":
opponent_letter = "b"
else:
opponent_letter = "w"
if new_m < 0 or new_m > 7 or new_n < 0 or new_n > 7:
return False
if board[m][n][0].lower() != letter:
return False
if board[by_m][by_n][0].lower() != opponent_letter:
return False
if board[new_m][new_n] != " . ":
return False
return True
@staticmethod
def make_a_move(board, move):
letter = board[move[1]][move[2]][0]
if letter == "w" and move[3] == 0:
letter = "W"
elif letter == "b" and move[3] == 7:
letter = "B"
board[move[3]][move[4]] = letter + move[0][1] + move[0][2]
if abs(move[1] - move[3]) == 2:
board[(move[1] + move[3]) // 2][(move[2] + move[4]) // 2] = " . "
board[move[1]][move[2]] = " . "
def play(self):
print(Color.PURPLE + "Welcome to Checkers!" + Color.RESET)
print("""
First, a few rules:
1. You select pieces by typing it's name without the letter 'w' or 'b'
2. You move pieces by typing the row and column of the destination
3. You can quit the game at any moment by pressing ENTER
4. You can surrender the game at any moment by pressing 's'
""")
while True:
choice = input("Is jumping mandatory? [Y/N]: ")
if choice.lower() == "y":
self.mandatory_jump = True
break
elif choice.lower() == "n":
self.mandatory_jump = False
break
elif choice == "":
print(Color.YELLOW + "Game ended!" + Color.RESET)
game_over()
elif choice.lower() == "s":
while True:
choice = input("Are you sure you want to surrender? [Y/N]: ")
if choice.lower() == "y":
print(Color.RED + "You surrendered before the game even started!" + Color.RESET)
game_over()
elif choice.lower() == "n":
break
else:
print(Color.BOLD + "Invalid choice! Please try again." + Color.RESET)
else:
print(Color.BOLD + "Invalid choice! Please try again." + Color.RESET)
while True:
Checkers.print_matrix(self.matrix, self.player_turn, self.mandatory_jump)
print("MY PIECES: %d\tCOMPUTER'S PIECES: %d" % (self.player_pieces, self.computer_pieces))
if self.player_turn:
print(Color.PURPLE + "Player's turn!" + Color.RESET)
self.player_move()
else:
print(Color.PURPLE + "Computer's turn!" + Color.RESET)
print("Thinking...")
self.computer_move()
if self.player_pieces == 0:
print(Color.RED + "You have no pieces left! YOU LOSE!" + Color.RESET)
game_over()
elif self.computer_pieces == 0:
print(Color.GREEN + "Computer has no pieces left! YOU WIN!" + Color.RESET)
game_over()
self.player_turn = not self.player_turn
@staticmethod
def minimax(board, alfa, beta, depth, maximizing_player, mandatory_jump):
if depth == 0:
return Checkers.heuristic(board)
current_state = State(board)
if maximizing_player:
max_eval = -inf
for child in current_state.get_children(mandatory_jump, True):
ev = Checkers.minimax(child.get_board(), alfa, beta, depth - 1, False, mandatory_jump)
max_eval = max(max_eval, ev)
alfa = max(alfa, ev)
if beta <= alfa:
break
return max_eval
else:
min_eval = inf
for child in current_state.get_children(mandatory_jump, False):
ev = Checkers.minimax(child.get_board(), alfa, beta, depth - 1, True, mandatory_jump)
min_eval = min(min_eval, ev)
beta = min(beta, ev)
if beta <= alfa:
break
return min_eval
@staticmethod
def heuristic(board):
result = 0
computer = 0
opponent = 0
for i in range(8):
for j in range(8):
if board[i][j][0].lower() == "w":
opponent += 1
elif board[i][j][0] == "W":
opponent += 5
else:
computer += 1
if board[i][j][0] == "b":
result += 5
if board[i][j][0] == "B":
result += 10
if i == 0 or i == 7 or j == 0 or j == 7:
result += 10
if 3 <= i <= 4 and 2 <= j <= 5:
if board[i][j][0].lower() == "b":
result += 5
if board[i][j][0].lower() == "w":
result -= 5
if 3 <= i <= 4 and (j < 2 or j > 5):
if board[i][j][0].lower() == "b":
result += 3
if board[i][j][0].lower() == "w":
result -= 3
if i + 1 > 7:
continue
if j + 1 > 7:
continue
if board[i][j][0].lower() == "b" and board[i + 1][j + 1][0].lower() == "b":
result += 3
if j - 1 < 0:
continue
if board[i][j][0].lower() == "b" and board[i + 1][j - 1][0].lower() == "b":
result += 3
if i - 1 < 0:
continue
if board[i][j][0].lower() == "b" and board[i - 1][j + 1][0].lower() == " . ":
result -= 3
if board[i + 1][j - 1][0].lower() == "w":
result -= 3
if board[i][j][0].lower() == "b" and board[i - 1][j - 1][0].lower() == " . ":
result -= 3
if board[i + 1][j + 1][0].lower() == "w":
result -= 3
return result + (computer - opponent) * 1000
def game_over():
while True:
new_game = input(Color.BOLD + "Do you want to play again? (Y/N): " + Color.RESET)
if new_game.lower() == "y":
main()
elif new_game.lower() == "n":
print(Color.YELLOW + "Thanks for playing! :)" + Color.RESET)
exit()
else:
print(Color.BOLD + "Invalid choice! Please try again." + Color.RESET)
def main():
game = Checkers()
game.play()
if __name__ == "__main__":
main()