2026-rff_mp/stepushovgs/labyrinth/test.ipynb

370 lines
11 KiB
Plaintext
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.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4489fc7e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"S # ###\n",
"## # # E\n",
"# # #\n",
"### ## #\n",
"# #\n",
"########\n"
]
}
],
"source": [
"with open('test_lab.txt') as f:\n",
" data = f.readlines()\n",
" for el in data:\n",
" print(el.rstrip())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fde1eddb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"S # ###\n",
"## # # E\n",
"# # #\n",
"### ## #\n",
"# #\n",
"########\n"
]
}
],
"source": [
"from source.builder import TextFileMazeBuilder\n",
"\n",
"builder = TextFileMazeBuilder()\n",
"maze = builder.buildFromFile(filename='test_lab.txt')\n",
"\n",
"maze.printer()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "22325f68",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Загружен лабиринт:\n",
"S*P# ###\n",
"## # # E\n",
"# # #\n",
"### ## #\n",
"# #\n",
"########\n"
]
}
],
"source": [
"from source.observer.console_view import ConsoleView\n",
"from source.observer.observer import Event\n",
"\n",
"view = ConsoleView()\n",
"view.update(Event(\n",
" event=\"maze_loaded\",\n",
" maze=maze,\n",
" player_position=(2, 0),\n",
" path=[(0, 0), (1, 0)]\n",
"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19840429",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Путь найден:\n",
"S**# ###\n",
"##*# #*E\n",
"# ** #*#\n",
"###*##*#\n",
"# ****#\n",
"########\n"
]
},
{
"data": {
"text/plain": [
"([(2, 1), (1, 0)],\n",
" [(0, 0),\n",
" (1, 0),\n",
" (2, 0),\n",
" (2, 1),\n",
" (2, 2),\n",
" (3, 2),\n",
" (3, 3),\n",
" (3, 4),\n",
" (4, 4),\n",
" (5, 4),\n",
" (6, 4),\n",
" (6, 3),\n",
" (6, 2),\n",
" (6, 1),\n",
" (7, 1)])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from source.strategy import DFS, MazeSolver\n",
"# from source.strategy.maze_solver import \n",
"from source.classes import Cell\n",
"\n",
"\n",
"solver = MazeSolver(maze, DFS(), ConsoleView())\n",
"stats = solver.solve()\n",
"\n",
"[cord.getXY() for cord in maze.getNeighbors(cell=Cell(2, 0))], [cord.getXY() for cord in stats.path]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73ba37a8",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'module' object is not callable",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[9], line 6\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01msource\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mstrategy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmaze_solver\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m MazeSolver\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01msource\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mclasses\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcell\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Cell\n\u001b[1;32m----> 6\u001b[0m solver \u001b[38;5;241m=\u001b[39m MazeSolver(maze, \u001b[43mBFS\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m, ConsoleView())\n\u001b[0;32m 7\u001b[0m stats \u001b[38;5;241m=\u001b[39m solver\u001b[38;5;241m.\u001b[39msolve()\n\u001b[0;32m 9\u001b[0m [cord\u001b[38;5;241m.\u001b[39mgetXY() \u001b[38;5;28;01mfor\u001b[39;00m cord \u001b[38;5;129;01min\u001b[39;00m maze\u001b[38;5;241m.\u001b[39mgetNeighbors(cell\u001b[38;5;241m=\u001b[39mCell(\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m0\u001b[39m))], [cord\u001b[38;5;241m.\u001b[39mgetXY() \u001b[38;5;28;01mfor\u001b[39;00m cord \u001b[38;5;129;01min\u001b[39;00m stats\u001b[38;5;241m.\u001b[39mpath]\n",
"\u001b[1;31mTypeError\u001b[0m: 'module' object is not callable"
]
}
],
"source": [
"from source.strategy import BFS, MazeSolver\n",
"# from source.strategy import \n",
"from source.classes import Cell\n",
"\n",
"\n",
"solver = MazeSolver(maze, BFS(), ConsoleView())\n",
"stats = solver.solve()\n",
"\n",
"[cord.getXY() for cord in maze.getNeighbors(cell=Cell(2, 0))], [cord.getXY() for cord in stats.path]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "857c5c04",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"2\n",
"1\n",
"4\n",
"3\n",
"3\n"
]
},
{
"data": {
"text/plain": [
"{'0', '1', '2', '3', '4'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def dfs(graph, start, visited=None):\n",
" if visited is None:\n",
" visited = set()\n",
" visited.add(start)\n",
"\n",
" print(start)\n",
"\n",
" for next in graph[start] - visited:\n",
" dfs(graph, next, visited)\n",
" return visited\n",
"\n",
"\n",
"graph = {'0': set(['1', '2']),\n",
" '1': set(['0', '3', '4']),\n",
" '2': set(['0']),\n",
" '3': set(['1']),\n",
" '4': set(['2', '3'])}\n",
"\n",
"dfs(graph, '0')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9a5ea5cb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Размер: 8x6\n",
"(0,0): wall=False, start=True, exit=False\n",
"(1,0): wall=False, start=False, exit=False\n",
"(2,0): wall=False, start=False, exit=False\n",
"(3,0): wall=True, start=False, exit=False\n",
"(4,0): wall=False, start=False, exit=False\n",
"(5,0): wall=True, start=False, exit=False\n",
"(6,0): wall=True, start=False, exit=False\n",
"(7,0): wall=True, start=False, exit=False\n",
"(0,1): wall=True, start=False, exit=False\n",
"(1,1): wall=True, start=False, exit=False\n",
"(2,1): wall=False, start=False, exit=False\n",
"(3,1): wall=True, start=False, exit=False\n",
"(4,1): wall=False, start=False, exit=False\n",
"(5,1): wall=True, start=False, exit=False\n",
"(6,1): wall=False, start=False, exit=False\n",
"(7,1): wall=False, start=False, exit=True\n",
"(0,2): wall=True, start=False, exit=False\n",
"(1,2): wall=False, start=False, exit=False\n",
"(2,2): wall=False, start=False, exit=False\n",
"(3,2): wall=False, start=False, exit=False\n",
"(4,2): wall=False, start=False, exit=False\n",
"(5,2): wall=True, start=False, exit=False\n",
"(6,2): wall=False, start=False, exit=False\n",
"(7,2): wall=True, start=False, exit=False\n",
"(0,3): wall=True, start=False, exit=False\n",
"(1,3): wall=True, start=False, exit=False\n",
"(2,3): wall=True, start=False, exit=False\n",
"(3,3): wall=False, start=False, exit=False\n",
"(4,3): wall=True, start=False, exit=False\n",
"(5,3): wall=True, start=False, exit=False\n",
"(6,3): wall=False, start=False, exit=False\n",
"(7,3): wall=True, start=False, exit=False\n",
"(0,4): wall=True, start=False, exit=False\n",
"(1,4): wall=False, start=False, exit=False\n",
"(2,4): wall=False, start=False, exit=False\n",
"(3,4): wall=False, start=False, exit=False\n",
"(4,4): wall=False, start=False, exit=False\n",
"(5,4): wall=False, start=False, exit=False\n",
"(6,4): wall=False, start=False, exit=False\n",
"(7,4): wall=True, start=False, exit=False\n",
"(0,5): wall=True, start=False, exit=False\n",
"(1,5): wall=True, start=False, exit=False\n",
"(2,5): wall=True, start=False, exit=False\n",
"(3,5): wall=True, start=False, exit=False\n",
"(4,5): wall=True, start=False, exit=False\n",
"(5,5): wall=True, start=False, exit=False\n",
"(6,5): wall=True, start=False, exit=False\n",
"(7,5): wall=True, start=False, exit=False\n",
"\n",
"Клетка (2,0) из лабиринта: wall=True\n",
"Соседи (2,0): [(1, 2)]\n",
"Соседи (1,0): [(0, 0)]\n"
]
}
],
"source": [
"# Проверьте структуру лабиринта\n",
"print(f\"Размер: {maze.width}x{maze.height}\")\n",
"\n",
"# Проверьте конкретные клетки\n",
"for y in range(maze.height):\n",
" for x in range(maze.width):\n",
" cell = maze.cells[y][x]\n",
" print(f\"({x},{y}): wall={cell.isWall}, start={cell.isStart}, exit={cell.isExit}\")\n",
"\n",
"# Проверьте соседей конкретной клетки из лабиринта\n",
"cell_from_maze = maze.cells[2][0] # Берём реальную клетку из лабиринта\n",
"print(f\"\\nКлетка (2,0) из лабиринта: wall={cell_from_maze.isWall}\")\n",
"print(f\"Соседи (2,0): {[n.getXY() for n in maze.getNeighbors(cell_from_maze)]}\")\n",
"\n",
"# Проверьте соседей (1,0)\n",
"cell_1_0 = maze.cells[1][0]\n",
"print(f\"Соседи (1,0): {[n.getXY() for n in maze.getNeighbors(cell_1_0)]}\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "32edf4d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['S # ###', '## # # E', '# # #', '### ## #', '# #', '########']\n",
"8 6\n"
]
}
],
"source": [
"with open('test_lab.txt') as f:\n",
" data = f.read().splitlines()\n",
" x, y = 0, 0\n",
" width = len(data[0])\n",
" height = len(data)\n",
" print(data)\n",
" print(width, height)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48d20564",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}