-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbinary.py
More file actions
61 lines (48 loc) · 1.53 KB
/
binary.py
File metadata and controls
61 lines (48 loc) · 1.53 KB
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
# generates a square maze (size*size) with the binary tree technique
import numpy as np
def carve_maze(grid:np.ndarray, size:int) -> np.ndarray:
output_grid = np.empty([size*3, size*3],dtype=str)
output_grid[:] = '#'
i = 0
j = 0
while i < size:
w = i*3 + 1
while j < size:
k = j*3 + 1
toss = grid[i,j]
output_grid[w,k] = ' '
if toss == 0 and k+2 < size*3:
output_grid[w,k+1] = ' '
output_grid[w,k+2] = ' '
if toss == 1 and w-2 >=0:
output_grid[w-1,k] = ' '
output_grid[w-2,k] = ' '
j = j + 1
i = i + 1
j = 0
return output_grid
def preprocess_grid(grid:np.ndarray, size:int) -> np.ndarray:
# fix first row and last column to avoid digging outside the maze external borders
first_row = grid[0]
first_row[first_row == 1] = 0
grid[0] = first_row
for i in range(1,size):
grid[i,size-1] = 1
return grid
def main():
n=1
p=0.5
size=5
# 1 (head) N, 0 (tail) E
# np.random.seed(42)
grid = np.random.binomial(n,p, size=(size,size))
# print('grid')
# print(grid)
processed_grid = preprocess_grid(grid, size)
# print('processed_grid')
# print(processed_grid)
output = carve_maze(processed_grid, size)
for elm in output:
print(" ".join(elm))
if __name__ == '__main__':
main()