def __init__(self, cell: Cell, beeper: Beeper): self.current_cell = cell self._facing = CD.NORTH self._beeper = beeper # Events self.turn_event = Event() self.no_beeper_event = Event()
class Counter: def __init__(self): self.tick_event = Event() self._count = 0 def tick(self): self._count += 1 self.tick_event.notify(self._count)
class Wall(CellLimit): def __init__(self): self.walk_into_event = Event() self.knock_event = Event() def walk_into(self) -> None: self.walk_into_event.notify() def knock(self) -> None: self.knock_event.notify()
class Beeper: def __init__(self): self.beep_event = Event() self.beeper_set_event = Event() self._is_set = False self._cell = None def count(self, count: int): if (count % 5 == 0 and self._is_set): self.beep_event.notify(self._cell.row, self._cell.col) def set_beeper(self, cell: Cell): self._is_set = True self._cell = cell self.beeper_set_event.notify()
class Player: _cardinal_directions = (CD.NORTH, CD.EAST, CD.SOUTH, CD.WEST) _relative_directions = ("Forward", "Right", "Backward", "Left") _turning_dict = {"Left": -1, "Right": 1} def __init__(self, cell: Cell, beeper: Beeper): self.current_cell = cell self._facing = CD.NORTH self._beeper = beeper # Events self.turn_event = Event() self.no_beeper_event = Event() def turn(self, direction: str) -> None: i = self._turning_dict[direction] current_index = self._cardinal_directions.index(self._facing) self._facing = self._cardinal_directions[(current_index + i) % len( self._cardinal_directions)] self.turn_event.notify(self._facing) def go_in_direction(self, direction: str) -> None: direction_index = self._relative_directions.index(direction) current_cardinal_index = self._cardinal_directions.index(self._facing) cardinal_index = (direction_index + current_cardinal_index) % 4 self.current_cell = self.current_cell.go_in_direction( self._cardinal_directions[cardinal_index]) def knock_in_direction(self, direction: str) -> None: direction_index = self._relative_directions.index(direction) current_cardinal_index = self._cardinal_directions.index(self._facing) cardinal_index = (direction_index + current_cardinal_index) % 4 self.current_cell.knock_in_direction( self._cardinal_directions[cardinal_index]) def set_beeper(self): if self._beeper: self._beeper.set_beeper(self.current_cell) self._beeper = None else: self.no_beeper_event.notify()
class Cell(CellLimit): def __init__( self, north_border: CellLimit, east_border: CellLimit, south_border: CellLimit, west_border: CellLimit, is_winning_cell: CellLimit, row: int, col: int ): self.borders = { CD.NORTH: north_border, CD.SOUTH: south_border, CD.EAST: east_border, CD.WEST: west_border, } self.is_winning_cell = is_winning_cell # This property is useful for creating mazes self.is_visited = False self.row = row self.col = col self._has_puddle = choices([True, False], cum_weights=[1, 11])[0] # Events self.puddle_event = Event() self.whoosh_event = Event() def go_in_direction(self, direction): next_cell = self.borders[direction].walk_into() if next_cell is not None: return next_cell else: return self def knock_in_direction(self, direction): self.borders[direction].knock() def knock(self) -> None: self.whoosh_event.notify() def walk_into(self): if self._has_puddle: self.puddle_event.notify() return self
def __init__( self, north_border: CellLimit, east_border: CellLimit, south_border: CellLimit, west_border: CellLimit, is_winning_cell: CellLimit, row: int, col: int ): self.borders = { CD.NORTH: north_border, CD.SOUTH: south_border, CD.EAST: east_border, CD.WEST: west_border, } self.is_winning_cell = is_winning_cell # This property is useful for creating mazes self.is_visited = False self.row = row self.col = col self._has_puddle = choices([True, False], cum_weights=[1, 11])[0] # Events self.puddle_event = Event() self.whoosh_event = Event()
def __init__(self, player: Player, maze: Maze): self._player = player self._maze = maze self.is_won = False # Events self.win_event = Event() self.sound_event = Event() self.puddle_event = Event() self.whoosh_event = Event() self.knock_event = Event() self.walk_into_event = Event() # Events subscription self._maze.wall.knock_event += self.knock_event.notify self._maze.wall.walk_into_event += self.walk_into_event.notify for row in self._maze.cells_grid: for cell in row: cell.puddle_event += self.puddle_event.notify cell.whoosh_event += self.whoosh_event.notify
def __init__(self): self.tick_event = Event() self._count = 0
def __init__(self): self.beep_event = Event() self.beeper_set_event = Event() self._is_set = False self._cell = None
class GameState: def __init__(self, player: Player, maze: Maze): self._player = player self._maze = maze self.is_won = False # Events self.win_event = Event() self.sound_event = Event() self.puddle_event = Event() self.whoosh_event = Event() self.knock_event = Event() self.walk_into_event = Event() # Events subscription self._maze.wall.knock_event += self.knock_event.notify self._maze.wall.walk_into_event += self.walk_into_event.notify for row in self._maze.cells_grid: for cell in row: cell.puddle_event += self.puddle_event.notify cell.whoosh_event += self.whoosh_event.notify def set_game_as_won(self): self.is_won = True self.win_event.notify() def beep_event(self, beeper_row: int, beeper_col: int): relative_direction = self._get_relative_direction( self._player.current_cell.row, self._player.current_cell.col, beeper_row, beeper_col) if relative_direction is not None: if relative_direction == "your place": is_muffled = False else: is_muffled = self._is_wall(self._player.current_cell, relative_direction) self.sound_event.notify( Sound("Beep", relative_direction, is_muffled)) def _is_wall(self, cell: Cell, relative_direction: str): return type(cell.borders[relative_direction]) == Wall def _get_relative_direction(self, first_row: int, first_col: int, second_row: int, second_col: int): """ Returns relative position of the second object relative to the first one, as long as they are adjacent. Diagonal cells are not considered adjacent. If they are not adjacent, returns None """ dif = (second_row - first_row, second_col - first_col) results = { (0, 0): "your place", (1, 0): CD.SOUTH, (-1, 0): CD.NORTH, (0, -1): CD.WEST, (0, 1): CD.EAST, } try: return results[dif] except KeyError: return None
def __init__(self): self.walk_into_event = Event() self.knock_event = Event()