-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.py
350 lines (297 loc) · 11.8 KB
/
logic.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
import os
import pandas as pd
from matplotlib import pyplot as plt
class Game:
def __init__(self):
self.player_names = ['player 0', 'player 1']
self.winner = None
# create the gobblers
number_of_gobblers = 6
self.gobblers = []
for player in range(2): # number of players
size = 1
for gobbler in range(number_of_gobblers):
gobbler = Gobbler(player, size)
self.gobblers.append(gobbler)
size += 1
# initialize some values
self.current_player_idx = 0
self.selected_gobbler = None
# used for keeping track of gobblers on board
self.board = [[] for n in range(9)]
self.winning_combinations = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[6,4,2],
]
def select_gobbler(self, gobbler_size: int) -> bool:
# Don't allow any more plays if the game is over
if self.winner is not None:
return False
# don't allow the player to select a gobbler if one is
# already selected
if self.selected_gobbler:
return False
# convert to integer and check if the value is valid (between 0 and 5)
gobbler_idx = self._convert_input(gobbler_size, 0, 5)
# if the value is invalid, return False
if gobbler_idx is None:
return False
# gobblers can only be selected if they are on top
gobbler = [g for g in self.gobblers if g.player == self.current_player_idx][gobbler_idx]
if not gobbler.is_on_top:
return False
# if no exclusionary conditions are met, select
# the indicated gobbler
self.selected_gobbler = gobbler
# remove gobbler from its previous position, if it has one
if self.selected_gobbler.board_position is not None:
del self.board[self.selected_gobbler.board_position][-1]
# update the gobbler's board position
self.selected_gobbler.board_position_previous = self.selected_gobbler.board_position
self.selected_gobbler.board_position = None
# update the is_on_top flag for all gobblers
self._update_on_top()
return True
def place_selected_gobbler(self, board_position: int) -> bool:
# if there is no selected gobbler, then there is
# nothing to place
if not self.selected_gobbler:
return False, None
# convert to integer and check if the value is valid (between 0 and 8)
board_position = self._convert_input(board_position, 0, 8)
# don't allow the player to put the gobbler back where they got it
if self.selected_gobbler.board_position_previous == board_position:
return False, None
# if the value is invalid, return False
if board_position is None:
return False, None
# if there is already a gobbler on the indicated board
# position, and it is bigger than the selected gobbler,
# cannot place the selected gobbler
if self.board[board_position] and \
self.selected_gobbler.size <= self.board[board_position][-1].size:
return False, None
# add the gobbler to the board
self.board[board_position].append(self.selected_gobbler)
# update the gobbler's board position
self.selected_gobbler.board_position = board_position
# update the is_on_top flag for all gobblers
self._update_on_top()
# toggle the current player
self.current_player_idx = int(not self.current_player_idx)
# deselect gobbler
self.selected_gobbler = None
return True, self._check_for_winner()
def set_player_names(self, player_names: list) -> list[bool, str]:
player_name_0 = player_names[0]
player_name_1 = player_names[1]
name_len_requirement = 3
if len(player_name_0) < name_len_requirement or len(player_name_1) < name_len_requirement:
return False, f'Player names must be at least {name_len_requirement} characters long.'
elif player_name_0 == player_name_1:
return False, 'Player names cannot be identical.'
else:
self.player_names = player_names
return True, 'Let the games begin!'
def represent_board(self) -> str:
"""
returns a string representation of the board
used for playing the game
"""
str_repr = ''
n = 0
for cell in self.board:
if cell:
player = cell[-1].player
size = cell[-1].size
str_to_add = f'|{size}({player})'
else:
str_to_add = f'|____'
str_repr += str_to_add
n += 1
if n > 2:
str_repr += '|\n'
n = 0
str_repr += '-----------------------'
return str_repr
def _convert_input(self, value: int, minimum: int, maximum: int) -> int:
"""
validates and converts user input into correct data type
if the value is invalid, returns None
value: the user-provided value
minimum: the expected minimum value
maximum: the expected maximum value
"""
# convert to an integer
try:
value = int(value)
except ValueError:
return None
# convert from a 1-x range to a 0-x range
value = value - 1
# check if the value is within the acceptable range
if minimum <= value <= maximum:
return value
else:
return None
def _check_for_winner(self) -> int:
"""
checks if there is a winner
if so, returns winner (int)
if not, returns None
"""
# check all of the winning combinations for a winner
for combo in self.winning_combinations:
# initialize a list to keep track of the
# owner of each piece
result_to_check = []
for position in combo:
if self.board[position]:
# record the player of the current piece in a list
result_to_check.append(self.board[position][-1].player)
else:
result_to_check.append(None)
# if there is only one non-None unique value, then
# we have a winner
unique_values = list(set(result_to_check))
if len(unique_values) == 1 and unique_values[0] is not None:
self.winner = unique_values[0]
return self.winner
return None
def _update_on_top(self):
"""
checks all of the gobblers on the board and
updates their is_on_top flag
"""
for position in self.board:
for n, gobbler in enumerate(position):
if len(position) - 1 == n:
gobbler.is_on_top = True
else:
gobbler.is_on_top = False
@property
def winner_name(self) -> str:
if self.winner is None:
return None
else:
return self.player_names[self.winner]
@property
def current_player_name(self) -> str:
return self.player_names[self.current_player_idx]
class Gobbler:
def __init__(self, player: int, size: int):
self.player = player # integer 0-1
self.size = size # integers 0-5
self.board_position = None # integers 0-8 or None
self.board_position_previous = None # so that it can be placed back where it came from
self.is_on_top = True
class GameStats:
def __init__(self):
self.num_turns = 0
self.player = 0
self.moves = [[], # a list of lists, one for each player
[],]
def record_move(self, gobbler_size:int, board_position:int) -> None:
self.moves[self.player].append(f'{gobbler_size} to {board_position}')
self.num_turns = len(self.moves[0])
self.player = int(not self.player)
def save(self, winner) -> None:
"""
Save the stats to csv and
save some images.
"""
self.write_to_csv(winner)
self.read_stats_from_csv()
self._save_charts()
def write_to_csv(self, winner) -> None:
data_to_record = {
'winner': winner,
'first_move_0': self.moves[0][0],
'first_move_1': self.moves[1][0],
'last_move_0': self.moves[0][-1],
'last_move_1': self.moves[1][-1],
'first_move_winner': self.moves[winner][0],
'last_move_winner': self.moves[winner][-1],
'num_turns': self.num_turns,
}
path = 'stats.csv'
column_headers = ''
line_to_write = ''
file_is_new = not os.path.exists(path)
for k, v in data_to_record.items():
if file_is_new:
column_headers += f'{k},'
line_to_write += f'{v},'
# add linebreaks
column_headers += '\n'
line_to_write += '\n'
# write the column headers
if file_is_new:
with open(path, 'a') as f:
f.write(column_headers)
# write the line
with open(path, 'a') as f:
f.write(line_to_write)
def read_stats_from_csv(self) -> pd.DataFrame:
self.stats = pd.read_csv('stats.csv')
return self.stats
def _save_charts(self) -> None:
"""
saves all of the bar charts
"""
chart_funcs = [self._get_winner_bar_chart,
self._get_num_turns_chart,
self._get_successful_opening_moves_bar_chart]
for func in chart_funcs:
func()
def _get_winner_bar_chart(self) -> None:
"""
Given the stats of all previously recorded games,
generate a bar chart that shows the winner breakdown
"""
value_counts = self.stats['winner'].value_counts()
try:
won_by_0 = value_counts[0]
except:
won_by_0 = 0
try:
won_by_1 = value_counts[1]
except:
won_by_1 = 0
players = ['Player 0', 'Player 1']
values = [won_by_0, won_by_1]
plt.figure(figsize = (4, 4))
plt.bar(players, values, color=[(0,0,1), (1,191/255,0)], width = 0.3)
plt.title('Win Count')
plt.savefig('static/winners.png')
def _get_successful_opening_moves_bar_chart(self) -> None:
"""
Given the stats of all previously recorded games,
generate a bar chart that shows successful opening moves
"""
value_counts = self.stats['first_move_winner'].value_counts().to_dict()
moves = list(value_counts.keys())
counts = list(value_counts.values())
if len(moves) > 5:
moves = moves[:5]
counts = counts[:5]
plt.figure(figsize = (4, 4))
plt.bar(moves, counts, color='maroon', width = 0.3)
plt.title('Successful Openers (Gobbler -> Board Pos.)',)
plt.savefig('static/opening_moves.png')
def _get_num_turns_chart(self) -> None:
"""
Given the stats of all previously recorded games,
generate a line graph of the number of turns per game
"""
turns = self.stats['num_turns'].to_list()
plt.figure(figsize = (4, 4))
plt.plot(turns, color='blue')
plt.title('Num. of Turns Each Game',)
plt.savefig('static/num_turns.png')