[4] Add player cycle to maze-core.py
This commit is contained in:
parent
51664b3273
commit
9536ba1378
|
|
@ -317,6 +317,10 @@ class ConsoleView(Observer):
|
||||||
print("\n Path not found!")
|
print("\n Path not found!")
|
||||||
return
|
return
|
||||||
print(f"\n Path found! Length: {len(path)}")
|
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:
|
class Player:
|
||||||
|
|
@ -405,4 +409,96 @@ class MazeSolver:
|
||||||
|
|
||||||
self.notify("path_found", path)
|
self.notify("path_found", path)
|
||||||
|
|
||||||
return SearchStats(time_ms, self._strategy.get_visited_count(), len(path))
|
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!")
|
||||||
Loading…
Reference in New Issue
Block a user