-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_calculation.py
209 lines (180 loc) · 6.06 KB
/
game_calculation.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
import numpy.core.multiarray as nparray
from game_constants import *
# return a list of options player in turn can play
# each option is a tuple of two positions, which are also tuples.
# first position is the option and the second one is the cell that leads to that options.
# second position helps to flip cells if the option is played
def calculate_options(turn: int, board: nparray):
options = []
for r in range(7):
for c in range(7):
if board[r, c] != turn:
continue
options = options + calculate_cell_options(turn, board, [r, c])
return options
# calculate options for each cell
def calculate_cell_options(turn: int, board: nparray, position: list):
row, column = position
opponent = toggle_turn(turn)
options = []
# up
count = 0
for i in range(row - 1, -1, -1):
each = board[i, column]
if each == CELL_EMPTY and count == 0:
break
elif each == opponent:
count += 1
elif each == turn:
break
else:
options.append(((i, column), (row, column)))
break
# down
count = 0
for i in range(row + 1, 8, 1):
each = board[i, column]
if each == CELL_EMPTY and count == 0:
break
elif each == opponent:
count += 1
elif each == turn:
break
else:
options.append(((i, column), (row, column)))
break
# left
count = 0
for i in range(column - 1, -1, -1):
each = board[row, i]
if each == CELL_EMPTY and count == 0:
break
elif each == opponent:
count += 1
elif each == turn:
break
else:
options.append(((row, i), (row, column)))
break
# right
count = 0
for i in range(column + 1, 8, 1):
each = board[row, i]
if each == CELL_EMPTY and count == 0:
break
elif each == opponent:
count += 1
elif each == turn:
break
else:
options.append(((row, i), (row, column)))
break
# up - left | both decreasing
count = 0
for i in range(1, min(row, column) + 1, 1):
each = board[row - i, column - i]
if each == CELL_EMPTY and count == 0:
break
elif each == opponent:
count += 1
elif each == turn:
break
else:
options.append(((row - i, column - i), (row, column)))
break
# up - right | row decreasing, column increasing
count = 0
for i in range(1, min(row, 7 - column) + 1, 1):
each = board[row - i, column + i]
if each == CELL_EMPTY and count == 0:
break
elif each == opponent:
count += 1
elif each == turn:
break
else:
options.append(((row - i, column + i), (row, column)))
break
# down - right | both increasing
count = 0
for i in range(1, min(7 - row, 7 - column) + 1, 1):
each = board[row + i, column + i]
if each == CELL_EMPTY and count == 0:
break
elif each == opponent:
count += 1
elif each == turn:
break
else:
options.append(((row + i, column + i), (row, column)))
break
# down - left | row increasing, column decreasing
count = 0
for i in range(1, min(7 - row, column) + 1, 1):
each = board[row + i, column - i]
if each == CELL_EMPTY and count == 0:
break
elif each == opponent:
count += 1
elif each == turn:
break
else:
options.append(((row + i, column - i), (row, column)))
break
return options
# flip cells in directions which has the given option at one side of it.
def flip_after_play(turn: int, board: nparray, options: list, option: tuple):
count = 0
for each in options:
if option != each[0]:
continue
x_src, y_src = option
x_dest, y_dest = each[1]
# up
if x_src > x_dest and y_src == y_dest:
count += x_src - x_dest - 1
for i in range(x_src - 1, x_dest, -1):
board[i, y_src] = turn
# down
if x_src < x_dest and y_src == y_dest:
count += x_dest - x_src - 1
for i in range(x_src + 1, x_dest, 1):
board[i, y_src] = turn
# left
if x_src == x_dest and y_src > y_dest:
count += y_src - y_dest - 1
for i in range(y_src - 1, y_dest, -1):
board[x_src, i] = turn
# right
if x_src == x_dest and y_src < y_dest:
count += y_dest - y_src - 1
for i in range(y_src + 1, y_dest, 1):
board[x_src, i] = turn
# NOTE: In flipping diagonally, we can only flip disks to the destination disk.
# Hence, the source and destination subtraction on the x-axis, gives us the max length that we can flip
# between source and destination disks.
# up left | both decreasing
if x_src > x_dest and y_src > y_dest:
length = x_src - x_dest
count += length - 1
for i in range(1, length):
board[x_src - i, y_src - i] = turn
# up right | row decreasing, column increasing
if x_src > x_dest and y_src < y_dest:
length = x_src - x_dest
count += length - 1
for i in range(1, length):
board[x_src - i, y_src + i] = turn
# down left | row increasing, column decreasing
if x_src < x_dest and y_src > y_dest:
length = x_dest - x_src
count += length - 1
for i in range(1, length):
board[x_src + i, y_src - i] = turn
# down right | both increasing
if x_src < x_dest and y_src < y_dest:
length = x_dest - x_src
count += length - 1
for i in range(1, length):
board[x_src + i, y_src + i] = turn
return count