{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "bc164689-22b6-4efd-bdd7-6a9913be9303", "metadata": {}, "outputs": [], "source": [ "from typing import List, Optional, Tuple\n", "from modelsCell import Cell\n", "\n", "class Maze:\n", " \"\"\"Модель лабиринта.\"\"\"\n", " \n", " def __init__(self, width: int = 0, height: int = 0):\n", " self.width = width\n", " self.height = height\n", " self._cells: List[List[Optional[Cell]]] = [\n", " [None for _ in range(width)] for _ in range(height)\n", " ]\n", " self.start_cell: Optional[Cell] = None\n", " self.exit_cell: Optional[Cell] = None\n", " \n", " def set_cell(self, x: int, y: int, cell: Cell) -> None:\n", " \"\"\"Установить клетку.\"\"\"\n", " if 0 <= x < self.width and 0 <= y < self.height:\n", " self._cells[y][x] = cell\n", " \n", " def get_cell(self, x: int, y: int) -> Optional[Cell]:\n", " \"\"\"Получить клетку по координатам.\"\"\"\n", " if 0 <= x < self.width and 0 <= y < self.height:\n", " return self._cells[y][x]\n", " return None\n", " \n", " def get_neighbors(self, cell: Cell) -> List[Cell]:\n", " \"\"\"Получить проходимых соседей клетки (вверх, вниз, влево, вправо).\"\"\"\n", " neighbors = []\n", " directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] # вверх, вниз, влево, вправо\n", " \n", " for dx, dy in directions:\n", " nx, ny = cell.x + dx, cell.y + dy\n", " neighbor = self.get_cell(nx, ny)\n", " if neighbor and neighbor.is_passable():\n", " neighbors.append(neighbor)\n", " \n", " return neighbors\n", " \n", " def get_all_cells(self) -> List[Cell]:\n", " \"\"\"Получить все клетки лабиринта.\"\"\"\n", " cells = []\n", " for y in range(self.height):\n", " for x in range(self.width):\n", " cell = self.get_cell(x, y)\n", " if cell:\n", " cells.append(cell)\n", " return cells" ] } ], "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 }