-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameplay_utils.py
156 lines (122 loc) · 4.63 KB
/
gameplay_utils.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
import random
from enum import Enum
from game_field import Cell, GameField
class MotionDirection(Enum):
UP = 1
DOWN = 2
RIGHT = 3
LEFT = 4
DO_NOTHING = 5
def _is_motion_horizontal(motion_direction: MotionDirection) -> bool:
return motion_direction in (
MotionDirection.RIGHT, MotionDirection.LEFT,
)
def _can_person_go(
game_field: GameField,
new_y: int,
new_x: int,
) -> bool:
if game_field.get(new_y, new_x) == Cell.EMPTY:
return True
return False
def _is_person_on_track(game_field: GameField, new_y: int, new_x: int) -> bool:
try:
if game_field.get(new_y, new_x) == Cell.TRACK:
return True
return False
except IndexError:
pass
def _is_border_reached(game_field: GameField, new_y: int, new_x: int) -> bool:
return game_field.get(new_y, new_x) in (Cell.BORDER, Cell.MARKED)
def _is_on_track(game_field: GameField, new_y: int, new_x: int) -> bool:
return game_field.get(new_y, new_x) == Cell.TRACK
def _get_new_coordinates_by_motion_direction(
old_y: int, old_x: int, motion_direction: MotionDirection,
) -> tuple[int, int]:
decision_mapping = {
MotionDirection.UP: lambda y, x: (y - 1, x),
MotionDirection.DOWN: lambda y, x: (y + 1, x),
MotionDirection.RIGHT: lambda y, x: (y, x + 1),
MotionDirection.LEFT: lambda y, x: (y, x - 1),
}
return decision_mapping[motion_direction](old_y, old_x)
def _get_new_steps_count(top: int) -> int:
return random.randint(2, top)
def _get_new_movement_direction(old_direction):
new_dir = random.randint(1, 4)
if new_dir == old_direction:
return MotionDirection(_get_new_movement_direction(new_dir))
else:
return MotionDirection(new_dir)
def return_changes(func):
def foo(self, *args, **kwargs):
self._game_field.clear_changes()
func(self, *args, **kwargs)
return self._game_field.get_changes()
return foo
class LittleFigureDetector:
""" When hero reach border, whole game field became
separated by two figures, the class needs to detect which
of these figures is less, and then the class marked it"""
def __init__(
self,
top: int,
bottom: int,
left: int,
right: int,
game_field: GameField,
):
self._top = top
self._bottom = bottom
self._left = left
self._right = right
self._game_field = game_field
def detect(self):
self._fill_one_figure()
self._select_little_figure()
def _select_little_figure(self):
empty_count = 0
considered_count = 0
for i in self._game_field._matrix:
for j in i:
if j == Cell.EMPTY:
empty_count += 1
if j == Cell.CONSIDER:
considered_count += 1
for y in range(self._top):
for x in range(self._right):
if empty_count < considered_count:
if self._game_field.get(y, x) == Cell.EMPTY:
self._game_field.set(y, x, Cell.MARKED)
if self._game_field.get(y, x) == Cell.CONSIDER:
self._game_field.set(y, x, Cell.EMPTY)
else:
if self._game_field.get(y, x) == Cell.CONSIDER:
self._game_field.set(y, x, Cell.MARKED)
if self._game_field.get(y, x) == Cell.TRACK:
self._game_field.set(y, x, Cell.MARKED)
def _fill_one_figure(self, y=None, x=None):
if y is None or x is None:
y, x = self._get_random_empty_coordinates()
try:
if self._game_field.get(y + 1, x) == Cell.EMPTY:
self._game_field.set(y + 1, x, Cell.CONSIDER)
self._fill_one_figure(y + 1, x)
if self._game_field.get(y, x + 1) == Cell.EMPTY:
self._game_field.set(y, x + 1, Cell.CONSIDER)
self._fill_one_figure(y, x + 1)
if self._game_field.get(y - 1, x) == Cell.EMPTY:
self._game_field.set(y - 1, x, Cell.CONSIDER)
self._fill_one_figure(y - 1, x)
if self._game_field.get(y, x - 1) == Cell.EMPTY:
self._game_field.set(y, x - 1, Cell.CONSIDER)
self._fill_one_figure(y, x - 1)
except IndexError:
pass
def _get_random_empty_coordinates(self):
random.seed()
y = random.randint(3, self._top - 2)
x = random.randint(2, self._right - 2)
if self._game_field.get(y, x) == Cell.EMPTY:
return y, x
return self._get_random_empty_coordinates()