Skip to content

Commit 148ee72

Browse files
committed
wip
1 parent d0312a8 commit 148ee72

2 files changed

Lines changed: 52 additions & 46 deletions

File tree

pelita/game.py

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .network import setup_controller, ZMQPublisher
1616
from .base_utils import default_rng
1717
from .team import make_team
18-
from .spec import GameState
18+
from .spec import GameState, Layout, Pos
1919
from .viewer import ProgressViewer, AsciiViewer, ReplyToViewer, ReplayWriter, ResultPrinter
2020

2121
_logger = logging.getLogger(__name__)
@@ -268,7 +268,7 @@ def setup_viewers(viewers, print_result=True):
268268
return viewer_state
269269

270270

271-
def setup_game(team_specs, *, layout_dict, max_rounds=300, layout_name="", rng=None,
271+
def setup_game(team_specs, *, layout_dict: Layout, max_rounds=300, layout_name="", rng=None,
272272
allow_camping=False, error_limit=5, timeout_length=3,
273273
viewers=None, store_output=False,
274274
team_names=(None, None), team_infos=(None, None),
@@ -316,120 +316,120 @@ def setup_game(team_specs, *, layout_dict, max_rounds=300, layout_name="", rng=N
316316

317317
# Initialize the game state.
318318

319-
game_state: GameState = dict(
319+
game_state: GameState = {
320320
### The layout attributes
321321
#: Walls. Set of (int, int)
322-
walls=set(layout_dict['walls']),
322+
'walls': set(layout_dict['walls']),
323323

324324
#: Shape of the maze. (int, int)
325-
shape=layout_dict['shape'],
325+
'shape': layout_dict['shape'],
326326

327327
#: Food per team. List of sets of (int, int)
328-
food=food,
328+
'food': food,
329329

330330
#: Food ages per team. Dict of (int, int) to int
331-
food_age=[{}, {}],
331+
'food_age': ({}, {}),
332332

333333
### Round/turn information
334334
#: Current bot, int, None
335-
turn=None,
335+
'turn': None,
336336

337337
#: Current round, int, None
338-
round=None,
338+
'round': None,
339339

340340
#: Is the game finished? bool
341-
gameover=False,
341+
'gameover': False,
342342

343343
#: Who won? int, None
344-
whowins=None,
344+
'whowins': None,
345345

346346
### Bot/team status
347347
#: Positions of all bots. List of (int, int)
348-
bots=layout_dict['bots'][:],
348+
'bots': layout_dict['bots'][:],
349349

350350
#: Score of the teams. List of int
351-
score=[0] * 2,
351+
'score': (0, 0),
352352

353353
#: Fatal errors
354-
fatal_errors=[[], []],
354+
'fatal_errors': ([], []),
355355

356356
#: Errors
357-
errors=[{}, {}],
357+
'errors': ({}, {}),
358358

359359
### Configuration
360360
#: Maximum number of rounds, int
361-
max_rounds=max_rounds,
361+
'max_rounds': max_rounds,
362362

363363
#: Time till timeout, int
364-
timeout=3,
364+
'timeout': 3,
365365

366366
#: Noise radius, int
367-
noise_radius=NOISE_RADIUS,
367+
'noise_radius': NOISE_RADIUS,
368368

369369
#: Sight distance, int
370-
sight_distance=SIGHT_DISTANCE,
370+
'sight_distance': SIGHT_DISTANCE,
371371

372372
#: Max food age
373-
max_food_age=max_food_age,
373+
'max_food_age': max_food_age,
374374

375375
#: Shadow distance, int
376-
shadow_distance=SHADOW_DISTANCE,
376+
'shadow_distance': SHADOW_DISTANCE,
377377

378378
### Informative
379379
#: Name of the layout, str
380-
layout_name=layout_name,
380+
'layout_name': layout_name,
381381

382382
#: Name of the teams. Tuple of str
383-
team_names=team_names,
383+
'team_names': team_names,
384384

385385
#: Additional team info. Tuple of str|None
386-
team_infos=team_infos,
386+
'team_infos': team_infos,
387387

388388
#: Time each team needed, list of float
389-
team_time=[0, 0],
389+
'team_time': (0.0, 0.0),
390390

391391
# List of bot deaths, which counts the number of deaths per bot
392392
# In other words, deaths[bot_idx] is the number of times the bot
393393
# bot_idx has been killed until now.
394-
deaths = [0]*4,
394+
'deaths': [0] * 4,
395395

396396
# List of bot kills, which counts the number of kills per bot
397397
# In other words, kills[bot_idx] is the number of times the bot
398398
# bot_idx has killed another bot until now.
399-
kills = [0]*4,
399+
'kills': [0] * 4,
400400

401401
# List of boolean flags weather bot has been eaten since its last move
402-
bot_was_killed = [False]*4,
402+
'bot_was_killed': [False]*4,
403403

404404
# The noisy positions that the bot in `turn` has currently been shown.
405405
# None, if not noisy
406-
noisy_positions = [None] * 4,
406+
'noisy_positions': [None] * 4,
407407

408408
#: The moves that the bots returned. Keeps only the recent one at the respective bot’s index.
409-
requested_moves=[None] * 4,
409+
'requested_moves': [None] * 4,
410410

411411
#: Messages the bots say. Keeps only the recent one at the respective bot’s index.
412-
say=[""] * 4,
412+
'say': [""] * 4,
413413

414414
### Internal
415415
#: Internal team representation
416-
teams=[None] * 2,
416+
'teams': [None] * 2,
417417

418418
#: Random number generator
419-
rng=rng,
419+
'rng': rng,
420420

421421
#: Timeout length, int, None
422-
timeout_length=timeout_length,
422+
'timeout_length': timeout_length,
423423

424424
#: Error limit. A team loses when the limit is reached, int
425-
error_limit=error_limit,
425+
'error_limit': error_limit,
426426

427427
#: Viewers, list
428-
viewers=viewer_state['viewers'],
428+
'viewers': viewer_state['viewers'],
429429

430430
#: Controller
431-
controller=viewer_state['controller']
432-
)
431+
'controller': viewer_state['controller']
432+
}
433433

434434
# Wait until the controller tells us that it is ready
435435
# We then can send the initial maze
@@ -590,20 +590,20 @@ def prepare_bot_state(game_state, idx=None):
590590
'error_count': [len(e) for e in game_state['errors'][:]],
591591
'food': [list(team_food) for team_food in game_state['food']],
592592
'shaded_food': shaded_food,
593-
'team_names': game_state['team_names'][:],
594593
'team_time': game_state['team_time'][:],
595594
'is_noisy': is_noisy,
596595
'round': game_state['round'],
597596
'turn': turn,
598597
'timeout_length': game_state['timeout_length'],
599-
'max_rounds': game_state['max_rounds'],
600598
}
601599

602600
if bot_initialization:
603601
bot_state.update({
604602
'walls': game_state['walls'], # only in initial round
605603
'shape': game_state['shape'], # only in initial round
606-
'seed': seed # only used in set_initial phase
604+
'seed': seed, # only used in set_initial phase
605+
'max_rounds': game_state['max_rounds'],
606+
'team_names': game_state['team_names'][:],
607607
})
608608

609609
return bot_state
@@ -1027,8 +1027,8 @@ def check_exit_remote_teams(game_state):
10271027
pass
10281028

10291029

1030-
def split_food(width, food):
1031-
team_food = [set(), set()]
1030+
def split_food(width, food: list[Pos]):
1031+
team_food: tuple[set[Pos], set[Pos]] = (set(), set())
10321032
for pos in food:
10331033
idx = pos[0] // (width // 2)
10341034
team_food[idx].add(pos)

pelita/spec.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
Pos: TypeAlias = tuple[int, int]
88
FoodAges: TypeAlias = dict[Pos, int]
99

10+
class Layout(TypedDict):
11+
bots: list[Pos]
12+
walls: set[Pos]
13+
shape: Shape
14+
food: tuple[set[Pos], set[Pos]]
15+
1016
class GameState(TypedDict):
1117
walls: set[Pos]
1218
shape: Shape
@@ -17,17 +23,17 @@ class GameState(TypedDict):
1723
gameover: bool
1824
whowins: None|int
1925
bots: list[Pos]
20-
score: list[int]
26+
score: tuple[int, int]
2127
fatal_errors: tuple[list[Any], list[Any]]
2228
errors: tuple[Any, Any]
2329
max_rounds: int
2430
timeout: int
2531
noise_radius: int
2632
sight_distance: int
27-
max_food_age: int
33+
max_food_age: float|int
2834
shadow_distance: int
2935
layout_name: str
30-
team_names: tuple[str, str]
36+
team_names: tuple[None|str, None|str]
3137
team_infos: tuple[None|str, None|str]
3238
team_time: tuple[float, float]
3339
deaths: list[int]

0 commit comments

Comments
 (0)