forked from shrawani21/gamer_21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
323 lines (285 loc) · 12.3 KB
/
player.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
import random
from typing import List, Dict, Optional
from box import Box
def get_line_boxes(matrix: List[List[Box]], line: List[tuple[int, int]]) -> List[Box]:
"""Returns the boxes adjacent to a given line."""
p1, p2 = line
boxes = []
if p1[0] == p2[0]: # horizontal
pos1 = min(p1, p2, key=lambda x: x[1])
if pos1[0] > 0:
box_top = matrix[pos1[0] - 1][pos1[1]]
boxes.append(box_top)
if pos1[0] < len(matrix):
box_bottom = matrix[pos1[0]][pos1[1]]
boxes.append(box_bottom)
else: # vertical
pos1 = min(p1, p2, key=lambda x: x[0])
if pos1[1] > 0:
box_left = matrix[pos1[0]][pos1[1] - 1]
boxes.append(box_left)
if pos1[1] < len(matrix[0]):
box_right = matrix[pos1[0]][pos1[1]]
boxes.append(box_right)
return boxes
def get_adjacent_boxes(matrix: List[List[Box]], box: Box) -> Dict[str, Optional[Box]]:
"""Returns the boxes adjacent to a given box."""
boxes = {'top': None, 'bottom': None, 'left': None, 'right': None}
if box.idx[0] > 0:
boxes['top'] = matrix[box.idx[0] - 1][box.idx[1]]
if box.idx[0] < len(matrix) - 1:
boxes['bottom'] = matrix[box.idx[0] + 1][box.idx[1]]
if box.idx[1] > 0:
boxes['left'] = matrix[box.idx[0]][box.idx[1] - 1]
if box.idx[1] < len(matrix[0]) - 1:
boxes['right'] = matrix[box.idx[0]][box.idx[1] + 1]
return boxes
def count_adjacent_boxes(matrix: List[List[Box]], box: Box) -> int:
"""Returns the number of adjacent boxes around a given box."""
count = 0
if box.idx[0] > 0:
count += 1
if box.idx[0] < len(matrix) - 1:
count += 1
if box.idx[1] > 0:
count += 1
if box.idx[1] < len(matrix[0]) - 1:
count += 1
return count
def get_random_empty_side(box: Box, exclude: Optional[tuple[int, int]] = None) -> Optional[tuple[int, int]]:
"""Returns a random empty side of a given box."""
choices = []
if box.top is None and (exclude is None or box.top_idx() != exclude):
choices.append(box.top_idx())
if box.bottom is None and (exclude is None or box.bottom_idx() != exclude):
choices.append(box.bottom_idx())
if box.left is None and (exclude is None or box.left_idx() != exclude):
choices.append(box.left_idx())
if box.right is None and (exclude is None or box.right_idx() != exclude):
choices.append(box.right_idx())
return random.choice(choices) if choices else None
def get_side_counts(matrix: List[List[Box]], box: Box) -> Dict[str, Optional[int]]:
"""Returns the number of sides occupied around a given box."""
sides = {'top': None, 'bottom': None, 'left': None, 'right': None}
if box.idx[0] > 0:
sides['top'] = matrix[box.idx[0] - 1][box.idx[1]].sides
if box.idx[0] < len(matrix) - 1:
sides['bottom'] = matrix[box.idx[0] + 1][box.idx[1]].sides
if box.idx[1] > 0:
sides['left'] = matrix[box.idx[0]][box.idx[1] - 1].sides
if box.idx[1] < len(matrix[0]) - 1:
sides['right'] = matrix[box.idx[0]][box.idx[1] + 1].sides
return sides
def is_side_less_than(matrix: List[List[Box]], box: Box, side: str, num: int) -> bool:
"""Checks if the sides around a given box are less than a specified number."""
sides = get_side_counts(matrix, box)[side]
return sides is None or sides < num
def is_facing_outside(matrix: List[List[Box]], box: Box) -> bool:
"""Checks if a given box is facing the outside of the matrix."""
if box.idx[0] == 0 and box.top is None:
return True
if box.idx[0] == len(matrix) - 1 and box.bottom is None:
return True
if box.idx[1] == 0 and box.left is None:
return True
if box.idx[1] == len(matrix[0]) - 1 and box.right is None:
return True
return False
def easy_strategy(matrix: List[List[Box]], prev_line: Optional[List[tuple[int, int]]]) -> Optional[tuple[int, int]]:
"""Returns a move for easy difficulty."""
if prev_line:
for prev_box in get_line_boxes(matrix, prev_line):
if prev_box.sides == 3:
return get_random_empty_side(prev_box)
boxes = Box.ALL_BOXES
box0 = [box for box in boxes if box.sides == 0]
box1 = [box for box in boxes if box.sides == 1]
box2 = [box for box in boxes if box.sides == 2]
box3 = [box for box in boxes if box.sides == 3]
if box3:
return get_random_empty_side(random.choice(box3))
if box0:
return get_random_empty_side(random.choice(box0))
if box1:
return get_random_empty_side(random.choice(box1))
if box2:
return get_random_empty_side(random.choice(box2))
def medium_strategy(matrix: List[List[Box]], prev_line: Optional[List[tuple[int, int]]]) -> Optional[tuple[int, int]]:
"""Returns a move for medium difficulty."""
if prev_line:
for prev_box in get_line_boxes(matrix, prev_line):
if prev_box.sides == 3:
return get_random_empty_side(prev_box)
boxes = Box.ALL_BOXES
box0 = [box for box in boxes if box.sides == 0]
box1 = [box for box in boxes if box.sides == 1]
box2 = [box for box in boxes if box.sides == 2]
box3 = [box for box in boxes if box.sides == 3]
box_less2 = box0 + box1
if box3:
return get_random_empty_side(random.choice(box3))
sides_to_check = ['top', 'bottom', 'left', 'right']
choices = []
for side in sides_to_check:
choices.extend([
getattr(box, f"{side}_idx")()
for box in box_less2 if is_side_less_than(matrix, box, side, 2)
])
if choices:
return random.choice(choices)
if box0:
return get_random_empty_side(random.choice(box0))
if box1:
return get_random_empty_side(random.choice(box1))
if box2:
return get_random_empty_side(random.choice(box2))
def hard_strategy(matrix: List[List[Box]], prev_line: Optional[List[tuple[int, int]]]) -> Optional[tuple[int, int]]:
"""Returns a move for hard difficulty."""
if prev_line:
for prev_box in get_line_boxes(matrix, prev_line):
if prev_box.sides == 3:
return get_random_empty_side(prev_box)
boxes = Box.ALL_BOXES
box0 = [box for box in boxes if box.sides == 0]
box1 = [box for box in boxes if box.sides == 1]
box3 = [box for box in boxes if box.sides == 3]
if box3:
return get_random_empty_side(random.choice(box3))
box_less2 = box0 + box1
sides_to_check = ['top', 'bottom', 'left', 'right']
choices = []
for side in sides_to_check:
choices.extend([
getattr(box, f"{side}_idx")()
for box in box_less2 if is_side_less_than(matrix, box, side, 2)
])
if choices:
return random.choice(choices)
chains = []
checked = []
crosses = []
options = boxes.copy()
while len(checked) < len(boxes):
current = [options[0]]
if current[0].color is not None:
checked.append(current[0])
options.remove(current[0])
continue
if current[0].sides < 2:
crosses.append(current[0])
checked.append(current[0])
options.remove(current[0])
continue
new_chain = []
chain = []
while current:
for box in current:
checked.append(box)
chain.append(box)
options.remove(box)
for direction in ['top', 'bottom', 'left', 'right']:
adj_box = get_adjacent_boxes(matrix, box)[direction]
if getattr(box, direction) is None and adj_box and adj_box not in new_chain and adj_box not in chain:
if adj_box.sides >= 2:
new_chain.append(adj_box)
current = new_chain
new_chain = []
chains.append(chain)
sorted_chains = sorted(chains, key=len)
if sorted_chains:
return get_random_empty_side(random.choice(sorted_chains[0]))
return get_random_empty_side(random.choice(crosses))
def extreme_strategy(matrix: List[List[Box]], prev_line: Optional[List[tuple[int, int]]]) -> Optional[tuple[int, int]]:
"""Returns a move for extreme difficulty."""
boxes = Box.ALL_BOXES
box0 = [box for box in boxes if box.sides == 0]
box1 = [box for box in boxes if box.sides == 1]
box3 = [box for box in boxes if box.sides == 3]
box_less2 = box0 + box1
sides_to_check = ['top', 'bottom', 'left', 'right']
choices = []
for side in sides_to_check:
choices.extend([
getattr(box, f"{side}_idx")()
for box in box_less2 if is_side_less_than(matrix, box, side, 2)
])
chains = []
checked = []
crosses = []
options = boxes.copy()
while len(checked) < len(boxes):
current = [options[0]]
if current[0].color is not None:
checked.append(current[0])
options.remove(current[0])
continue
if current[0].sides < 2:
crosses.append(current[0])
checked.append(current[0])
options.remove(current[0])
continue
new_chain = []
chain = []
while current:
for box in current:
checked.append(box)
chain.append(box)
options.remove(box)
for direction in ['top', 'bottom', 'left', 'right']:
adj_box = get_adjacent_boxes(matrix, box)[direction]
if getattr(box, direction) is None and adj_box and adj_box not in new_chain and adj_box not in chain:
if adj_box.sides >= 2:
new_chain.append(adj_box)
current = new_chain
new_chain = []
chains.append(chain)
sorted_chains = sorted(chains, key=len)
if prev_line:
for prev_box in get_line_boxes(matrix, prev_line):
if prev_box.sides == 3:
side = get_random_empty_side(prev_box)
if not choices and len(sorted_chains) > 1:
if len(sorted_chains[1]) > 2 and len(box3) < 2:
side_boxes = get_line_boxes(matrix, side)
for box in side_boxes:
if box != prev_box and count_adjacent_boxes(matrix, box) < 4 and is_facing_outside(matrix, box):
return get_random_empty_side(box, exclude=side)
return side
if box3:
selected_box = random.choice(box3)
side = get_random_empty_side(selected_box)
if not choices and len(sorted_chains) > 1:
if len(sorted_chains[1]) > 2 and len(box3) < 2:
side_boxes = get_line_boxes(matrix, side)
for box in side_boxes:
if box != selected_box and count_adjacent_boxes(matrix, box) < 4 and is_facing_outside(matrix, box):
return get_random_empty_side(box, exclude=side)
return side
if choices:
return random.choice(choices)
if sorted_chains:
return get_random_empty_side(random.choice(sorted_chains[0]))
return get_random_empty_side(random.choice(crosses))
def expert_strategy(matrix: List[List[Box]], prev_line: Optional[List[tuple[int, int]]]) -> Optional[tuple[int, int]]:
"""Returns a move for expert difficulty. (To be implemented)"""
pass
class Player:
def __init__(self, player_type: str, color: str, difficulty: int = 1):
self.player_type = player_type
self.score = 0
self.color = color
self.move = None
self.difficulty = difficulty
def get_move(self, matrix: List[List[Box]], prev_line: Optional[List[tuple[int, int]]]) -> Optional[tuple[int, int]]:
if self.player_type == 'human':
move = self.move
self.move = None
return move
strategy_funcs = {
1: easy_strategy,
2: medium_strategy,
3: hard_strategy,
4: extreme_strategy,
5: expert_strategy # Placeholder for future implementation
}
return strategy_funcs[self.difficulty](matrix, prev_line)