def get_ucci_move(self, fen, time=3): p = subprocess.Popen(self.config.resource.eleeye_path, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) setfen = f'position fen {fen}\n' setrandom = 'setoption randomness small\n' cmd = 'ucci\n' + setrandom + setfen + f'go time {time * 1000}\n' try: out, err = p.communicate(cmd, timeout=time+0.5) except: p.kill() try: out, err = p.communicate() except Exception as e: logger.error(f"{e}, cmd = {cmd}") return self.get_ucci_move(fen, time+1) print(out) lines = out.split('\n') if lines[-2] == 'nobestmove': return None move = lines[-2].split(' ')[1] if move == 'depth': move = lines[-1].split(' ')[6] return senv.parse_ucci_move(move)
def get_ucci_move(self, fen, time=3): p = subprocess.Popen(self.config.resource.eleeye_path, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) setfen = f'position fen {fen}\n' setrandom = 'setoption randomness small\n' cmd = 'ucci\n' + setrandom + setfen + f'go time {time * 1000}\n' try: out, err = p.communicate(cmd, timeout=time + 0.5) except: p.kill() try: out, err = p.communicate() except Exception as e: logger.error(f"{e}, cmd = {cmd}") return self.get_ucci_move(fen, time + 1) print(out) lines = out.split('\n') if lines[-2] == 'nobestmove': return None move = lines[-2].split(' ')[1] if move == 'depth': move = lines[-1].split(' ')[6] return senv.parse_ucci_move(move)
def cmd_position(self): ''' position {fen <fenstring> | startpos } [moves <move1> .... <moven>] ''' if not self.is_ready: return move_idx = -1 if len(self.args) > 0: if self.args[0] == 'fen': # init with fen string fen = self.args[1] try: self.state = senv.fen_to_state(fen) except Exception as e: logger.error(f"cmd position error! cmd = {self.args}, {e}") return self.history = [self.state] turn = self.args[2] if turn == 'b': self.state = senv.fliped_state(self.state) self.is_red_turn = False self.turns = (int(self.args[6]) - 1) * 2 + 1 else: self.is_red_turn = True self.turns = (int(self.args[6]) - 1) * 2 if len(self.args) > 7 and self.args[7] == 'moves': move_idx = 8 elif self.args[0] == 'startpos': self.state = senv.INIT_STATE self.is_red_turn = True self.history = [self.state] self.turns = 0 if len(self.args) > 1 and self.args[1] == 'moves': move_idx = 2 elif self.args[0] == 'moves': move_idx = 1 else: self.state = senv.INIT_STATE self.is_red_turn = True self.history = [self.state] self.turns = 0 logger.debug(f"state = {self.state}") # senv.render(self.state) # execute moves if move_idx != -1: for i in range(move_idx, len(self.args)): action = senv.parse_ucci_move(self.args[i]) if not self.is_red_turn: action = flip_move(action) self.history.append(action) self.state = senv.step(self.state, action) self.is_red_turn = not self.is_red_turn self.turns += 1 self.history.append(self.state) logger.debug(f"state = {self.state}")
def test_ucci(): import cchess_alphazero.environment.static_env as senv from cchess_alphazero.environment.lookup_tables import flip_move state = senv.INIT_STATE state = senv.step(state, '0001') fen = senv.state_to_fen(state, 1) print(fen) senv.render(state) move = 'b7b0' move = senv.parse_ucci_move(move) print(f'Parsed move {move}') move = flip_move(move) print(f'fliped move {move}') state = senv.step(state, move) senv.render(state) fen = senv.state_to_fen(state, 2) print(fen)