39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
#!/usr/bin/env python
|
|
# coding: utf-8
|
|
|
|
# In[ ]:
|
|
|
|
|
|
from typing import Optional
|
|
from modelsMaze import Maze
|
|
from modelsCell import Cell
|
|
|
|
class Player:
|
|
"""Игрок, перемещающийся по лабиринту."""
|
|
|
|
def __init__(self, maze: Maze, start_cell: Cell):
|
|
self.maze = maze
|
|
self.current_cell = start_cell
|
|
self._previous_cell: Optional[Cell] = None
|
|
|
|
def move_to(self, cell: Cell) -> bool:
|
|
"""Переместить игрока в указанную клетку (если она проходима)."""
|
|
if cell and cell.is_passable():
|
|
self._previous_cell = self.current_cell
|
|
self.current_cell = cell
|
|
return True
|
|
return False
|
|
|
|
def undo_move(self) -> bool:
|
|
"""Отменить последнее перемещение."""
|
|
if self._previous_cell:
|
|
self.current_cell = self._previous_cell
|
|
self._previous_cell = None
|
|
return True
|
|
return False
|
|
|
|
@property
|
|
def position(self) -> Cell:
|
|
return self.current_cell
|
|
|