-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.py
152 lines (120 loc) · 4.7 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from random import randrange, shuffle
import math
AGENT = "a"
FOOD = "f"
WALL = "*"
SPACE = "-"
maze_folder = "envirements//"
class MAZE:
def __init__(self, filename):
try:
self.maze = self.read_maze_from_file(filename)
except:
self.save_maze_in_file(filename)
self.maze = self.read_maze_from_file(filename)
__agentPos = [-1, -1]
__foodPos = [-1, -1]
def getAgentPosition(self):
return self.__agentPos
def get_agent_and_food_poss_from_file(self):
return self.__agentPos, self.__foodPos
def getAgentPossRandomly(self):
while True:
agent_poss_x = randrange(1, len(self.maze) - 1)
agent_poss_y = randrange(1, len(self.maze[agent_poss_x]) - 1)
if(self.maze[agent_poss_x][agent_poss_y] == SPACE):
break
return [agent_poss_x, agent_poss_y]
def getAgentAndFoodPossRandomly(self):
agent = self.getAgentPossRandomly()
while True:
food_poss_x = randrange(1, len(self.maze) - 1)
food_poss_y = randrange(1, len(self.maze[food_poss_x]) - 1)
if(self.maze[food_poss_x][food_poss_y] == SPACE):
if not(food_poss_x == agent[0] and food_poss_y == agent[1]):
break
food = [food_poss_x, food_poss_y]
return [agent, food]
def edit_block(self, x, y, value): self.maze[x][y] = value
def update_agent_location(self, agent_pos, x, y):
self.edit_block(agent_pos[0], agent_pos[1], SPACE)
self.edit_block(x, y, AGENT)
def printMaze(self):
for row in self.maze:
for col in row:
print(col, end="")
print()
def is_food(self, x, y): return self.maze[x][y] == FOOD
def is_space(self, x, y): return self.maze[x][y] == SPACE
def agent_not_found(self):
return self.__agentPos[0] == -1 or self.__foodPos[0] == -1
def generate_random_position_for_agent_and_food(self):
self.__agentPos, self.__foodPos = self.getAgentAndFoodPossRandomly()
def read_maze_from_file(self, filename):
maze = []
mazeFile = open(maze_folder + filename, "r")
columns = mazeFile.readlines()
x = 0
for column in columns:
column = column.strip()
row = [i for i in column]
maze.append(row)
y = 0
for i in row:
if(i == AGENT):
self.__agentPos[0] = x
self.__agentPos[1] = y
elif(i == FOOD):
self.__foodPos[0] = x
self.__foodPos[1] = y
y += 1
x += 1
self.maze = maze
if(self.agent_not_found()):
self.generate_random_position_for_agent_and_food()
self.update_maze()
return maze
def update_maze(self):
self.edit_block(self.__agentPos[0], self.__agentPos[1], AGENT)
self.edit_block(self.__foodPos[0], self.__foodPos[1], FOOD)
def checkNumbersAreStandardFodMaze(self,_w, _h):
if(_w < 5):
_w = 5
if(_h < 5):
_h = 5
return _w, _h
def generate_maze(self, r=5, c=5):
r, c = self.checkNumbersAreStandardFodMaze(r, c)
col = math.ceil(r/2) - 1
row = math.ceil(c/2) - 1
vis = [[0] * row + [1] for _ in range(col)] + [[1] * (row + 1)]
ver = [["*-"] * row + ['*'] for _ in range(col)] + [[]]
hor = [["**"] * row + ['*'] for _ in range(col + 1)]
# vis = [[0] * col + [1] for _ in range(row)] + [[1] * (col + 1)]
# ver = [["*-"] * col + ['*'] for _ in range(row)] + [[]]
# hor = [["**"] * col + ['*'] for _ in range(row + 1)]
def walk(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]:
continue
if xx == x:
hor[max(y, yy)][x] = "*-"
if yy == y:
ver[y][max(x, xx)] = "--"
walk(xx, yy)
walk(randrange(col), randrange(row))
s = f'{r},{c}\n'
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
return s
def save_maze_in_file(self, filename):
r, c = input("insert m , n: ").split()
row = int(r)
column = int(c)
maze = self.generate_maze(row, column)
text_file = open((maze_folder + filename), "w")
text_file.write(maze)
text_file.close()