forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
32 lines (32 loc) · 1.4 KB
/
maze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'''
This file contains the Maze class that represents the maze in the game.
'''
import random
import tkinter as tk
class Maze:
def __init__(self):
self.rows = 10
self.cols = 10
self.maze = [[0] * self.cols for _ in range(self.rows)]
self.player_row = 0
self.player_col = 0
self.goal_row = self.rows - 1
self.goal_col = self.cols - 1
self.generate_maze()
def generate_maze(self):
# Generate a random maze
for row in range(self.rows):
for col in range(self.cols):
if random.random() < 0.3:
self.maze[row][col] = 1
def move_player(self, direction):
if direction == "up" and self.player_row > 0 and self.maze[self.player_row-1][self.player_col] != 1:
self.player_row -= 1
elif direction == "down" and self.player_row < self.rows-1 and self.maze[self.player_row+1][self.player_col] != 1:
self.player_row += 1
elif direction == "left" and self.player_col > 0 and self.maze[self.player_row][self.player_col-1] != 1:
self.player_col -= 1
elif direction == "right" and self.player_col < self.cols-1 and self.maze[self.player_row][self.player_col+1] != 1:
self.player_col += 1
if self.player_row == self.goal_row and self.player_col == self.goal_col:
print("Congratulations! You have reached the goal!")