-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
64 lines (52 loc) · 1.73 KB
/
engine.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
import subprocess
import json
import tpn
class Engine:
def __init__(self, path):
self.process = subprocess.Popen([path],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True)
def send_message(self, message):
serialized_message = json.dumps(message)
self.process.stdin.write(serialized_message + '\n')
self.process.stdin.flush()
def receive_message(self):
while True:
raw_message = self.process.stdout.readline()
try:
return json.loads(raw_message)
except:
assert False, "Unknown message:" + raw_message
def load(self, input_nodes, output_nodes, node_evals):
msg = {
"type": "Load",
"input_nodes": input_nodes,
"output_nodes": output_nodes,
"node_evals": node_evals
}
self.send_message(msg)
def play_game(self):
msg = { "type": "PlayGame" }
self.send_message(msg)
response = self.receive_message()
return response["score"]
def pos(self, tpn):
msg = { "type": "Pos", "tpn": tpn }
self.send_message(msg)
def go(self):
msg = { "type": "Go" }
self.send_message(msg)
return self.receive_message()
def peek(self):
msg = { "type": "Peek" }
self.send_message(msg)
out = self.receive_message()
print(out)
return tpn.loads(out["tpn"])
def ready(self):
msg = { "type": "Ready" }
self.send_message(msg)
return self.receive_message()
def terminate(self):
self.process.terminate()