-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspace.py
443 lines (372 loc) · 13.2 KB
/
space.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
from enum import Enum
from math import inf
from ks.models import ECell, EDirection, Position
from colorama import Fore, Style
import time
from math import ceil
from random import randint
init_health = 3
wall_breaker_cooldown = 12
wall_breaker_duration = 6
wall_score_coefficients = 1
area_wall_crash_score = -20
my_wall_crash_score = -40
enemy_wall_crash_score = -60
max_cycles = 300
full_block = "\u2588"
lightY = Fore.LIGHTGREEN_EX
lightB = Fore.LIGHTBLUE_EX
INF = chr(8734)
class Action(Enum):
Right = [0, 1, 0]
Left = [0, -1, 0]
Up = [-1, 0, 0]
Down = [1, 0, 0]
RightBreak = [0, 1, 1]
LeftBreak = [0, -1, 1]
UpBreak = [-1, 0, 1]
DownBreak = [1, 0, 1]
direct = {
EDirection.Right: Action.Right,
EDirection.Left: Action.Left,
EDirection.Up: Action.Up,
EDirection.Down: Action.Down,
}
direct_reverse = {
Action.Right: EDirection.Right,
Action.RightBreak: EDirection.Right,
Action.Left: EDirection.Left,
Action.LeftBreak: EDirection.Left,
Action.Up: EDirection.Up,
Action.UpBreak: EDirection.Up,
Action.Down: EDirection.Down,
Action.DownBreak: EDirection.Down,
}
side_wall = {"Blue": ECell.BlueWall, "Yellow": ECell.YellowWall}
class CustomAgent:
def __init__(
self,
side,
direction,
pos: Position,
point,
is_breaker_active=False,
can_active_breaker=True,
breaker_cooldown=0,
breaker_rem_time=0,
health=init_health,
):
self.side = side
self.direction = direction
self.pos = pos
self.point = point
self.is_breaker_active = is_breaker_active
self.can_active_breaker = can_active_breaker
self.breaker_cooldown = breaker_cooldown
self.breaker_rem_time = breaker_rem_time
self.health = health
def update_breaker(self):
if self.breaker_rem_time > 0:
self.breaker_rem_time -= 1
self.breaker_cooldown -= 1
elif 0 < self.breaker_cooldown < wall_breaker_cooldown:
self.breaker_cooldown -= 1
if self.breaker_rem_time == 1:
self.is_breaker_active = False
if self.breaker_cooldown == 0:
self.can_active_breaker = True
def get_valid_actions(self) -> list[Action]:
reti = list(Action)[: None if self.can_active_breaker else 4]
act = direct[self.direction]
for i in range(2 if self.can_active_breaker else 1):
unavailable = Action([-act.value[0], -act.value[1], i])
reti.remove(unavailable)
return reti
class State:
def __init__(
self, board=None, me: CustomAgent = None, enemy: CustomAgent = None
) -> None:
self.board = board
self.me = me
self.enemy = enemy
def is_over(self):
return \
(self.me.health <= 0) or \
(self.me.health <= 0) or \
(self.me.pos.x == self.enemy.pos.x and self.me.pos.y == self.enemy.pos.y)
# one less
def f(self):
difference = (self.me.point - self.enemy.point)
if difference > 0 and self.is_over():
return inf
if difference < 0 and self.is_over():
return -inf
return difference + (self.me.health - self.enemy.health) * init_health
def commit_action(self, agent: CustomAgent, act: Action):
# returns: [curr_cell, curr_rem_time, curr_cooldown] *curr = current
new_pos = Position(
agent.pos.x + act.value[1], agent.pos.y + act.value[0])
# Breaker_rem_time and cooldown
if act.value[2] == 1 and agent.can_active_breaker and not agent.is_breaker_active:
agent.can_active_breaker = False
agent.is_breaker_active = True
agent.breaker_cooldown = wall_breaker_cooldown
agent.breaker_rem_time = wall_breaker_duration + 1
# we should make wall behind ourselves and get wall_score_coefficient for it:
self.board[agent.pos.y][agent.pos.x] = side_wall[agent.side]
agent.point += wall_score_coefficients
# move in direction:
new_cell = self.board[new_pos.y][new_pos.x]
# check collapse with areaWall:
if new_cell == ECell.AreaWall:
agent.point += area_wall_crash_score
agent.health = 0
elif new_cell == ECell.BlueWall:
if self.me.side == 'Blue':
self.me.point -= 1
else:
self.enemy.point -= 1
if not agent.is_breaker_active: # not on wall breaker
if agent.health == 1:
agent.point += (
my_wall_crash_score if agent.side == "Blue" else enemy_wall_crash_score
)
agent.health -= 1
elif new_cell == ECell.YellowWall:
if self.enemy.side == 'Blue':
self.me.point -= 1
else:
self.enemy.point -= 1
if not agent.is_breaker_active:
if agent.health == 1:
agent.point += (
enemy_wall_crash_score if agent.side == "Blue" else my_wall_crash_score
)
agent.health -= 1
# make new_cell an empty cell
if agent.health > 0:
self.board[new_pos.y][new_pos.x] = ECell.Empty
agent.pos = new_pos
agent.direction = direct_reverse[act]
agent.update_breaker()
return new_cell
def reverse_action(self, old_agent: CustomAgent, agent: CustomAgent, last_cell):
self.board[agent.pos.y][agent.pos.x] = last_cell
if last_cell == ECell.BlueWall:
if self.me.side == 'Blue':
self.me.point += 1
else:
self.enemy.point += 1
elif last_cell == ECell.YellowWall:
if self.enemy.side == 'Blue':
self.me.point += 1
else:
self.enemy.point += 1
agent.can_active_breaker = old_agent.can_active_breaker
agent.is_breaker_active = old_agent.is_breaker_active
agent.breaker_cooldown = old_agent.breaker_cooldown
agent.breaker_rem_time = old_agent.breaker_rem_time
agent.direction = old_agent.direction
agent.health = old_agent.health
agent.point = old_agent.point
agent.pos = old_agent.pos
self.board[agent.pos.y][agent.pos.x] = ECell.Empty
def alpha_beta_search(self, depth=8, alpha=-inf, beta=inf):
v, minV = -inf, -inf
agent = self.me
act_to_do = None
for act in agent.get_valid_actions():
copyAgent = CustomAgent(
None,
agent.direction,
agent.pos,
agent.point,
agent.is_breaker_active,
agent.can_active_breaker,
agent.breaker_cooldown,
agent.breaker_rem_time,
agent.health,
)
last_cell = self.commit_action(agent, act)
minV = self.min_value(alpha, beta, depth - 1)
if minV > v:
act_to_do = act
v = minV
# elif minV == v:
# x = randint(0, 2)
# if x==1 and act.value[2]!=1:
# act_to_do = act
try:
print('\t', act.name, (6 - len(act.name)) * ' ', '\t', f'-{INF}' if minV == -inf else minV, '\t')
except:
pass
self.reverse_action(copyAgent, agent, last_cell)
if v >= beta:
return act_to_do
alpha = max(alpha, v)
# print_board(self)
return act_to_do
def min_value(self, alpha, beta, depth):
if depth < 1:
return self.f()
v = inf
agent = self.enemy
for act in agent.get_valid_actions():
copyAgent = CustomAgent(
None,
agent.direction,
agent.pos,
agent.point,
agent.is_breaker_active,
agent.can_active_breaker,
agent.breaker_cooldown,
agent.breaker_rem_time,
agent.health,
)
last_cell = self.commit_action(agent, act)
if self.is_over():
v = min(self.f(), v)
else:
v = min(self.max_value(alpha, beta, depth - 1), v)
self.reverse_action(copyAgent, agent, last_cell)
if v <= alpha:
return v
beta = min(beta, v)
return v
def max_value(self, alpha, beta, depth):
if depth < 1:
return self.f()
v = -inf
agent = self.me
for act in agent.get_valid_actions():
copyAgent = CustomAgent(
None,
agent.direction,
agent.pos,
agent.point,
agent.is_breaker_active,
agent.can_active_breaker,
agent.breaker_cooldown,
agent.breaker_rem_time,
agent.health,
)
last_cell = self.commit_action(agent, act)
v = max(self.min_value(alpha, beta, depth - 1), v)
self.reverse_action(copyAgent, agent, last_cell)
if v >= beta:
return v
alpha = max(alpha, v)
return v
def hope(self, side: str) -> int:
pass
"""
1. Breaker_rem_time and coolDown
2. Make Wall in Current Position
3. Move in Direction
4. Check Collapse With Opponent Then AreaWall
5. Check Collapse With Players Walls and Destroying them Or Dying
"""
def eval_color(cell: ECell, x: int, y: int, blue: Position, yellow: Position):
if cell == ECell.AreaWall:
return Fore.LIGHTRED_EX
if cell == ECell.BlueWall:
return lightB
if cell == ECell.YellowWall:
return lightY
else:
if x == blue.x and y == blue.y:
return Fore.BLUE
if x == yellow.x and y == yellow.y:
return Fore.GREEN
return Fore.WHITE
def print_properties(property_name: str, blue: str, yellow: str):
print(f' {property_name}\t{lightB}{blue}\t{lightY}{yellow}', end='')
def print_board(state: State):
board, blue, yellow = state.board, None, None
if state.me.side == 'Blue':
blue = state.me
yellow = state.enemy
else:
blue = state.enemy
yellow = state.me
print("", end=" " * 3)
for i in range(len(board[0])):
print(f" {i}", end=" " if i < 10 else '')
print()
for i in range(len(board)):
print(f" {Fore.RESET}{i}", end=" " if i < 10 else " ")
for j in range(len(board[i])):
print(
eval_color(board[i][j], j, i, blue.pos,
yellow.pos) + full_block * 3,
end=Fore.RESET,
)
if i == 0:
print_properties('Health', blue.health, yellow.health)
elif i == 1:
print_properties('Point ', blue.point, yellow.point)
elif i == 3:
print_properties('RemTime', blue.breaker_rem_time,
yellow.breaker_rem_time)
elif i == 4:
print_properties('CoolDown', blue.breaker_cooldown,
yellow.breaker_cooldown)
elif i == 6:
mapp = {True: 'T', False: 'F'}
print_properties('Breaker', mapp[blue.is_breaker_active],
mapp[yellow.is_breaker_active])
print()
def make_board(N):
boarder = [0, N - 1]
center = [N // 2, N // 2 - 1]
board = []
for i in range(N):
inner = []
for j in range(N):
if j in boarder or i in boarder:
inner.append(ECell.AreaWall)
elif j in center and i in center:
inner.append(ECell.AreaWall)
else:
inner.append(ECell.Empty)
board.append(inner)
return board
def show_options(agent: CustomAgent):
counter = 1
actions = list(Action)
valid = agent.get_valid_actions()
invalid = []
for i in actions:
if i not in valid:
print(Fore.RED + Style.DIM, end='')
invalid.append(counter)
else:
print(Fore.LIGHTWHITE_EX + Style.BRIGHT, end='')
print(f'\t\t{counter}) {i.name}')
print(Fore.RESET + Style.RESET_ALL, end='')
counter += 1
while (True):
try:
i = int(input('Enter a Number\t')) - 1
if i + 1 in invalid:
print('Xkholi')
continue
return actions[i]
except:
continue
if __name__ == "__main__":
print('\n' * 20)
N = 20
blueAgent = CustomAgent("Blue", EDirection.Down, Position(1, 1), 0)
yellowAgent = CustomAgent(
"Yellow", EDirection.Left, Position(N - 2, N - 2), 0)
state = State(board=make_board(N), me=blueAgent, enemy=yellowAgent)
print_board(state)
while (True):
x = time.time()
ai_act = state.alpha_beta_search(6)
print(Fore.LIGHTMAGENTA_EX + '%.3f' % (time.time() - x) + Fore.RESET)
state.commit_action(state.me, ai_act)
act = show_options(state.enemy)
state.commit_action(state.enemy, act)
print_board(state)