From f4f6ffd0218b6fe8af9fc78766a1c292de3f4c44 Mon Sep 17 00:00:00 2001 From: kulisak12 Date: Sun, 18 Sep 2022 10:12:00 +0200 Subject: [PATCH] =?UTF-8?q?Strategick=C3=A1:=20Akce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- klient/play.py | 59 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/klient/play.py b/klient/play.py index 47b4b0f..3a55ba6 100755 --- a/klient/play.py +++ b/klient/play.py @@ -1,7 +1,17 @@ #!/usr/bin/env python3 import json import sys -from typing import Callable, Iterable, List, Optional, Tuple +from __future__ import annotations +from enum import Enum +from typing import Callable, Iterable, List, Optional + + +class Action(Enum): + UP = "up" + DOWN = "down" + LEFT = "left" + RIGHT = "right" + STAY = None class State: @@ -11,18 +21,20 @@ class State: self.round_number: int = state["round"] -state: State = None +state: State class Member: - def __init__(self, field: 'Field', team: int, id: int): + def __init__(self, field: Field, team: int, id: int): self.field = field, self.team = team self.id = id - self.action: Optional[str] = None + self.action = Action.STAY class Field: + members: List[Member] + def __init__(self, i: int, j: int, hill: bool, home_for_team: Optional[int], occupied_by_team: Optional[int]): @@ -31,7 +43,36 @@ class Field: self.hill = hill self.home_for_team = home_for_team self.occupied_by_team = occupied_by_team - self.members: List[Member] = None + + def get_neighbour_field(self, action: Action) -> Field: + if action == Action.UP: + neighbour_i = self.i - 1 + neighbour_j = self.j + elif action == Action.DOWN: + neighbour_i = self.i + 1 + neighbour_j = self.j + elif action == Action.LEFT: + neighbour_i = self.i + neighbour_j = self.j - 1 + elif action == Action.RIGHT: + neighbour_i = self.i + neighbour_j = self.j + 1 + else: + neighbour_i = self.i + neighbour_j = self.j + # ensure coords are in bounds + neighbour_i %= len(state.world), + neighbour_j %= len(state.world[0]) + return state.world[neighbour_i][neighbour_j] + + def is_accessible(self, team: int) -> bool: + """If this field is accessible for the given team.""" + + if self.hill: + return False + if self.home_for_team is not None and self.home_for_team != team: + return False + return True def main() -> None: @@ -42,14 +83,12 @@ def main() -> None: # TODO: set actions for all members # example: all members go up for member in my_members: - member.action = "up" + member.action = Action.UP print(json.dumps(build_turn(my_members))) -def find_fields( - predicate: Callable[[Field], bool] -) -> List[Tuple[Field]]: +def find_fields(predicate: Callable[[Field], bool]) -> List[Field]: """Find all fields that satisfy the given predicate.""" result = [] @@ -60,7 +99,7 @@ def find_fields( return result -def find_team_members(team_id: int) -> List[Tuple[Member]]: +def find_team_members(team_id: int) -> List[Member]: """Find all members that belong to the given team.""" result = []