import random class MazeGame: def __init__(self): self.current_position = (0, 0) self.exit_position = (4, 4) self.generate_maze() def generate_maze(self): # For simplicity, we'll use a 5x5 grid for the maze self.maze = [[' ' for _ in range(5)] for _ in range(5)] self.maze[self.current_position[0]][self.current_position[1]] = 'S' self.maze[self.exit_position[0]][self.exit_position[1]] = 'E' def print_maze(self): for row in self.maze: print(' '.join(row)) print() def move(self, direction): x, y = self.current_position if direction == 'north' and x > 0: x -= 1 elif direction == 'south' and x < 4: x += 1 elif direction == 'west' and y > 0: y -= 1 elif direction == 'east' and y < 4: y += 1 self.maze[self.current_position[0]][self.current_position[1]] = ' ' self.current_position = (x, y) self.maze[self.current_position[0]][self.current_position[1]] = 'S' def play(self): print("Welcome to the Maze Game!") while self.current_position != self.exit_position: self.print_maze() direction = input("Enter your move (north/south/east/west): ").lower() if direction in ['north', 'south', 'east', 'west']: self.move(direction) else: print("Invalid move. Please enter a valid direction.") print("Congratulations! You've reached the exit!") # Run the game maze_game = MazeGame() maze_game.play()