{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "492dd9b1-922e-4c95-9112-4b2df086eae6", "metadata": {}, "outputs": [], "source": [ "from typing import Optional\n", "from commandsCommand import Command\n", "from commandsPlayer import Player\n", "from modelsMaze import Maze\n", "from modelsCell import Cell\n", "\n", "class MoveCommand(Command):\n", " \"\"\"Команда перемещения игрока.\"\"\"\n", " \n", " # Направления\n", " DIRECTIONS = {\n", " 'w': (0, -1), # вверх\n", " 's': (0, 1), # вниз\n", " 'a': (-1, 0), # влево\n", " 'd': (1, 0), # вправо\n", " }\n", " \n", " def __init__(self, player: Player, maze: Maze, direction: str):\n", " self.player = player\n", " self.maze = maze\n", " self.direction = direction.lower()\n", " self._target_cell: Optional[Cell] = None\n", " self._executed = False\n", " \n", " def execute(self) -> bool:\n", " \"\"\"Выполнить перемещение.\"\"\"\n", " if self.direction not in self.DIRECTIONS:\n", " return False\n", " \n", " dx, dy = self.DIRECTIONS[self.direction]\n", " x = self.player.current_cell.x + dx\n", " y = self.player.current_cell.y + dy\n", " \n", " self._target_cell = self.maze.get_cell(x, y)\n", " \n", " if self._target_cell and self._target_cell.is_passable():\n", " self.player.move_to(self._target_cell)\n", " self._executed = True\n", " return True\n", " \n", " return False\n", " \n", " def undo(self) -> bool:\n", " \"\"\"Отменить перемещение.\"\"\"\n", " if self._executed:\n", " success = self.player.undo_move()\n", " if success:\n", " self._executed = False\n", " return True\n", " return False" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:base] *", "language": "python", "name": "conda-base-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.9" } }, "nbformat": 4, "nbformat_minor": 5 }