Skip to content

Commit accbb36

Browse files
committed
wip
1 parent aa38052 commit accbb36

File tree

2 files changed

+51
-45
lines changed

2 files changed

+51
-45
lines changed

pelita/game.py

Lines changed: 42 additions & 42 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__)
@@ -276,7 +276,7 @@ def setup_viewers(viewers, print_result=True):
276276
return viewer_state
277277

278278

279-
def setup_game(team_specs, *, layout_dict, max_rounds=300, rng=None,
279+
def setup_game(team_specs, *, layout_dict: Layout, max_rounds=300, rng=None,
280280
allow_camping=False, error_limit=5, timeout_length=3,
281281
viewers=None, store_output=False,
282282
team_names=(None, None), team_infos=(None, None),
@@ -324,118 +324,118 @@ def setup_game(team_specs, *, layout_dict, max_rounds=300, rng=None,
324324

325325
# Initialize the game state.
326326

327-
game_state: GameState = dict(
327+
game_state: GameState = {
328328
### The layout attributes
329329
#: Walls. Set of (int, int)
330-
walls=set(layout_dict['walls']),
330+
'walls': set(layout_dict['walls']),
331331

332332
#: Shape of the maze. (int, int)
333-
shape=layout_dict['shape'],
333+
'shape': layout_dict['shape'],
334334

335335
#: Food per team. List of sets of (int, int)
336-
food=food,
336+
'food': food,
337337

338338
#: Food ages per team. Dict of (int, int) to int
339-
food_age=[{}, {}],
339+
'food_age': ({}, {}),
340340

341341
### Round/turn information
342342
#: Current bot, int, None
343-
turn=None,
343+
'turn': None,
344344

345345
#: Current round, int, None
346-
round=None,
346+
'round': None,
347347

348348
#: Is the game finished? bool
349-
gameover=False,
349+
'gameover': False,
350350

351351
#: Who won? int, None
352-
whowins=None,
352+
'whowins': None,
353353

354354
### Bot/team status
355355
#: Positions of all bots. List of (int, int)
356-
bots=layout_dict['bots'][:],
356+
'bots': layout_dict['bots'][:],
357357

358358
#: Score of the teams. List of int
359-
score=[0] * 2,
359+
'score': (0, 0),
360360

361361
#: Fatal errors
362-
fatal_errors=[[], []],
362+
'fatal_errors': ([], []),
363363

364364
#: Errors
365-
errors=[{}, {}],
365+
'errors': ({}, {}),
366366

367367
### Configuration
368368
#: Maximum number of rounds, int
369-
max_rounds=max_rounds,
369+
'max_rounds': max_rounds,
370370

371371
#: Time till timeout, int
372-
timeout=3,
372+
'timeout': 3,
373373

374374
#: Noise radius, int
375-
noise_radius=NOISE_RADIUS,
375+
'noise_radius': NOISE_RADIUS,
376376

377377
#: Sight distance, int
378-
sight_distance=SIGHT_DISTANCE,
378+
'sight_distance': SIGHT_DISTANCE,
379379

380380
#: Max food age
381-
max_food_age=max_food_age,
381+
'max_food_age': max_food_age,
382382

383383
#: Shadow distance, int
384-
shadow_distance=SHADOW_DISTANCE,
384+
'shadow_distance': SHADOW_DISTANCE,
385385

386386
### Informative
387387

388388
#: Name of the teams. Tuple of str
389-
team_names=team_names,
389+
'team_names': team_names,
390390

391391
#: Additional team info. Tuple of str|None
392-
team_infos=team_infos,
392+
'team_infos': team_infos,
393393

394394
#: Time each team needed, list of float
395-
team_time=[0, 0],
395+
'team_time': (0.0, 0.0),
396396

397397
# List of bot deaths, which counts the number of deaths per bot
398398
# In other words, deaths[bot_idx] is the number of times the bot
399399
# bot_idx has been killed until now.
400-
deaths = [0]*4,
400+
'deaths': [0] * 4,
401401

402402
# List of bot kills, which counts the number of kills per bot
403403
# In other words, kills[bot_idx] is the number of times the bot
404404
# bot_idx has killed another bot until now.
405-
kills = [0]*4,
405+
'kills': [0] * 4,
406406

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

410410
# The noisy positions that the bot in `turn` has currently been shown.
411411
# None, if not noisy
412-
noisy_positions = [None] * 4,
412+
'noisy_positions': [None] * 4,
413413

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

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

420420
### Internal
421421
#: Internal team representation
422-
teams=[None] * 2,
422+
'teams': [None] * 2,
423423

424424
#: Random number generator
425-
rng=rng,
425+
'rng': rng,
426426

427427
#: Timeout length, int, None
428-
timeout_length=timeout_length,
428+
'timeout_length': timeout_length,
429429

430430
#: Error limit. A team loses when the limit is reached, int
431-
error_limit=error_limit,
431+
'error_limit': error_limit,
432432

433433
#: Viewers, list
434-
viewers=viewer_state['viewers'],
434+
'viewers': viewer_state['viewers'],
435435

436436
#: Controller
437-
controller=viewer_state['controller']
438-
)
437+
'controller': viewer_state['controller']
438+
}
439439

440440
# Wait until the controller tells us that it is ready
441441
# We then can send the initial maze
@@ -596,20 +596,20 @@ def prepare_bot_state(game_state, idx=None):
596596
'error_count': [len(e) for e in game_state['errors'][:]],
597597
'food': [list(team_food) for team_food in game_state['food']],
598598
'shaded_food': shaded_food,
599-
'team_names': game_state['team_names'][:],
600599
'team_time': game_state['team_time'][:],
601600
'is_noisy': is_noisy,
602601
'round': game_state['round'],
603602
'turn': turn,
604603
'timeout_length': game_state['timeout_length'],
605-
'max_rounds': game_state['max_rounds'],
606604
}
607605

608606
if bot_initialization:
609607
bot_state.update({
610608
'walls': game_state['walls'], # only in initial round
611609
'shape': game_state['shape'], # only in initial round
612-
'seed': seed # only used in set_initial phase
610+
'seed': seed, # only used in set_initial phase
611+
'max_rounds': game_state['max_rounds'],
612+
'team_names': game_state['team_names'][:],
613613
})
614614

615615
return bot_state
@@ -1036,8 +1036,8 @@ def check_exit_remote_teams(game_state):
10361036
pass
10371037

10381038

1039-
def split_food(width, food):
1040-
team_food = [set(), set()]
1039+
def split_food(width, food: list[Pos]):
1040+
team_food: tuple[set[Pos], set[Pos]] = (set(), set())
10411041
for pos in food:
10421042
idx = pos[0] // (width // 2)
10431043
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)