-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.py
More file actions
77 lines (66 loc) · 2.28 KB
/
Copy pathBoard.py
File metadata and controls
77 lines (66 loc) · 2.28 KB
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
from enum import IntEnum
import numpy as np
class Color(IntEnum):
NONE = 0
RED = 1
YELLOW = 2
class Board:
def __init__(self):
self.cells = np.zeros([6, 7], dtype=np.int)
self._winner = Color.NONE
@property
def winner(self):
return self._winner
colorsWinSeq = {1: [1] * 4, 2: [2] * 4}
def check_for_4(self, arr: np.array, color):
# return Board.colorsWinSeq[color] in arr.tolist()
#
# if np.count_nonzero(arr == color) < 4:
# return False
# arr = arr[arr != 0]
repeated_in_row = 0
arr_len = len(arr)
for ind in range(arr_len):
if (arr_len - ind + repeated_in_row) < 4:
return False
if arr[ind] != color:
repeated_in_row = 0
continue
repeated_in_row += 1
if repeated_in_row == 4:
self._winner = Color(color)
return True
return False
def print_board(self):
print(np.matrix(self.cells))
def __check_for_winner(self, row_id, col_id, color: Color):
color_int = self.cells[row_id, col_id]
row = self.cells[row_id, :]
if self.check_for_4(row, color_int):
return True
col = self.cells[:, col_id]
if self.check_for_4(col, color_int):
return True
diag1 = np.diag(self.cells, k=col_id - row_id)
if self.check_for_4(diag1, color_int):
return True
diag2 = np.diag(np.fliplr(self.cells), k=(6 - col_id) - row_id)
if self.check_for_4(diag2, color_int):
return True
return False
def add_to_col(self, col_id: int, color: Color):
if self._winner != Color.NONE:
return
col = self.cells[:, col_id]
free_in_col = np.where(col == int(Color.NONE))[0]
if len(free_in_col) == 0:
return
row_id = free_in_col[-1]
# if color == Color.RED:
self.cells[row_id, col_id] = color
# elif color == Color.YELLOW:
# self.cells[row_id, col_id] = color.YELLOW
return self.__check_for_winner(row_id, col_id, color)
def available_columns(self) -> list:
first_row = self.cells[0, :]
return np.where(first_row == int(Color.NONE))[0]