forked from Pranav-Bhaskar/Zer0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prog.py
76 lines (70 loc) · 1.63 KB
/
prog.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
import os
import numpy
from network.net import Model
import socket
from pathlib import Path
def_dir = "./CheckPoint/"
spe = ['.move', 'black', 'white']
peices = {'Z' : 0, 'P' : 1, 'Q' : 2, 'R' : 3, 'B' : 4, 'K' : 5}
def pre_process(k):
k = k.split(',')
l = k[-1]
k = k[:-1]
for x in l:
k.append(peices[str(x)])
k = [int(x) for x in k]
return numpy.array(k)
def reader(m):
val = []
l = [f for f in os.listdir(def_dir) if f not in spe]
for direc in l:
with open(def_dir + direc, "r") as f:
for line in f:
line = line.strip()
processed = pre_process(line)
break
os.remove(def_dir + direc)
processed = numpy.reshape(processed, (1, 71))
predicted = m.predict(processed)
k = [str(processed[0][x]) for x in range(71)]
k = ','.join(k)
val.append((predicted, direc, k))
val.sort(key=lambda x: x[0][0])
for x in val[:-1]:
if x[2][0] is '1':
f = open(def_dir + "white/.wrong", "a+")
else:
f = open(def_dir + "black/.wrong", "a+")
f.write(str(x[2]))
f.write('\n')
f.close()
f = open(def_dir + ".move", "w")
f.write(str(val[-1][1]))
f.close()
if val[-1][2][0] is '1':
f = open(def_dir + "white/.right", "a+")
else:
f = open(def_dir + "black/.right", "a+")
f.write(str(val[-1][2]))
f.write('\n')
f.close()
def connector(c):
k = c.recv(1024).decode()
if k.strip() == '1':
return True
reader(m)
c.send('1'.encode())
return False
if __name__ == '__main__':
m = Model()
m.load()
s = socket.socket()
s.bind(('127.0.0.1', 8888))
s.listen(99)
open('./done.server', 'a').close()
lock_file = Path('./server.lock')
c, md = s.accept()
while lock_file.is_file():
if connector(c):
break
s.close()