forked from aycock/entombed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmazegen.py
147 lines (108 loc) · 2.88 KB
/
mazegen.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
#!/usr/bin/env python
# Python 3
# see LICENSE file for licensing information
import random
def get_random_bit():
return random.randint(0, 1)
def left_random_bit():
return get_random_bit()
def right_random_bit():
return get_random_bit()
def mid_random_bit():
return get_random_bit()
def generated(x):
pass
solid = 'XX'
empty = '__'
def pr_row(seed):
pf12 = ''
for i in range(8):
if seed & 1:
pf12 = solid + pf12
else:
pf12 = empty + pf12
seed >>= 1
pf012 = solid * 2 + pf12
print(pf012, pf012[::-1])
# the mystery table from Entombed
MAGIC = {
(0b00, 0b000): 1,
(0b00, 0b001): 1,
(0b00, 0b010): 1,
(0b00, 0b011): None, # None == random bit
(0b00, 0b100): 0,
(0b00, 0b101): 0,
(0b00, 0b110): None,
(0b00, 0b111): None,
(0b01, 0b000): 1,
(0b01, 0b001): 1,
(0b01, 0b010): 1,
(0b01, 0b011): 1,
(0b01, 0b100): None,
(0b01, 0b101): 0,
(0b01, 0b110): 0,
(0b01, 0b111): 0,
(0b10, 0b000): 1,
(0b10, 0b001): 1,
(0b10, 0b010): 1,
(0b10, 0b011): None,
(0b10, 0b100): 0,
(0b10, 0b101): 0,
(0b10, 0b110): 0,
(0b10, 0b111): 0,
(0b11, 0b000): None,
(0b11, 0b001): 0,
(0b11, 0b010): 1,
(0b11, 0b011): None,
(0b11, 0b100): None,
(0b11, 0b101): 0,
(0b11, 0b110): 0,
(0b11, 0b111): 0,
}
def row_gen(last_rows):
# prepend and append random bits to last row
last_row_padded = left_random_bit()
last_row_padded <<= 8
last_row_padded |= last_rows[-1]
last_row_padded <<= 1
last_row_padded |= right_random_bit()
# last two bits generated in current row, initial value = 10
last_two = 0b10
new_row = 0
# iterate from 7...0, inclusive
for i in range(7, -1, -1):
three_above = (last_row_padded >> i) & 0b111
new_bit = MAGIC[last_two, three_above]
if new_bit is None:
new_bit = mid_random_bit()
new_row = (new_row << 1) | new_bit
last_two = ((last_two << 1) | new_bit) & 0b11
# hook for verification
generated(new_row)
# now do postprocessing
last_rows.append(new_row)
last_rows = last_rows[-11:]
# condition 1
history = [b & 0xf0 for b in last_rows]
if 0 not in history:
if sum([b & 0x80 for b in history]) == 0:
# print 'pp 1'
last_rows[-1] = 0
# condition 2
history = [b & 0xf for b in last_rows[-7:]]
if 0 not in history:
comparator = 0
if len(last_rows) >= 9:
comparator = last_rows[-9]
if sum([b & 1 for b in history]) == (comparator & 1) * 7:
# print 'pp 2'
last_rows[-1] &= 0xf0
pr_row(last_rows[-1])
return last_rows
def maze_gen():
last_rows = [0]
while True:
last_rows = row_gen(last_rows)
if __name__ == '__main__':
# random.seed(12345)
maze_gen()