-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrunkenBishop.py
189 lines (161 loc) · 5.71 KB
/
DrunkenBishop.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
import numpy as np
import hashlib
class Board:
START = None
END = None
XDIM = 17
YDIM = 9
symbols = [' ', '.', 'o', '+', '=', '*', 'B', '0', 'X', '@', '%', '&', '#', '/', '^']
START_SYMBOL = 'S'
END_SYMBOL = 'E'
field = None # initialised to (XDIM, YDIM) numpy array of zeros
i_string = None
i_bytes = None
def __init__(self, title:str=None, xdim:int=17, ydim:int=9):
self.XDIM = xdim
self.YDIM = ydim
self.START = (self.XDIM//2, self.YDIM//2)
self.title = title
self.clear_board()
def clear_board(self):
self.field = np.zeros((self.XDIM, self.YDIM), dtype=np.int)
def resize(self, xdim: int, ydim: int):
self.XDIM = xdim
self.YDIM = ydim
self.clear_board()
def __str__(self):
if self.title is None:
ostr = "+" + "-"*self.XDIM + "+\n"
else:
if len(self.title) > self.XDIM - 2:
tstr = "["+self.title[:self.XDIM - 5]+"...]"
else:
tstr = "["+self.title+"]"
ostr = "+" + tstr.center(self.XDIM, '-') + "+\n"
for y in range(self.YDIM):
ostr += "|"
for x in range(self.XDIM):
if x == self.START[0] and y == self.START[1]:
ostr += self.START_SYMBOL
elif x == self.END[0] and y == self.END[1]:
ostr += self.END_SYMBOL
else:
# Note modulo to wrap symbols around for very long walks
ostr += self.symbols[self.field[x, y] % len(self.symbols)]
ostr += "|\n"
ostr += "+" + "-"*(self.XDIM) + "+"
return ostr
def make_art(self, istr: str, do_md5:bool=True, is_hex:bool=False) -> str:
self.i_string = istr
if is_hex:
self.i_bytes = bytes.fromhex(self.i_string)
elif do_md5:
md5 = hashlib.md5()
md5.update(self.i_string.encode('utf-8'))
self.i_bytes = md5.digest()
else:
self.i_bytes = self.i_string.encode('utf-8')
bishop = list(self.START) # copy the start position
# Don't need to increment start as it will always be "S"
for byte in self.i_bytes:
# Extract the pairs of bits from the byte into an array.
# Least significant first
w = byte
pairs = []
for i in range(4):
b1 = int(w&1 != 0)
w = w >> 1
b2 = int(w&1 != 0)
w = w >> 1
pairs.append((b2, b1))
for p in pairs:
dy = p[0]*2 - 1
dx = p[1]*2 - 1
# Move the bishop, sliding along walls as necessary
bishop[0] = max(min(bishop[0] + dx, self.XDIM - 1), 0)
bishop[1] = max(min(bishop[1] + dy, self.YDIM - 1), 0)
# Drop a coin on the current square
self.field[bishop[0], bishop[1]] += 1
# We are done. Mark the end point
self.END = bishop
return str(self)
def test():
test_hash = "fc94b0c1e5b0987c5843997697ee9fb7"
print("test input:")
print(test_hash)
print("\nAs hex input:")
expected_hex = (
"+---[Test Hash]---+\n"
"| .=o. . |\n"
"| . *+*. o |\n"
"| =.*..o |\n"
"| o + .. |\n"
"| S o. |\n"
"| o . |\n"
"| . . . |\n"
"| o .|\n"
"| E.|\n"
"+-----------------+")
b = Board(title="Test Hash")
out_hex = b.make_art(test_hash, is_hex=True)
print_test_comparison(expected_hex, out_hex)
passed_hex = out_hex == expected_hex
if passed_hex:
print("TEST PASSED")
else:
print("TEST FAILED")
b.clear_board()
print("\nWith MD5 hash:")
expected_md5 = (
"+---[Test Hash]---+\n"
"| . . |\n"
"| . = . . |\n"
"| o o . o |\n"
"| . . . . |\n"
"| S + . |\n"
"| E. + |\n"
"| . .o |\n"
"| ..oo.o |\n"
"| +Bo=*.. |\n"
"+-----------------+")
out_md5 = b.make_art(test_hash, do_md5=True)
print_test_comparison(expected_md5, out_md5)
passed_md5 = out_md5 == expected_md5
if passed_md5:
print("TEST PASSED")
else:
print("TEST FAILED")
b.clear_board()
print("\nAs raw string:")
expected_raw = (
"+---[Test Hash]---+\n"
"| o#XBoo.o.o. |\n"
"| o.#%Boooo.E |\n"
"| + Xo=oo = o |\n"
"| . o o |\n"
"| S |\n"
"| + o |\n"
"| |\n"
"| |\n"
"| |\n"
"+-----------------+")
out_raw = b.make_art(test_hash, do_md5=False)
print_test_comparison(expected_raw, out_raw)
passed_raw = out_raw == expected_raw
if passed_raw:
print("TEST PASSED")
else:
print("TEST FAILED")
if passed_hex and passed_md5 and passed_raw:
print("\nALL TESTS PASSED! :)")
else:
print("\nSOME TESTS FAILED! :(")
def print_test_comparison(expected, got):
expected_parts = expected.split("\n")
got_parts = got.split("\n")
fstr = "{{:^{0}}} {{:^{0}}}".format(len(expected_parts[0]))
print(fstr.format("EXPECTED", "GOT"))
for i in range(len(expected_parts)):
print(fstr.format(expected_parts[i], got_parts[i]))
if __name__ == "__main__":
test()