-
Notifications
You must be signed in to change notification settings - Fork 2
/
h4x.py
executable file
·217 lines (199 loc) · 5.01 KB
/
h4x.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
#!/usr/bin/env python3
import socket
import sys
# Run with: h4x.py <host> <port>
if len(sys.argv) > 1:
host = sys.argv[1]
else:
host = '127.0.0.1'
if len(sys.argv) > 2:
port = int(sys.argv[2])
else:
port = 1234
s = socket.create_connection((host, port))
f = s.makefile()
x = y = 0
n = True
board = [[None for _ in range(8)] for _ in range(8)]
while True:
while True:
l = f.readline()
if l.startswith('THERE'):
if 'NOTHING INTERESTING' in l:
board[y][x] = 0
elif 'TAVERN' in l:
board[y][x] = 1
elif 'REDFORD' in l:
board[y][x] = 2
elif 'RED BULL' in l:
board[y][x] = 3
elif 'YELLOW DRAGON' in l:
board[y][x] = 4
elif 'BLUE DRAGON' in l:
board[y][x] = 5
elif 'CRYSTAL DRAGON' in l:
board[y][x] = 6
elif 'GREEN DRAGON' in l:
board[y][x] = 7
elif 'BEIGE DRAGON' in l:
board[y][x] = 8
elif 'BLACK DRAGON' in l:
board[y][x] = 9
elif 'GRAY DRAGON' in l:
board[y][x] = 10
elif 'ORANGE DRAGON' in l:
board[y][x] = 11
elif 'RED DRAGON' in l:
board[y][x] = 12
elif 'WHITE DRAGON' in l:
board[y][x] = 13
elif 'DRUNK DRAGON' in l:
board[y][x] = 14
else:
print(x, y, l)
break
if n:
if y == 7:
if x == 7:
break
s.sendall(b'e')
x += 1
n = False
else:
s.sendall(b'n')
y += 1
else:
if y == 0:
if x == 7:
break
s.sendall(b'e')
x += 1
n = True
else:
s.sendall(b's')
y -= 1
print(board)
class Rng:
def __init__(self, seed):
self.state = seed
def get(self):
res = self.state & 0x1f
for _ in range(5):
p = 0
for i in range(15):
if (self.state & (0x1d << 10 | 0xc << 5 | 0x11)) & 1 << i:
p ^= 1
self.state >>= 1
self.state |= p << 14
return res
def peek(self):
return self.state & 0x1f
INITTAB = [
2,
3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
]
for seed in range(1, 0x8000):
rng = Rng(seed)
xboard = [[0 for _ in range(8)] for _ in range(8)]
xboard[0][0] = 1
for thing in INITTAB:
while True:
b = rng.get()
a = rng.get()
a |= b << 5
nx = a & 7
ny = a >> 3 & 7
if xboard[ny][nx] == 0:
xboard[ny][nx] = thing
break
if xboard == board:
RNG = rng
print('SEED {:04x}'.format(seed))
n = not n
while True:
cur = board[y][x]
if cur in [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]:
print('fight')
s.sendall(b'f')
for l in f:
assert l
print(l.strip())
if 'HEAT' in l:
break
done = False
while True:
save = RNG.state
RNG.get()
r_m = RNG.peek()
if r_m & 0x10:
print('attack')
s.sendall(b'a')
for l in f:
assert l
print(l.strip())
if 'HEAT' in l:
RNG.get()
break
if 'YOU KILL' in l:
print('kill')
done = True
break
if done:
break
else:
print('shield')
s.sendall(b's')
for l in f:
assert l
print(l.strip())
if 'HEAT' in l:
break
while True:
l = f.readline()
assert l
print(l.strip())
if l.startswith('THERE'):
break
if n:
if y == 7:
if x == 0:
break
s.sendall(b'w')
x -= 1
n = False
else:
s.sendall(b'n')
y += 1
else:
if y == 0:
if x == 0:
break
s.sendall(b'w')
x -= 1
n = True
else:
s.sendall(b's')
y -= 1
while True:
l = f.readline()
assert l
print(l.strip())
if l.startswith('THERE'):
break
print('done?')
s.sendall(b'i')
while True:
l = f.readline()
assert l
print(l.strip())
if l.startswith('THERE'):
break
for _ in range(16):
s.sendall(b't')
while True:
l = f.readline()
assert l
print(l.strip())
if l.startswith('THERE'):
break