2026-rff_mp/ivantsovma/maze/visualization/game_controller.py

38 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from .observer import Observable, Event, EventType
from .command import MoveCommand, Player
class GameController(Observable):
def __init__(self, maze):
super().__init__()
self.maze = maze
self.player = Player(maze.start)
self.command_history = []
def move(self, direction):
"""Перемещает игрока в направлении"""
cmd = MoveCommand(self.player, direction, self.maze, self)
if cmd.execute():
self.command_history.append(cmd)
# Правильный вызов notify с одним аргументом Event
self.notify(Event(EventType.PLAYER_MOVED, self.player.current_cell))
return True
return False
def undo(self):
"""Отменяет последнее действие"""
if self.command_history:
cmd = self.command_history.pop()
cmd.undo()
self.notify(Event(EventType.UNDO, None))
return True
return False
def reset(self):
"""Сбрасывает игру"""
self.player.reset()
self.command_history.clear()
self.notify(Event(EventType.PLAYER_MOVED, self.player.current_cell))
def get_player_position(self):
"""Возвращает позицию игрока"""
return self.player.current_cell