-
Notifications
You must be signed in to change notification settings - Fork 0
/
blobby_algorithm.py
256 lines (216 loc) Β· 8.91 KB
/
blobby_algorithm.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from randomdict import RandomDict
import sys, time, random
# Make set S with set A and set B
# Take a random cell from S
# check its neighbours and add it to subset
# index = y*width + x
height = 20
width = 40
FPS = 0
seed = 1
ROOM_SIZE = 4
# random.seed(seed)
TOP_LEFT_CORNER = u'\u2554' # β
TOP_RIGHT_CORNER = u'\u2557' # β
BOT_LEFT_CORNER = u'\u255a' # β
BOT_RIGHT_CORNER = u'\u255d' # β
VERT_WALL = u'\u2551' # β
HORZ_WALL = u'\u2550' # β
VERT_WALL_RIGHT = u'\u2560' # β
VERT_WALL_LEFT = u'\u2563' # β£
HORZ_WALL_UP = u'\u2569' # β©
HORZ_WALL_DOWN = u'\u2566' # β¦
WALL_ALL = u'\u256c' # β¬
SPACE = " "
N,S,W,E = -width, width, -1, 1
NO, DOWN, RIGHT, ALL = 0,1,2,3
MOVE_X = {N : 0, S : 0, W : -1, E : 1}
MOVE_Y = {N : -1, S : 1, W : 0, E : 0}
set_s = RandomDict()
grid = [[0]*width for i in range(height)]
for y in range(height):
for x in range(width):
set_s[y*width + x] = (x,y)
if x == width-1:
grid[y][x] |= RIGHT
if y == height-1:
grid[y][x] |= DOWN
# S = {A : {}, B : {}}
# S = {0: (0,0), 1 (0,1), ..., 99 : (9,9)}
# U = {A : }
def print_grid(grid, set_a_path_grid, set_b_path_grid):
height, width = len(grid), len(grid[0])
maze_str = ""
maze_str += TOP_LEFT_CORNER # β Always first
for x in range(width - 1):
# ββββ¦ if there is a vertical wall below it else ββββ
maze_str += HORZ_WALL*3 + HORZ_WALL_DOWN if grid[0][x] & RIGHT != 0 else HORZ_WALL*4
maze_str += HORZ_WALL*3 + TOP_RIGHT_CORNER + "\n" # ββββ # Always last
for y in range(height):
first_line = VERT_WALL # β, Always like this
second_line = VERT_WALL_RIGHT if grid[y][0] & DOWN != 0 else VERT_WALL
if y == height - 1:
second_line = BOT_LEFT_CORNER # β, if we reach the end
for x in range(width):
# " β" if there needs to be a wall else " "
if set_a_path_grid[y][x]:
path_chr = u'β'
elif set_b_path_grid[y][x]:
path_chr = u'β'
else:
path_chr = SPACE
first_line += path_chr*3 + VERT_WALL if grid[y][x] & RIGHT != 0 else path_chr*4
if grid[y][x] == (RIGHT + DOWN):
# ββββ | ββββ£ |ββββ¬ | ββββ©
try:
if grid[y+1][x] & RIGHT != 0 and grid[y][x+1] & DOWN != 0:
second_line += HORZ_WALL*3 + WALL_ALL # ββββ¬
elif grid[y+1][x] & RIGHT != 0:
second_line += HORZ_WALL*3 + VERT_WALL_LEFT # ββββ£
elif grid[y][x+1] & DOWN != 0:
second_line += HORZ_WALL*3 + HORZ_WALL_UP # ββββ©
else:
raise
except:
if y != height - 1 and x == width - 1:
second_line += HORZ_WALL*3 + VERT_WALL_LEFT # ββββ£
elif y == height -1 and x != width - 1:
second_line += HORZ_WALL*3 + HORZ_WALL_UP # ββββ©
else:
second_line += HORZ_WALL*3 + BOT_RIGHT_CORNER # ββββ
elif grid[y][x] == RIGHT:
# " β" | " β" | " β "
try:
if grid[y+1][x] & RIGHT != 0 and grid[y][x+1] & DOWN != 0:
second_line += path_chr*3 + VERT_WALL_RIGHT # " β "
elif grid[y][x+1] & DOWN != 0:
second_line += path_chr*3 + BOT_LEFT_CORNER # " β"
else:
raise
except:
second_line += path_chr*3 + VERT_WALL # " β"
elif grid[y][x] == DOWN:
# ββββ | ββββ | ββββ¦
try:
if grid[y+1][x] & RIGHT != 0 and grid[y][x+1] & DOWN != 0:
second_line += HORZ_WALL*3 + HORZ_WALL_DOWN # ββββ¦
elif grid[y+1][x] & RIGHT != 0:
second_line += HORZ_WALL*3 + TOP_RIGHT_CORNER # ββββ
else:
raise
except:
second_line += HORZ_WALL*4 # ββββ
else:
# " β" | " β" | " β"
try:
if grid[y][x+1] & DOWN != 0 and grid[y+1][x] & RIGHT == 0:
second_line += path_chr*3 + HORZ_WALL # " β"
elif grid[y][x+1] & DOWN != 0:
second_line += path_chr*3 + TOP_LEFT_CORNER # " β"
elif grid[y+1][x] & RIGHT != 0:
second_line += path_chr*3 + VERT_WALL # " β"
else:
raise
except:
second_line += path_chr*4 # " "
maze_str += first_line + "\n"
maze_str += second_line + "\n"
sys.stdout.write("\r" + "\n"*(51-(width*2 + 1)) + "{}".format("\n" + maze_str))
time.sleep(FPS)
def blobby_recursive(grid, set_s):
if len(set_s) < ROOM_SIZE:
return None
set_a_path_grid = [[False]*len(grid[0]) for i in range(len(grid))]
set_b_path_grid = [[False]*len(grid[0]) for i in range(len(grid))]
print_grid(grid, set_a_path_grid, set_b_path_grid)
set_u = RandomDict()
x_1 ,y_1 = set_s.random_value()
del set_s[y_1*width + x_1]
x_2, y_2 = set_s.random_value()
del set_s[y_2*width + x_2]
set_u["A"] = RandomDict()
set_u["B"] = RandomDict()
set_u["A"][y_1*width + x_1] = (x_1, y_1)
set_u["B"][y_2*width + x_2] = (x_2, y_2)
set_a_path_grid[y_1][x_1] = True
set_b_path_grid[y_2][x_2] = True
set_a = RandomDict()
set_b = RandomDict()
print_grid(grid, set_a_path_grid, set_b_path_grid)
path_count = 0
while set_u["A"] or set_u["B"]:
random_set_str = "A" if random.randint(0,1) == 0 else "B"
try:
c_x, c_y = set_u[random_set_str].random_value()
except:
random_set_str = "A" if random_set_str == "B" else "B"
c_x, c_y = set_u[random_set_str].random_value()
del set_u[random_set_str][c_y*width + c_x]
if random_set_str == "A":
set_a[c_y*width + c_x] = (c_x, c_y)
else:
set_b[c_y*width + c_x] = (c_x, c_y)
for direction in [N,S,E,W]:
try:
if set_s[(c_y*width + c_x) + direction]:
path_count += 1
nx, ny = c_x + MOVE_X[direction], c_y + MOVE_Y[direction]
if 0 <= nx < width and 0 <= y < height:
set_u[random_set_str][(c_y*width + c_x) + direction] = (nx, ny)
if random_set_str == "A":
set_a_path_grid[ny][nx] = True
else:
set_b_path_grid[ny][nx] = True
del set_s[(c_y*width + c_x) + direction]
else:
raise
except:
continue
if path_count >= 8:
print_grid(grid, set_a_path_grid, set_b_path_grid)
path_count = 0
wall_cords = make_walls(set_a, set_b, grid)
wall_cords += make_walls(set_b, set_a, grid)
if len(wall_cords) > 0:
print_grid(grid, set_a_path_grid, set_b_path_grid)
r_x, r_y, wall_direction = random.choice(wall_cords)
grid[r_y][r_x] ^= wall_direction
if len(wall_cords) > 0:
print_grid(grid, set_a_path_grid, set_b_path_grid)
set_max = set_a if len(set_a) >= len(set_b) else set_b
set_min = set_b if len(set_a) >= len(set_b) else set_a
blobby_recursive(grid, set_a)
blobby_recursive(grid, set_b)
def make_walls(set_to_check, set_to_observe, grid):
wall_coords = []
for index, coordinates in set_to_check.items():
x,y = coordinates
try:
if x != width - 1:
set_to_observe[index + E]
grid[y][x] |= RIGHT
wall_coords.append((x,y,RIGHT))
except:
pass
try:
if y != height - 1:
set_to_observe[index + S]
grid[y][x] |= DOWN
wall_coords.append((x,y,DOWN))
except:
pass
return wall_coords
while True:
set_s = RandomDict()
grid = [[0]*width for i in range(height)]
for y in range(height):
for x in range(width):
set_s[y*width + x] = (x,y)
if x == width-1:
grid[y][x] |= RIGHT
if y == height-1:
grid[y][x] |= DOWN
blobby_recursive(grid, set_s)
set_a_path_grid = [[False]*len(grid[0]) for i in range(len(grid))]
set_b_path_grid = [[False]*len(grid[0]) for i in range(len(grid))]
print_grid(grid, set_a_path_grid, set_b_path_grid)