-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_play_helpers.py
159 lines (127 loc) · 4.14 KB
/
game_play_helpers.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
import copy
import numpy as np
def create_board():
return [["⚪️" for i in range(7)] for i in range(6)]
def show_board(board):
template = ["1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 "]
print("\nCurrent Board State:")
print(template)
for i in range(len(board)):
print(board[i])
print("-------------------------------------------")
def convert_board_to_string(board, player):
y, r, w = "🟡", "🔴", "⚪️"
if player == r:
str_board = "R,"
elif player == y:
str_board = "Y,"
for row in board:
for cell in row:
if cell == w:
str_board += "W"
elif cell == r:
str_board += "R"
elif cell == y:
str_board += "Y"
return str_board
# def convert_string_to_board(string):
# y, r = "🟡", "🔴"
# board = create_board()
# pointer = 0
# string = string.split(",")[1]
# for i in range(len(board)):
# for j in range(len(board[i])):
# if string[pointer] == "W":
# pass
# elif string[pointer] == "R":
# board[i][j] = r
# elif string[pointer] == "Y":
# board[i][j] = y
#
# pointer += 1
# return board
def get_diagonals(board, bltr=True):
return_grid = [[] for total in range(12)]
for i in range(7):
for j in range(6):
if bltr:
return_grid[i + j].append(board[j][i])
else:
return_grid[(j - i) % 12].append(board[j][i])
return return_grid
def retrieve_diagonal(ind):
lst = []
if ind < 12:
for i in range(7):
for j in range(6):
if i + j == ind:
lst.append([j, i])
elif ind >= 12:
ind = ind - 12
for i in range(7):
for j in range(6):
if (j - i) % 12 == ind:
lst.append([j, i])
return lst
def check_win(board):
y, r, w = "🟡", "🔴", "⚪️"
diagonals = get_diagonals(board) + get_diagonals(board, False)
# check diagonals
for ind, diagonal in enumerate(diagonals):
if len(diagonal) >= 4:
for i in range(len(diagonal) - 3):
if diagonal[i:i + 4] == [r for i in range(4)]:
return True, r, retrieve_diagonal(ind)[i:i + 4]
if diagonal[i:i + 4] == [y for i in range(4)]:
return True, y, retrieve_diagonal(ind)[i:i + 4]
# check rows
for ind, row in enumerate(board):
for i in range(4):
if row[i:i + 4] == [r for i in range(4)]:
return True, r, [[ind, i + j] for j in range(4)]
if row[i:i + 4] == [y for i in range(4)]:
return True, y, [[ind, i + j] for j in range(4)]
# check columns
board_t = np.transpose(np.array(board))
for ind, col in enumerate(board_t):
for i in range(3):
if list(col)[i:i + 4] == [r for i in range(4)]:
return True, r, [[i + j, ind] for j in range(4)]
if list(col)[i:i + 4] == [y for i in range(4)]:
return True, y, [[i + j, ind] for j in range(4)]
return False, None, None
def show_win(board):
win, player, pos = check_win(board)
dummy = copy.deepcopy(board)
if win:
for row, col in pos:
dummy[row][col] = "✅ "
show_board(dummy)
def col_full(board, col):
w = "⚪️"
for i in range(6):
if board[i][col] == w:
return False
return True
def full_board(board):
for col in range(7):
if not col_full(board, col):
return False
return True
def make_move(board, player, col):
y, r, w = "🟡", "🔴", "⚪️"
dummy = copy.deepcopy(board)
if not col_full(dummy, col) and (player == r or player == y):
for i in range(5, -1, -1):
if dummy[i][col] == w:
dummy[i][col] = player
break
return dummy
else:
return dummy
def possible_moves(board):
moves = []
for col in range(7):
if not col_full(board, col):
moves.append(col)
return moves