{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "4dbe48b6", "metadata": {}, "outputs": [], "source": [ "from source.builder import TextFileMazeBuilder\n", "from source.observer import ConsoleView, Event\n", "from source.strategy import MazeSolver, BFS, DFS, Dijkstra, AStar\n", "# from source.strategy.maze_solver import \n", "from source.classes import Cell\n", "# from source.strategy import " ] }, { "cell_type": "code", "execution_count": null, "id": "007bf97a", "metadata": {}, "outputs": [], "source": [ "test_lab = './mazes/tests/test_lab.txt'\n", "test_lab2 = './mazes/tests/test_lab2.txt'\n", "test_lab3 = './mazes/tests/test_lab3.txt'\n", "test_lab4 = './mazes/tests/test_lab20x20.txt'\n", "test_labNoPath = './mazes/tests/test_labNoPath.txt'" ] }, { "cell_type": "code", "execution_count": null, "id": "4489fc7e", "metadata": {}, "outputs": [], "source": [ "with open(test_lab) 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": [], "source": [ "\n", "\n", "builder = TextFileMazeBuilder()\n", "maze = builder.buildFromFile(filename=test_lab)\n", "\n", "maze.printer()" ] }, { "cell_type": "code", "execution_count": null, "id": "22325f68", "metadata": {}, "outputs": [], "source": [ "\n", "# from source.observer.observer import \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": [], "source": [ "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": [], "source": [ "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": null, "id": "857c5c04", "metadata": {}, "outputs": [], "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": null, "id": "9a5ea5cb", "metadata": {}, "outputs": [], "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": null, "id": "32edf4d1", "metadata": {}, "outputs": [], "source": [ "with open(test_lab) 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": "dc7708c7", "metadata": {}, "outputs": [], "source": [ "maze = builder.buildFromFile(test_labNoPath)\n", "\n", "strats = [BFS(), DFS(), AStar(), Dijkstra()]\n", "\n", "for strat in strats:\n", " solver = MazeSolver(maze, strat, ConsoleView())\n", " print(solver.strategyName())\n", " stats = solver.solve()\n", " stats.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "48d20564", "metadata": {}, "outputs": [], "source": [ "maze2 = builder.buildFromFile(test_lab3)\n", "\n", "solver = MazeSolver(maze2, BFS(), ConsoleView())\n", "print(solver.strategyName())\n", "stats = solver.solve()\n", "stats.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "bf13d5ba", "metadata": {}, "outputs": [], "source": [ "maze2 = builder.buildFromFile(test_lab3)\n", "\n", "solver = MazeSolver(maze2, DFS(), ConsoleView())\n", "print(solver.strategyName())\n", "stats = solver.solve()\n", "stats.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "9383cb75", "metadata": {}, "outputs": [], "source": [ "maze2 = builder.buildFromFile(test_lab3)\n", "\n", "solver = MazeSolver(maze2, Dijkstra(), ConsoleView())\n", "print(solver.strategyName())\n", "stats = solver.solve()\n", "stats.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "835cff61", "metadata": {}, "outputs": [], "source": [ "maze2 = builder.buildFromFile('mazes\\\\benchmarks\\maze50x50.txt')\n", "\n", "solver = MazeSolver(maze2, AStar(), ConsoleView())\n", "print(solver.strategyName())\n", "stats = solver.solve()\n", "stats.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "2d84a151", "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 }