Browse Source

Strategická: Pár komentářů

master
David Klement 2 years ago
parent
commit
fd8c8ed082
  1. 35
      klient/play.py

35
klient/play.py

@ -17,9 +17,13 @@ class Direction(enum.Enum):
STAY = None STAY = None
def combine(self, other) -> Direction: def combine(self, other) -> Direction:
"""Rotate by another direction."""
return Direction((self.value + other.value) % 4) return Direction((self.value + other.value) % 4)
def invert(self) -> Direction: def invert(self) -> Direction:
"""Get the opposite direction."""
if self == Direction.STAY: if self == Direction.STAY:
return self return self
return self.combine(Direction.DOWN) return self.combine(Direction.DOWN)
@ -29,7 +33,7 @@ class Direction(enum.Enum):
class Team: class Team:
home: 'Field' home: Field
def __init__(self, id: int, is_me: bool) -> None: def __init__(self, id: int, is_me: bool) -> None:
self.id: int = id self.id: int = id
@ -74,6 +78,8 @@ class Field:
return hash((self.i, self.j)) return hash((self.i, self.j))
def get_neighbour_field(self, action: Direction) -> Field: def get_neighbour_field(self, action: Direction) -> Field:
"""Get the next field in the given direction."""
if action == Direction.UP: if action == Direction.UP:
neighbour_i = self.i - 1 neighbour_i = self.i - 1
neighbour_j = self.j neighbour_j = self.j
@ -95,7 +101,7 @@ class Field:
return state.world[neighbour_i][neighbour_j] return state.world[neighbour_i][neighbour_j]
def is_accessible(self, team: Team) -> bool: def is_accessible(self, team: Team) -> bool:
"""If this field is accessible for the given team.""" """Jestli daný tým může vstoupit na políčko."""
if self.hill: if self.hill:
return False return False
@ -104,11 +110,16 @@ class Field:
return False return False
return True return True
# Celý stav hry včetně parsování a vypisování
class State: class State:
"""The entire game state.
Also contains methods for parsing and actions submission.
"""
def __init__(self, state: dict) -> None: def __init__(self, state: dict) -> None:
self.teams = [Team(i, i==state["team_id"]) for i in range(state["teams_count"])] self.teams = [Team(i, i == state["team_id"])
for i in range(state["teams_count"])]
self.world = self._parse_world(state["state"]["world"]) self.world = self._parse_world(state["state"]["world"])
self.my_team: Team = self.teams[state["team_id"]] self.my_team: Team = self.teams[state["team_id"]]
self.round_number: int = state["round"] self.round_number: int = state["round"]
@ -150,8 +161,9 @@ class State:
fields.append(fields_row) fields.append(fields_row)
return fields return fields
def build_turn(self) -> dict: def build_turn(self) -> dict:
"""Build a dictionary with member actions."""
for team in self.teams: for team in self.teams:
if not team.is_me: if not team.is_me:
for member in team.members: for member in team.members:
@ -164,10 +176,12 @@ class State:
] ]
} }
state: State state: State
# Algoritmy # Algoritmy
def find_fields(predicate: Callable[[Field], bool]) -> List[Field]: def find_fields(predicate: Callable[[Field], bool]) -> List[Field]:
"""Find all fields that satisfy the given predicate.""" """Find all fields that satisfy the given predicate."""
@ -203,18 +217,21 @@ def pathfind(member: Member, goal: Field) -> Direction:
explored.add(neighbour) explored.add(neighbour)
return Direction.STAY return Direction.STAY
# main (zde doplňte kód)
# Strategie
def main() -> None: def main() -> None:
global state global state
state = State(json.loads(sys.stdin.read())) state = State(json.loads(sys.stdin.read()))
# TODO: set actions for all yours members # TODO: zde doplňte svou herní strategii
# example: all members go up # příklad: všechny vojáky posílám nahoru
for member in state.teams[1].members: for member in state.teams[state.my_team].members:
member.action = Direction.UP member.action = Direction.UP
print(json.dumps(state.build_turn())) print(json.dumps(state.build_turn()))
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save