Skip to content
Draft
269 changes: 137 additions & 132 deletions pelita/game.py

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions pelita/player/SmartEatingPlayer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@

from pelita.game import apply_move, next_round_turn
from pelita.player import food_eating_player
from pelita.spec import FullTeamState
from pelita.team import Bot, _ensure_list_tuples, make_bots

def simulate_move(bot: Bot, next_pos):
game_state: FullTeamState = dict(bot._game_state)
game_state['bots'] = _ensure_list_tuples(game_state['bots'])
game_state['error_limit'] = 0
game_state['gameover'] = False
game_state['walls'] = bot.walls
game_state['shape'] = bot.shape
game_state['fatal_errors'] = [[], []]
game_state['errors'] = [[], []]
game_state['game_phase'] = 'RUNNING'

print(bot)
print(game_state)
game_state = apply_move(game_state, next_pos)
if not game_state['game_phase'] == 'RUNNING':
return None

game_state.update(next_round_turn(game_state))


for tidx in range(2):
game_state['food'][tidx] = _ensure_list_tuples(game_state['food'][tidx])
game_state['shaded_food'][tidx] = _ensure_list_tuples(game_state['shaded_food'][tidx])

next_bot = make_bots(bot_positions=game_state['bots'],
is_noisy=game_state['is_noisy'],
walls=bot.walls,
shape=bot.shape,
food=game_state['food'],
shaded_food=game_state['shaded_food'],
round=game_state['round'],
turn=game_state['turn'],
score=game_state['score'],
deaths=game_state['deaths'],
kills=game_state['kills'],
bot_was_killed=game_state['bot_was_killed'],
error_count=game_state['error_count'],
initial_positions=[bot._initial_position, bot.other._initial_position, bot._initial_position, bot.other._initial_position],
homezone=[bot.other.homezone, bot.homezone],
team_names=game_state['team_names'],
team_time=game_state['team_time'],
rng="bot._rng",
graph=bot.graph)

return next_bot



def smart_eating_player(bot, state):
for pos in bot.legal_positions:
print(simulate_move(bot, next_pos=pos))

# food eating player but won’t do kamikaze (although a sufficiently smart
# enemy will be able to kill the bot in its next turn as it doesn’t flee)
next_pos = food_eating_player(bot, state)
Expand Down
91 changes: 91 additions & 0 deletions pelita/spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from typing import Literal, TypeAlias, TypedDict, Any
from random import Random

from .team import Team

Shape: TypeAlias = tuple[int, int]
Pos: TypeAlias = tuple[int, int]
FoodAges: TypeAlias = dict[Pos, int]

class Layout(TypedDict):
bots: list[Pos]
walls: set[Pos]
shape: Shape
food: list[Pos]
# food: tuple[set[Pos], set[Pos]]

class GameState(TypedDict):
game_uuid: str
walls: set[Pos]
shape: Shape
food: list[set[Pos]]
food_age: list[FoodAges]
game_phase: Literal["INIT", "RUNNING", "FAILURE", "FINISHED"]
turn: None|int
round: None|int
gameover: bool
whowins: None|int
bots: list[Pos]
score: list[int]
timeouts: list[Any]
fatal_errors: list[list[Any]]
max_rounds: int
timeout_length: int
initial_timeout: int
noise_radius: int
sight_distance: int
max_food_age: float|int
shadow_distance: int
team_names: list[None|str]
team_infos: list[None|str]
team_time: list[float]
deaths: list[int]
kills: list[int]
bot_was_killed: list[bool]
noisy_positions: list[None|Pos]
requested_moves: list[None|Pos]
say: list[str]
overlays: list[list[dict]]
teams: list[None|Team]
rng: Random
error_limit: int
viewers: list[Any]
controller: None|Any

class TeamInitial(TypedDict):
walls: set[Pos]
shape: Shape
seed: int
max_rounds: int
team_names: list[str]
timeout_length: float

class TeamState(TypedDict):
bots: list[Pos]
score: list[int]
kills: list[int]
deaths: list[int]
bot_was_killed: list[bool]
error_count: list[int]
food: list[list[Pos]]
shaded_food: list[list[Pos]]
team_time: list[float]
is_noisy: list[bool]
round: int|None
turn: int

class TeamStateFinished(TypedDict):
bots: list[Pos]
score: list[int]
kills: list[int]
deaths: list[int]
bot_was_killed: list[bool]
error_count: list[bool]
food: list[list[Pos]]
team_time: list[float]
round: int|None
turn: int
whowins: int

class FullTeamState(TeamInitial, TeamState):
pass
Loading
Loading