From 9536ba13782ebe686aafc5955e4e9bd2d2692a69 Mon Sep 17 00:00:00 2001 From: KislyuninED Date: Mon, 18 May 2026 12:21:44 +0000 Subject: [PATCH] [4] Add player cycle to maze-core.py --- .../docks/data/2-st-exercize/maze-core.py | 98 ++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/KislyuninED/docks/data/2-st-exercize/maze-core.py b/KislyuninED/docks/data/2-st-exercize/maze-core.py index 065344e..0d08a42 100644 --- a/KislyuninED/docks/data/2-st-exercize/maze-core.py +++ b/KislyuninED/docks/data/2-st-exercize/maze-core.py @@ -317,6 +317,10 @@ class ConsoleView(Observer): print("\n Path not found!") return print(f"\n Path found! Length: {len(path)}") + + def render_player(self, player_cell): + if self._player: + self.render_maze_with_player(self._player._maze) class Player: @@ -405,4 +409,96 @@ class MazeSolver: self.notify("path_found", path) - return SearchStats(time_ms, self._strategy.get_visited_count(), len(path)) \ No newline at end of file + return SearchStats(time_ms, self._strategy.get_visited_count(), len(path)) + + +def run_experiment(maze_file, strategy, runs=5): + builder = TextFileMazeBuilder() + maze = builder.build_from_file(maze_file) + + total_time = 0 + total_visited = 0 + total_length = 0 + + for _ in range(runs): + solver = MazeSolver(maze) + solver.set_strategy(strategy) + stats = solver.solve() + if stats: + total_time += stats.time_ms + total_visited += stats.visited_cells + total_length += stats.path_length + + return { + 'time_ms': total_time / runs, + 'visited_cells': total_visited / runs, + 'path_length': total_length / runs + } + + +if __name__ == "__main__": + if len(sys.argv) > 1 and sys.argv[1] == 'experiment': + print("Running experiments...") + sys.exit(0) + + builder = TextFileMazeBuilder() + maze = builder.build_from_file("maze1.txt") + + player = Player(maze.start, maze) + view = ConsoleView(player) + view.render_maze(maze) + + solver = MazeSolver(maze) + solver.attach(view) + + print("\n CONTROLS:") + print(" H (left) J (down) K (up) L (right)") + print(" U - undo Q - quit") + print("\n AUTO SEARCH:") + print(" B - BFS D - DFS A - A*") + print("\n" + "=" * 50) + + command_stack = [] + + while True: + key = input("\n Command > ").lower() + + if key == 'q': + print("\n Goodbye!") + break + elif key == 'b': + solver.set_strategy(BFSStrategy()) + stats = solver.solve() + print(f"\n BFS: time={stats.time_ms:.3f}ms, visited={stats.visited_cells}, length={stats.path_length}") + elif key == 'd': + solver.set_strategy(DFSStrategy()) + stats = solver.solve() + print(f"\n DFS: time={stats.time_ms:.3f}ms, visited={stats.visited_cells}, length={stats.path_length}") + elif key == 'a': + solver.set_strategy(AStarStrategy()) + stats = solver.solve() + print(f"\n A*: time={stats.time_ms:.3f}ms, visited={stats.visited_cells}, length={stats.path_length}") + elif key in ['h', 'j', 'k', 'l']: + dirs = {'h': (-1, 0), 'l': (1, 0), 'k': (0, -1), 'j': (0, 1)} + cmd = MoveCommand(player, dirs[key], maze) + if cmd.execute(): + command_stack.append(cmd) + view.render_maze_with_player(maze) + if player.current == maze.exit: + print("\n CONGRATULATIONS! YOU FOUND THE EXIT!") + print(f" Total moves: {len(command_stack)}") + break + else: + print("\n Cannot go there! It's a wall.") + elif key == 'u': + if command_stack: + cmd = command_stack.pop() + cmd.undo() + view.render_maze_with_player(maze) + print("\n Undo last move") + else: + print("\n Nothing to undo") + else: + print("\n Unknown command. Use h,j,k,l to move, u to undo, q to quit") + + print("\n Game over. Thanks for playing!") \ No newline at end of file