class ChessEngine: def __init__(self, enum): self.enum = enum self.ename = engines[enum] self.p = Popen(self.ename, stdin = PIPE, stdout = PIPE, shell = True) if not self.p: exit() self.fd = self.p.stdout.fileno() self.fh = msvcrt.get_osfhandle(self.fd) eparts = self.ename.split('\\') self.engtitle = eparts[len(eparts)-1] n = self.engtitle.rfind('.') self.engtitle = self.engtitle[:n] def quit(self): if self.p: self.p.stdin.write('quit\n') self.p.stdout.close() self.p.stdin.close() self.p = None def readln(self, timeout): t = 0.0 while t < timeout: _, aval, _ = PeekNamedPipe(self.fh, 0) if aval > 0: return self.p._translate_newlines(self.p.stdout.readline()) time.sleep(0.01) t = t + 0.01 return None def start(self): if self.p: self.p.stdin.write('uci\n') while True: line = self.readln(0.05) if not line: break; self.p.stdin.write('ucinewgame\n') def go(self, dt, n): if dt < 1.0: dt = 1.0 dtms = int(dt*1000) sendstr = 'go wtime %d btime %d movestogo %d\n' % (dtms, dtms, n) self.p.stdin.write(sendstr) t = 0.0 depth = 0 score = 0 mate = False while t < 2*dt: line = self.readln(0.1) if line: tokens = line.split() print tokens if len(tokens) > 1 and tokens[0] == 'bestmove': return tokens[1], score, depth, mate elif len(tokens) > 7: if tokens[5] == 'score': score = int(tokens[7]) if tokens[6] == 'mate': mate = True if tokens[1] == 'depth': d = int(tokens[2]) if d > depth: depth = d t = t + 0.1 time.sleep(0.1) return None, score, depth, mate def pos(self): strpos = 'position startpos' if len(moves) > 0: strpos = strpos + ' moves ' + ' '.join(moves) strpos = strpos + '\n' self.p.stdin.write(strpos)