This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
517 lines (475 loc) · 23 KB
/
board.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
"""
Board file including general functions and the Board class
"""
# Importing needed modules
from typing import Tuple
import numpy as np
from copy import deepcopy
# STATICS (START)
ROWS: int = 8
COLUMNS: int = 8
col_enumerator: Tuple[str, str, str, str, str, str, str, str] = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')
row_enumerator: Tuple[str, str, str, str, str, str, str, str] = ('1', '2', '3', '4', '5', '6', '7', '8')
# STATICS (END)
def col_enum(index: int):
"""
Converts number to column letter using the col_enumerator variable
:param index: The index number of a move (list index value)
:type index: int
:return: The column name as in col_enumerator
:rtype: int
"""
if index < len(col_enumerator):
return col_enumerator[index]
return False
def row_enum(index: int):
"""
Converts number to row number using the row_enumerator variable
:param index: The index number of a move (list index value)
:type index: int
:return: The row number as in row_enumerator
:rtype: int
"""
if index < len(row_enumerator):
return row_enumerator[index]
return False
class Board:
"""
Class board including the following variables and functions:
-----VARIABLES-----
:var self.board: The list presenting the board
:type self.board: list
:var self.last_move: The latest move played
:type self.last_move: tuple
"""
def __init__(self, board: list = '', last_move: tuple = ''):
"""
__init__ function
:param board: a board list. If none give, then the board is created from initial game state
:type board: list
:param last_move: The latest move played. If none given then it is initialized to blank ('')
:type last_move: tuple
:return: Nothing
:rtype: N/A
"""
# Creating board
if board == '':
self.board: list = self.create_board()
else:
self.board: list = board
self.last_move: tuple = last_move # Assigning parameter to self variable
@staticmethod
def create_board():
"""
Creating board
:return: a list indicating a board
:rtype: list
"""
return [[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', 'L', 'D', ' ', ' ', ' '],
[' ', ' ', ' ', 'D', 'L', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']]
def set_board(self, move: tuple, value: str, valid_moves: list, game_history: list, actor_color: str):
"""
Setting board value according to input from user/AI
:param move: A move that we would like to display
:type move: tuple
:param value: The value of the player
:type value: str
:param valid_moves: A list with all the valid moves
:type valid_moves: list
:param game_history: a list including all the game history
:type game_history: list
:param actor_color: the value of the current player
:type actor_color: str
:return: The move and the game history
:rtype: tuple
"""
def move_exists():
"""
Helper function which checks if a move is a valid move passed as an argument
:return: True if a move is in the list valid moves otherwise false
:rtype: bool
"""
# Checking if a move is present in the valid moves
for valid_move in valid_moves:
if (move[0], move[1]) == valid_move:
return True
return False
# Checking if we are out of bounds
if move[0] > ROWS or move[1] > COLUMNS:
print("Your piece can't be placed in outside of the board. Invalid move.")
# Handling move
if move_exists(): # Checks if move is in the valid moves via helper function
self.board[move[0]][move[1]] = value # Adding the move on the board with the passed valie
if value == 'D' or value == 'L':
game_history.append((move[0], move[1], value)) # Adding the move in game history
self.last_move: tuple = (move[0], move[1]) # Change the latest move to current move
self.flip_opponent_pieces(move, actor_color) # Calling the function to flip opponent pieces
return move, game_history
else:
print("Your piece can't be placed in {}{}. It is not a valid move.".format(col_enum(move[1]),
row_enum(move[0])))
return False
def print_board(self):
"""
Print the current board state
:return: Nothing
:rtype: N/A
"""
def print_header():
"""
Helper function to print the headers
:return: Nothing
:rtype: N/A
"""
print(" ", end='')
# Printing letters
for letter in range(ord('A'), ord('I')):
print(chr(letter), end='')
print(" ", end='')
print_header() # Printing the header vial the helper function
# Looping through the board in order to add the values. Printing also the rows and seperators
for i in range(0, ROWS):
print("")
print(" -------------------------------------------------")
print(str(i + 1) + " ", end='')
for j in range(0, COLUMNS):
print("| ", self.board[i][j], " ", end='')
print("| ", end='')
print(str(i + 1) + " ", end='')
print("")
print(" -------------------------------------------------")
print_header() # Printing the footer
print()
print()
def flip_opponent_pieces(self, piece: tuple, actor_color: str):
"""
Flipping the opponent pieces after placing a new checker
:param piece: The piece which we are going to place on the board
:type piece: tuple
:param actor_color: The color of the current player (being human or pc)
:type actor_color: str
:return: The updated board list
:rtype: list
"""
# Checking who is who
if actor_color == 'L':
opponent_color: str = 'D'
else:
opponent_color: str = 'L'
# Finding darks and lights and adding the coordinates in set
matrix: np.ndarray = np.array(self.board)
player_pieces_: np.ndarray = np.where(matrix == actor_color)
player_pieces: set = set()
for i in range(0, len(player_pieces_[0])):
player_pieces.add((player_pieces_[0][i], player_pieces_[1][i]))
opponent_pieces_: np.ndarray = np.where(matrix == opponent_color)
opponent_pieces: set = set()
for i in range(0, len(opponent_pieces_[0])):
opponent_pieces.add((opponent_pieces_[0][i], opponent_pieces_[1][i]))
pieces_to_flip: list = [] # Storing the values of the pieces to flip color
# Looping through the rows and columns
for x in range(piece[1] - 1, -1, -1): # Going upwards
if (piece[0], x) in opponent_pieces: # If next piece is opponent's add it to the list
pieces_to_flip.append((piece[0], x))
elif self.board[piece[0]][x] == actor_color:
for single_piece in pieces_to_flip: # If next piece is actor's flip it
self.board[single_piece[0]][single_piece[1]]: list = actor_color
break
elif self.board[piece[0]][x] == ' ': # If next piece is blank
break
pieces_to_flip.clear() # Clearing list
for y in range(piece[0] - 1, -1, -1): # Going left
if (y, piece[1]) in opponent_pieces: # If next piece is opponent's add it to the list
pieces_to_flip.append((y, piece[1]))
elif self.board[y][piece[1]] == actor_color:
for single_piece in pieces_to_flip: # If next piece is actor's flip it
self.board[single_piece[0]][single_piece[1]]: list = actor_color
break
elif self.board[y][piece[1]] == ' ': # If next piece is blank
break
pieces_to_flip.clear() # Clearing list
for x in range(piece[1] + 1, 8): # Going downwards
if (piece[0], x) in opponent_pieces: # If next piece is opponent's add it to the list
pieces_to_flip.append((piece[0], x))
elif self.board[piece[0]][x] == actor_color:
for single_piece in pieces_to_flip: # If next piece is actor's flip it
self.board[single_piece[0]][single_piece[1]]: list = actor_color
break
elif self.board[piece[0]][x] == ' ': # If next piece is blank
break
pieces_to_flip.clear() # Clearing list
for y in range(piece[0] + 1, 8): # Going right
if (y, piece[1]) in opponent_pieces: # If next piece is opponent's add it to the list
pieces_to_flip.append((y, piece[1]))
elif self.board[y][piece[1]] == actor_color:
for single_piece in pieces_to_flip: # If next piece is actor's flip it
self.board[single_piece[0]][single_piece[1]]: list = actor_color
break
elif self.board[y][piece[1]] == ' ': # If next piece is blank
break
pieces_to_flip.clear() # Clearing list
# Finding diagonal
for y, x in zip(range(piece[0] + 1, 8), range(piece[1] + 1, 8)): # Going down right
if (y, x) in opponent_pieces: # If next piece is opponent's add it to the list
pieces_to_flip.append((y, x))
elif self.board[y][x] == actor_color:
for single_piece in pieces_to_flip:
self.board[single_piece[0]][single_piece[1]]: list = actor_color # If next piece is actor's flip it
break
elif self.board[y][x] == ' ': # If next piece is blank
break
pieces_to_flip.clear() # Clearing list
for y, x in zip(range(piece[0] - 1, -1, -1), range(piece[1] - 1, -1, -1)): # Going up left
if (y, x) in opponent_pieces: # If next piece is opponent's add it to the list
pieces_to_flip.append((y, x))
elif self.board[y][x] == actor_color:
for single_piece in pieces_to_flip:
self.board[single_piece[0]][single_piece[1]]: list = actor_color # If next piece is actor's flip it
break
elif self.board[y][x] == ' ': # If next piece is blank
break
pieces_to_flip.clear() # Clearing list
for y, x in zip(range(piece[0] - 1, -1, -1), range(piece[1] + 1, 8)): # Going upright
if (y, x) in opponent_pieces: # If next piece is opponent's add it to the list
pieces_to_flip.append((y, x))
elif self.board[y][x] == actor_color:
for single_piece in pieces_to_flip:
self.board[single_piece[0]][single_piece[1]]: list = actor_color # If next piece is actor's flip it
break
elif self.board[y][x] == ' ': # If next piece is blank
break
pieces_to_flip.clear() # Clearing list
for y, x in zip(range(piece[0] + 1, 8), range(piece[1] - 1, -1, -1)): # Going down left
if (y, x) in opponent_pieces: # If next piece is opponent's add it ot the list
pieces_to_flip.append((y, x))
elif self.board[y][x] == actor_color:
for single_piece in pieces_to_flip:
self.board[single_piece[0]][single_piece[1]]: list = actor_color # If next piece is actor's flip it
break
elif self.board[y][x] == ' ': # If next piece is blank
break
pieces_to_flip.clear() # Clearing list
return self.board
def find_moves(self, player_color: str):
"""
Finding all possible moves for current round (neighbours of checkers)
:param player_color: The color of the player
:type player_color: str
:return: all valid moves found
:rtype: list
"""
# Who is who
if player_color == 'L':
opponent_color: str = 'D'
else:
opponent_color: str = 'L'
# Finding darks and lights and adding the coordinates in tuple
matrix: np.ndarray = np.array(self.board)
player_pieces_: np.ndarray = np.where(matrix == player_color)
player_pieces: set = set()
for i in range(0, len(player_pieces_[0])):
player_pieces.add((player_pieces_[0][i], player_pieces_[1][i]))
opponent_pieces_: np.ndarray = np.where(matrix == opponent_color)
opponent_pieces: set = set()
for i in range(0, len(opponent_pieces_[0])):
opponent_pieces.add((opponent_pieces_[0][i], opponent_pieces_[1][i]))
valid_moves: list = []
diagonal: set = set()
# Finding valid moves (valid=being around opposite color). Checking also border condition
for piece in opponent_pieces:
if (piece[0], piece[1] + 1) not in opponent_pieces and (piece[0], piece[1] + 1) not in player_pieces and \
piece[1] != 7:
for x in range(piece[1] - 1, -1, -1):
if (piece[0], x) in player_pieces:
valid_moves.append((piece[0], piece[1] + 1))
elif self.board[piece[0]][x] == ' ':
break
if (piece[0] + 1, piece[1]) not in opponent_pieces and (piece[0] + 1, piece[1]) not in player_pieces and \
piece[0] != 7:
for y in range(piece[0] - 1, -1, -1):
if (y, piece[1]) in player_pieces:
valid_moves.append((piece[0] + 1, piece[1]))
elif self.board[y][piece[1]] == ' ':
break
if (piece[0], piece[1] - 1) not in opponent_pieces and (piece[0], piece[1] - 1) not in player_pieces and \
piece[1] != 0:
for x in range(piece[1] + 1, 8):
if (piece[0], x) in player_pieces:
valid_moves.append((piece[0], piece[1] - 1))
elif self.board[piece[0]][x] == ' ':
break
if (piece[0] - 1, piece[1]) not in opponent_pieces and (piece[0] - 1, piece[1]) not in player_pieces and \
piece[0] != 0:
for y in range(piece[0] + 1, 8):
if (y, piece[1]) in player_pieces:
valid_moves.append((piece[0] - 1, piece[1]))
elif self.board[y][piece[1]] == ' ':
break
# Finding diagonal
if (piece[0] - 1, piece[1] - 1) not in opponent_pieces and (
piece[0] - 1, piece[1] - 1) not in player_pieces and piece[0] != 0 and piece[1] != 0:
for y, x in zip(range(piece[0] + 1, 8), range(piece[1] + 1, 8)):
if (y, x) in player_pieces:
valid_moves.append((piece[0] - 1, piece[1] - 1))
diagonal.add((piece[0] - 1, piece[1] - 1))
elif self.board[y][x] == ' ':
break
if (piece[0] + 1, piece[1] + 1) not in opponent_pieces and (
piece[0] + 1, piece[1] + 1) not in player_pieces and piece[0] != 7 and piece[1] != 7:
for y, x in zip(range(piece[0] - 1, -1, -1), range(piece[1] - 1, -1, -1)):
if (y, x) in player_pieces:
valid_moves.append((piece[0] + 1, piece[1] + 1))
diagonal.add((piece[0] + 1, piece[1] + 1))
elif self.board[y][x] == ' ':
break
if (piece[0] + 1, piece[1] - 1) not in opponent_pieces and (
piece[0] + 1, piece[1] - 1) not in player_pieces and piece[0] != 7 and piece[1] != 0:
for y, x in zip(range(piece[0] - 1, -1, -1), range(piece[1] + 1, 8)):
if (y, x) in player_pieces:
valid_moves.append((piece[0] + 1, piece[1] - 1))
diagonal.add((piece[0] + 1, piece[1] - 1))
elif self.board[y][x] == ' ':
break
if (piece[0] - 1, piece[1] + 1) not in opponent_pieces and (
piece[0] - 1, piece[1] + 1) not in player_pieces and piece[0] != 0 and piece[1] != 7:
for y, x in zip(range(piece[0] + 1, 8), range(piece[1] - 1, -1, -1)):
if (y, x) in player_pieces:
valid_moves.append((piece[0] - 1, piece[1] + 1))
diagonal.add((piece[0] - 1, piece[1] + 1))
elif self.board[y][x] == ' ':
break
# Finding actual valid moves
for (row_v, col_v) in valid_moves.copy():
exists_in_player: bool = False
for (row, col) in player_pieces:
if col == col_v or row == row_v or (row_v, col_v) in diagonal:
exists_in_player: bool = True
if not exists_in_player:
valid_moves.remove((row_v, col_v))
return valid_moves
def get_children(self, player_color: str):
"""
Finding children (possible board states after possible moves) of board
:param player_color: The color of the player
:type player_color: str
:return: A list including children (board instances)
:rtype: list
"""
children: list = []
hr: list = []
valid_moves: list = self.find_moves(player_color)
for move in valid_moves:
child: Board = deepcopy(self)
child.set_board(move, player_color, valid_moves, hr, player_color)
children.append(child)
return children
def check_win_conditions(self, player_color: str, computer_color: str, print_on: bool):
"""
Check win conditions and returns win value
:param player_color: The color of the player
:type player_color: str
:param computer_color: The color of the computer
:type computer_color: str
:param print_on: Value to print or not
:type print_on: bool
:return: The value of the win condition (who won)
:rtype: str
"""
player_score: int = 0
computer_score: int = 0
for i in range(0, COLUMNS):
for j in range(0, ROWS):
if self.board[i][j] == player_color:
player_score += 1
elif self.board[i][j] == computer_color:
computer_score += 1
if print_on:
if player_score > computer_score:
print("Victory! Player wins with score {} over Computer's score {} !".format(player_score,
computer_score))
return 'p'
elif player_score < computer_score:
print(
"Defeat! Computer wins with score {} over Player's score {} !".format(computer_score,
player_score))
return 'c'
elif player_score == computer_score:
print("Draw! Player and Computer have equal score {} - {} !".format(player_score,
computer_score))
return '-'
elif player_score == 0:
print("Defeat! Computer wins with score {} over Player's score {} !".format(64,
player_score))
return 'c'
elif computer_score == 0:
print("Victory! Player wins with score {} over Computer's score {} !".format(64,
computer_score))
return 'p'
else:
return 0
def evaluate(self, computer_color: str):
"""
Evaluates the current board
:param computer_color: The color of the computer
:type computer_color:
:return: The value of the evaluate function
:rtype: int
"""
board_value: int = 0
computer_value: int = 1
player_value: int = -1
# Who is who
if computer_color == 'L':
player_color: str = 'D'
else:
player_color: str = 'L'
# Iter through the board tiles
for row in range(0, ROWS):
for column in range(0, COLUMNS):
evaluation = 1
# Adj
if (row == 0 and column == 1) or (row == 1 and 0 <= column <= 1):
if self.board[0][0] == computer_color:
evaluation = 5
else:
evaluation = -5
elif (row == 0 and column == 6) or (row == 1 and 6 <= column <= 7):
if self.board[0][7] == computer_color:
evaluation = 5
else:
evaluation = -5
elif (row == 7 and column == 1) or (row == 6 and 0 <= column <= 1):
if self.board[7][0] == computer_color:
evaluation = 5
else:
evaluation = -5
elif (row == 7 and column == 6) or (row == 6 and 6 <= column <= 7):
if self.board[7][7] == computer_color:
evaluation = 5
else:
evaluation = -5
# Side apart from adj and corners
elif (row == 0 and 1 < column < 6) or (row == 7 and 1 < column < 6) or (column == 0 and 1 < row < 6) or \
(column == 7 and 1 < row < 6):
evaluation = 5
# Corners
elif (row == 0 and column == 0) or (row == 0 and column == 7) or (row == 7 and column == 0) or \
(row == 7 and column == 7):
evaluation = 25
# Adding/removing according to player
if self.board[row][column] == computer_color:
board_value += evaluation
elif self.board[row][column] == player_color:
board_value -= evaluation
# In case of game win add 1000 to the winner value
if self.check_win_conditions(player_color, computer_color, False) == 'p':
board_value = board_value + (player_value * 1000)
elif self.check_win_conditions(player_color, computer_color, False) == 'c':
board_value = board_value + (computer_value * 1000)
return board_value