Добавление modelsMaze
This commit is contained in:
parent
8ea4281568
commit
3d10d8f6b8
|
|
@ -1,58 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3a6bb811-80ad-4ef5-95b6-0b7cffc6545e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from dataclasses import dataclass\n",
|
||||
"from typing import Optional\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Cell:\n",
|
||||
" \"\"\"Клетка лабиринта.\"\"\"\n",
|
||||
" x: int\n",
|
||||
" y: int\n",
|
||||
" is_wall: bool = False\n",
|
||||
" is_start: bool = False\n",
|
||||
" is_exit: bool = False\n",
|
||||
" weight: int = 1 # Для взвешенных лабиринтов (доп. задание)\n",
|
||||
" \n",
|
||||
" def is_passable(self) -> bool:\n",
|
||||
" \"\"\"Проходима ли клетка.\"\"\"\n",
|
||||
" return not self.is_wall\n",
|
||||
" \n",
|
||||
" def __hash__(self) -> int:\n",
|
||||
" return hash((self.x, self.y))\n",
|
||||
" \n",
|
||||
" def __eq__(self, other) -> bool:\n",
|
||||
" if not isinstance(other, Cell):\n",
|
||||
" return False\n",
|
||||
" return self.x == other.x and self.y == other.y"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
82
MininaVD/docs2/data2/modelsMaze.ipynb
Normal file
82
MininaVD/docs2/data2/modelsMaze.ipynb
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
{
|
||||
"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
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user