-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy
More file actions
executable file
·74 lines (57 loc) · 1.74 KB
/
strategy
File metadata and controls
executable file
·74 lines (57 loc) · 1.74 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
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python3
################################################################################
import sys
import socket
SCREEN_ROWS = 20
SCREEN_COLS = 56
ROBOTS_KEY_N = b'k'
ROBOTS_KEY_NE = b'u'
ROBOTS_KEY_E = b'l'
ROBOTS_KEY_SE = b'n'
ROBOTS_KEY_S = b'j'
ROBOTS_KEY_SW = b'b'
ROBOTS_KEY_W = b'h'
ROBOTS_KEY_NW = b'y'
ROBOTS_KEY_X = b' '
ROBOTS_EMPTY = ord(' ')
ROBOTS_ROBOT = ord('+')
ROBOTS_JUNK = ord('*')
ROBOTS_PLAYER = ord('O')
ROBOTS_DEAD = ord('X')
################################################################################
def player(screen):
for row in range(SCREEN_ROWS):
for col in range(SCREEN_COLS):
if screen[row][col] == ROBOTS_PLAYER:
return (row, col);
return None
def strategy(screen):
for line in screen:
print(line.decode('latin9') + '<')
p = player(screen)
if p is not None:
(row, col) = p
print("@ %d/%d" % (row, col))
return ROBOTS_KEY_SE
################################################################################
LENGTH = (SCREEN_ROWS + 2) * (SCREEN_COLS + 3) + 6;
PORT = 8080
if len(sys.argv) != 2:
raise Exception('hostname missing')
host = sys.argv[1]
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, PORT))
while (True):
data = b''
while len(data) < LENGTH:
data += s.recv(LENGTH - len(data))
if len(data) == 0:
print('score: ' + str(score))
exit(0)
screen = [ ]
for row in range(SCREEN_ROWS):
screen.append(data[(row + 1) * (SCREEN_COLS + 3) + 1:(row + 2) * (SCREEN_COLS + 3) - 2])
score = int(data[(SCREEN_ROWS + 2) * (SCREEN_COLS + 3):-1].decode('latin9'))
key = strategy(screen);
s.sendall(key)
################################################################################