-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
119 lines (87 loc) · 3.24 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from tile import Tile
class Maze:
def __init__(self, path):
self.__player = (0, 0)
self.__maze = []
f = open(path, "r")
csv = f.read()
lines = csv.split("\n")
for y in range(len(lines)):
maze_line = []
line = lines[y]
tiles = line.split(",")
for x in range(len(tiles)):
tile = tiles[x]
maze_line.append(Tile(x, y, int(tile)))
self.__maze.append(maze_line)
self.__maze[0][0].set_is_path(True)
def get_player_coordinates(self):
return self.__player
def get_player_tile(self):
x, y = self.__player
return self.get_tile(x, y)
def get_tile(self, x, y):
return self.__maze[y][x]
def get_all_tiles(self):
return [tile for line in self.__maze for tile in line]
def get_player_adjacent_tiles(self):
x, y = self.__player
return self.get_adjacent_tiles(x, y)
def get_adjacent_tiles(self, x, y):
coordinates = [(x, y - 1), (x, y + 1), (x - 1, y), (x + 1, y)]
tiles = []
for (x, y) in coordinates:
if 0 <= x < len(self.__maze[0]) and 0 <= y < len(self.__maze):
tile = self.__maze[y][x]
tiles.append(tile)
return tiles
def get_has_won(self):
x, y = self.__player
return self.__maze[y][x].get_is_goal()
def move(self, direction):
new_x, new_y = self.__get_new_coordinates(direction)
if new_x is None or new_y is None:
return False
destination = self.__maze[new_y][new_x]
if not destination.get_is_wall():
self.__player = (new_x, new_y)
destination.set_is_path(True)
return True
return False
def display(self, mood="happy"):
output = "██" * (len(self.__maze[0]) + 2) + "\n"
for y in range(len(self.__maze)):
line_display = ""
for x in range(len(self.__maze[y])):
tile = self.__maze[y][x]
if self.__player == (x, y):
if mood == "sad":
line_display += ":("
elif mood == "overjoyed":
line_display += ":D"
else:
line_display += ":)"
elif tile.get_is_path():
line_display += "░░"
elif tile.get_is_empty():
line_display += " "
elif tile.get_is_wall():
line_display += "██"
elif tile.get_is_goal():
line_display += "##"
output += "██" + line_display + "██\n"
output += "██" * (len(self.__maze[0]) + 2)
return output
def __get_new_coordinates(self, direction):
new_x, new_y = self.__player
if direction == "up":
new_y -= 1
elif direction == "down":
new_y += 1
elif direction == "left":
new_x -= 1
elif direction == "right":
new_x += 1
if new_x < 0 or new_x >= len(self.__maze[0]) or new_y < 0 or new_y >= len(self.__maze):
return None, None
return new_x, new_y