def playstockfish(): board = chess.Board() engine = chess.engine.SimpleEngine.popen_uci("/Program Files/stockfish_20090216_x64") print (board) if color == "black": print ("Lightningwhite") while not board.is_game_over(claim_draw=True): mov = selectmove(x) make_move(mov) print (mov) result = engine.play(board, chess.engine.Limit(time=7)) board.push(result.move) if color == "white": print ("Lightningblack") result = engine.play(board, chess.engine.Limit(time=7)) board.push(result.move) mov = selectmove(x) make_move(mov) print (mov) while board.is_game_over(claim_draw=False): print (board) board = chess.Board() playstockfish()
def process_curent_game(session): board = chess.Board() engine = chess.engine.SimpleEngine.popen_uci("engine/stockfish") limit = chess.engine.Limit(time=2) local_opponents_last_move = None while True: try: cur_games = berserk.clients.Games(session).get_ongoing() except: continue if not cur_games: break else: cur_game = cur_games[0] if cur_game['isMyTurn']: remote_last_move = cur_game['lastMove'] if not remote_last_move: my_move = engine.play(board, limit).move.uci() board.push_uci(my_move) screen_utils.field.move(my_move, cur_game['color']) elif remote_last_move != local_opponents_last_move: local_opponents_last_move = remote_last_move board.push_uci(remote_last_move) my_move = engine.play(board, limit).move.uci() board.push_uci(my_move) screen_utils.field.move(my_move, cur_game['color']) engine.quit()
def __move_piece(self, engine, book, side): score_change = 0 moves_mate = math.inf promote_to_Q = False if len(self.moves_game) != 0: self.board.push_san(self.moves_game[-1]) if sum(1 for _ in book.find_all(self.board)) != 0: for entry in book.find_all(self.board): next_move = entry.move self.board.push(next_move) break else: if self.elapsed_time < 50: score_prior = engine.analyse(self.board, chess.engine.Limit(time=0.2)) engine_move = engine.play(self.board, chess.engine.Limit(time=0.1)) next_move = engine_move.move self.board.push(next_move) if len(str(next_move)) == 5 and str(next_move)[-1] == 'q': promote_to_Q = True if not promote_to_Q: score_after = engine.analyse(self.board, chess.engine.Limit(time=0.2)) if str(score_after["score"])[0] == '#': moves_mate = int(str(score_after["score"])[2:]) else: score_change = (int(str(score_after["score"])) - int(str(score_prior["score"]))) / 100 else: engine_move = engine.play(self.board, chess.engine.Limit(time=0.1)) next_move = engine_move.move self.board.push(next_move) start_click = str(next_move)[0:2] end_click = str(next_move)[2:4] if side == 'white': start_pos = self.pos_w[start_click] end_pos = self.pos_w[end_click] else: start_pos = self.pos_b[start_click] end_pos = self.pos_b[end_click] time.sleep( self.__wait_time(side, score_change, moves_mate, end_click, promote_to_Q)) pyautogui.click(start_pos[0], start_pos[1]) pyautogui.click(end_pos[0], end_pos[1]) if promote_to_Q: pyautogui.click(end_pos[0], end_pos[1])
def calculate_move(): r = requests.get(url=args.url + "/getfen?id=" + game_id) response = r.json()["fen"] board = chess.Board(response) while not board.is_game_over(): result = engine.play(board, chess.engine.Limit(time=0.3), ponder=True) return str(result.move)
def analyze_game(game, engine, time_per_move=1.0, verbose=False): """ Analyze a game using an engine and return the numerical evaluation of each position. :param game: game to analyze :type game: chess.pgn.Game :param engine: engine to use for analysis :type engine: chess.engine.SimpleEngine :param time_per_move: time to spend on each full move (2 plies), in seconds :type time_per_move: float :return: the evaluation for every position in the game's mainline, in pawns, from White's point of view :rtype: list of float """ limit = chess.engine.Limit(time=time_per_move / 2) scores = [] for ply, node in enumerate(itertools.chain([game], game.mainline())): board = node.board() result = engine.play(board, limit, info=chess.engine.INFO_SCORE) score = result.info['score'] besteval = score.white().score(mate_score=MAX_SCORE) / 100 if verbose: print(ply, besteval, (ply + 1) // 2, node.san() if ply else '') scores.append(besteval) return scores
def getEngineEval(): if token.validate_token(request.args.get('token', False)): __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) __filename__ = 'stockfish-prod' if os.environ.get( "Production", False) else 'stockfish-dev' # Assign game Position with chess.engine.SimpleEngine.popen_uci( os.path.join(__location__, __filename__)) as engine: try: fen_notation = request.args.get('fen_notation', "") board = chess.Board(fen_notation) except ValueError: return jsonify({ "fen_notation_error": "Please supply a valid fen notation", }) strength = request.args.get('strength', 1) time = min(1, 0.01 * 10**int(strength)) res = engine.play(board, chess.engine.Limit(time=time, depth=20)) info = engine.analyse(board, chess.engine.Limit(time=time, depth=20)) engine.close() return jsonify({ "best_move": str(res.move), "ponder": str(res.ponder), "evaluation": str(info["score"].white()), }) return jsonify({ "access_token_error": "There seems to be a problem with your access token", })
def rollout(curr_node): global depth global engine if(curr_node.state.is_game_over()): board = curr_node.state if(board.result()=='1-0'): #print("h1") return (1,curr_node) elif(board.result()=='0-1'): #print("h2") return (-1,curr_node) else: return (0.5,curr_node) depth+=1 all_moves = [curr_node.state.san(i) for i in list(curr_node.state.legal_moves)] result = engine.play(curr_node.state, chess.engine.Limit(time=0.001)) move = curr_node.state.san(result.move) tmp_state = chess.Board(curr_node.state.fen()) tmp_state.push_san(move) to_use = None for i in all_moves: tmp_state1 = chess.Board(curr_node.state.fen()) tmp_state1.push_san(i) child = node() child.state = tmp_state1 child.parent = curr_node curr_node.children.add(child) if(child.state==tmp_state): to_use = child break return rollout(to_use)
def chess_moves_publishing(boards): pub = rospy.Publisher('chess_next_moves', String, queue_size=10) rospy.init_node('chess_moves', anonymous=True) while not board.is_game_over(): while not rospy.is_shutdown(): result = engine.play(boards, chess.engine.Limit(time=0.100)) next_move = str(result.move) recipient_square = boards.piece_at(squares_integers[next_move[2:]]) boards.push(result.move) pieceCount = update_piece_count(boards) if (recipient_square is None): rospy.loginfo(next_move) pub.publish(next_move) else: rospy.loginfo(next_move + 's') pieceCount -= 1 pub.publish(next_move + 's') rospy.sleep(235) print(pieceCount) generate_human_move(pieceCount) rospy.sleep(5) engine.quit()
def move(game, board): original_game = game original_board = board # predict move board.set_fen(display_board(game)[1].fen()) if not chess.Color(chess.WHITE) or board.is_game_over(): return game, board result = engine.play(board, chess.engine.Limit(time=1)) board.push(result.move) print(result.move) print("from ", result.move.from_square) print("to ", result.move.to_square) from_square_x, from_square_y = translate(result.move.from_square) to_square_x, to_square_y = translate(result.move.to_square) print("translated:", "square-" + str(from_square_x) + str(from_square_y)) WebDriverWait(game, 20).until(expected_conditions.presence_of_element_located( (By.CLASS_NAME, "square-" + str(from_square_x) + str(from_square_y)))) game.find_elements_by_class_name("square-" + str(from_square_x) + str(from_square_y))[0].click() time.sleep(1.5) action = ActionChains(game) clickable = game.find_elements_by_class_name("coordinates")[0] x, y = get_element_by_board_cords(game, to_square_x, to_square_y) action.move_to_element_with_offset(clickable, x, y) action.click() action.perform() time.sleep(1.5) if board.fen() != display_board(game)[1].fen(): move(original_game, original_board) return game, board
def main(): train_data = [] for x in range(num_games): print("Starting game:" + str(x)) board = chess.Board() while not board.is_game_over() and board.fullmove_number < 20: legal_moves = board.legal_moves data = [] if board.turn == chess.BLACK: for move in legal_moves: board.push(move) # Estimate moves info = engine.analyse( board, chess.engine.Limit(time=0.0100, depth=6)) board.pop() data.append((move, str(info["score"]))) train_data.append(([to_X(board)], to_Y(data))) # Make best move result = engine.play(board, chess.engine.Limit(time=0.100)) board.push(result.move) # Make random move # board.push(random.choice(data)[0]) if (x + 1) % 96 == 0: train_data_formatted = split_data(train_data) model.fit([train_data_formatted[0]], [train_data_formatted[1]], epochs=1050, batch_size=32, callbacks=callbacks, verbose=2) train_data = [] model.save("checkpoints/" + model_name + datetime.now().strftime("D%d-H%H-M%M") + ".h5") model.save("models/" + model_name + ".h5") engine.quit()
def play_game(ws): board = generate_board(10) ws.send(json.dumps({"player": "random", "type": "player"})) engine = chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish") for state in board.states: analysis = engine.analyse(board, chess.engine.Limit(time=MOVE_GAP / 3)) ws.send( json.dumps({ "pieces": state, "score": analysis.score.white().score(), "type": "update" })) ws.send(json.dumps({"player": "nonrandom", "type": "player"})) while not board.is_game_over(claim_draw=True): result = engine.play(board, chess.engine.Limit(time=MOVE_GAP)) analysis = engine.analyse(board, chess.engine.Limit(time=MOVE_GAP)) print("score is:", analysis.score, "fen is", board.fen()) board.push(result.move) ws.send( json.dumps({ "pieces": board.get_pieces(), "score": analysis.score.white().score(), "type": "update" })) if board.is_checkmate(): ws.send(json.dumps({"winner": not board.turn(), "type": "winner"})) else: ws.send(json.dumps({"winner": None, "type": "winner"})) engine.quit()
def best_move(): fen = request.args.get('fen', '') if fen in cache: return cache[fen] board = chess.Board(fen) if board.is_game_over(): return {} engine = chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish") result = engine.play(board, chess.engine.Limit(time=1.00)) move = result.move san = board.san(move) board.push(move) # Commiting the move to the board # Analyze (NEW). Not activated. TODO #info = engine.analyse(board, chess.engine.Limit(time=0.5)) #score = info["score"] #mate_score = score.is_mate() #if score.is_mate(): # eval = f"{score.white().mate()}#" #else: # eval = f"{score.white().score()}" engine.quit() result = {"san": san, "fen": board.fen()} cache[fen] = result return result
def main(): board = chess.Board() engine = chess.engine.SimpleEngine.popen_uci("/usr/games/stockfish") #engine = chess.engine.SimpleEngine.popen_uci("~/.local/lib/python3.7/site-packages/stockfish") #create test matrix positions = create_colormap() positions[6][4] = 0 positions[4][4] = 1 print(board) print("\n") move = decode_move(board, positions) board.push(move) print(board) result = engine.play(board, chess.engine.Limit(time=0.5)) board.push(result.move) print("\n") print(board) engine.quit() print("\nTesting board novelty functions") print("first") print(previous_board(board, 2, 1)) print("initial") print(previous_board(board, 2, 0)) print("original") print(board)
def getNextMove(self, current_unit, current_layout): """ Run the chess engine with UCI communication to find the next move to draw for the user """ print('Next move for ' + current_unit) if ( not self.board.is_checkmate() ): #TODO add a you already won message and hooray ceremony if winning try: engine = chess.engine.SimpleEngine.popen_uci(get_engine_path()) # current_turn = self.board.epd().split(' ')[1] self.changeTurns(current_unit, current_layout) user_time_limit = int( SettingsManager().settings["Engine Time Limit"]) #TODO check if game finished , as it crashes the engine result = engine.play(self.board, chess.engine.Limit(time=user_time_limit)) print(result) self.board.push(result.move) self.last_move_image = show_move(str(result.move), self.out_board) self.window.updateBoard(self.board) engine.close() self.window.close() except OSError: print("Couldn't find your chess player") show_no_engine_msgbox()
def get(self): ret_dict = {} parser = reqparse.RequestParser() parser.add_argument('fen', type=str) parser.add_argument('svg', type=str) args = parser.parse_args() engine = chess.engine.SimpleEngine.popen_uci(stockfish_path) board = chess.Board(fen=args['fen']) info = engine.analyse(board, chess.engine.Limit(time=0.100)) next_move_limit = chess.engine.Limit(time=2.0) next_move = engine.play(board, next_move_limit) engine.quit() ret_dict = { 'current_score': str(info["score"]), 'next_move_suggestion': str(next_move.move), 'next_move_suggestions': str(next_move.info), 'board_before': str(board), } if ('svg' in args and args['svg'] == '1'): ret_dict['svg'] = chess.svg.board(board=board, lastmove=next_move.move) return ret_dict
def main(): ser = serial.Serial('/dev/ttyACM0', 9600) engine = chess.engine.SimpleEngine.popen_uci("/usr/games/stockfish") board = chess.Board() print("----- GAME START -----\n") while not board.is_game_over(): result = engine.play(board, chess.engine.Limit(time=0.500)) print(result.move) arduino_move = result.move.uci() print(type(arduino_move.encode())) ser.write(arduino_move.encode()) board.push(result.move) print("AI Move\n") print(board) print(" ") user_choice = input("User Move: ") board = check_input(user_choice, board) engine.quit()
def test_uci(python='python3', protocol='uci'): def new_engine(): #python = '/usr/local/bin/python3' d = pathlib.Path(__file__).parent if protocol == 'uci': args = [python, '-u', str(d / 'uci.py')] return chess.engine.SimpleEngine.popen_uci(args, debug=True) else: args = [python, '-u', str(d / 'xboard.py')] return chess.engine.SimpleEngine.popen_xboard(args, debug=True) limits = [ chess.engine.Limit(time=1), chess.engine.Limit(nodes=1000), chess.engine.Limit(white_clock=1, black_clock=1), chess.engine.Limit( white_clock=1, black_clock=1, white_inc=1, black_inc=1, remaining_moves=2) ] for limit in limits: engine = new_engine() board = chess.Board() while not board.is_game_over() and len(board.move_stack) < 3: result = engine.play(board, limit) board.push(result.move) engine.quit()
def help(): result = str(engine.play(chessboard, chess.engine.Limit(time=(0.5))).move) result = f[result[0]], int(result[1]) - 1, f[result[2]], int(result[3]) - 1 print(result) x1, y1, x2, y2 = result[0], result[1], result[2], result[3] pygame.draw.rect(screen, (0, 204, 255), (x1 * 100, y1 * 100, 100, 100)) pygame.draw.rect(screen, (0, 204, 255), (x2 * 100, y2 * 100, 100, 100))
def solve(fen): moves = [] board = chess.Board(fen) while not board.is_game_over(): result = engine.play(board, chess.engine.Limit(time=1.0)) moves.append(str(result.move)[0:2] + "-" + str(result.move)[2:]) board.push(result.move) return str.join(";", moves)
def helper(): global helping result = str(engine.play(chessboard, chess.engine.Limit(time=0.5)).move) result = f[result[0]], int(result[1]) - 1, f[result[2]], int(result[3]) - 1 x1, y1, x2, y2 = result[0], result[1], result[2], result[3] board_color[x1][7 - y1] = (110, 52, 235) board_color[x2][7 - y2] = (110, 52, 235) helping = 1
async def ai_move(ctx, board, state): if state['player_color'] != board.turn and not board.is_game_over(): await print_msg(ctx, 'AI is making a move..') result = engine.play(board, chess.engine.Limit(time=0.1)).move result_str = str(board.san(result)) await print_msg(ctx, 'AI moved ' + result_str) board.push(result) await print_board(ctx, board, state)
def get_best_move(engine, board, move): # make move on board board.push_san(move) # get best move from engine best_move = engine.play(board, chess.engine.Limit(depth=13)) # get move from engine move = best_move.move return move
def get_move(engine, pgn, depth): with open(pgn, "r") as f: game = chess.pgn.read_game(f) board = chess.Board() for move in game.mainline_moves(): board.push(move) best_move = engine.play(board, chess.engine.Limit(depth=depth)).move return (best_move)
async def play_handler(engine, board): try: # 100ms max result = await asyncio.wait_for(engine.play(board, chess.engine.Limit(time=0.01)), 0.1) return result except Exception: traceback.print_exc() return None
def cari_terbaik(engine, notasi, depth): with open(notasi, "r") as f: game = chess.pgn.read_game(f) papan = chess.Board() for pindah in game.mainline_moves(): papan.push(pindah) terbaik = engine.play(papan, chess.engine.Limit(depth=depth)).move return terbaik
def get_move(depth, fen): global engine print(depth) print("Calculating...") board = chess.Board(fen) result = engine.play(board, chess.engine.Limit(depth=depth)) print("Move found!", result.move) print() return str(result.move)
def compute_move_scores(game_str, engine): # we are black, crucially... moves = game_str.split(' ') board = chess.Board() bot_scores = [] comp_scores = [] for i, move in enumerate(moves): if i % 2 == 0: # this is a computer move, do not analyse it board.push_san(move) else: # this is our move, we need to provide analysis board.push_san(move) bot_score_tup = engine.analyse(board, chess.engine.Limit(time=0.1), game='key1') board.pop() computer_move = engine.play(board, chess.engine.Limit(time=0.05)) board.push(computer_move.move) comp_score_tup = engine.analyse(board, chess.engine.Limit(time=0.1), game='key2') board.pop() board.push_san(move) if not bot_score_tup['score'].is_mate() and not comp_score_tup['score'].is_mate(): bot_scores.append(bot_score_tup['score'].black().score()) comp_scores.append(comp_score_tup['score'].black().score()) bot_scores = np.array(bot_scores) comp_scores = np.array(comp_scores) # split the games up into early, mid and late-game early_game_cutoff = 12 mid_game_cutoff = 24 bot_early, comp_early = bot_scores[:early_game_cutoff], comp_scores[:early_game_cutoff] bot_mid, comp_mid = bot_scores[early_game_cutoff:mid_game_cutoff], comp_scores[early_game_cutoff:mid_game_cutoff] bot_late, comp_late = bot_scores[mid_game_cutoff:], comp_scores[mid_game_cutoff:] assert (len(bot_early) + len(bot_mid) + len(bot_late)) == len(bot_scores) assert (len(comp_early) + len(comp_mid) + len(comp_late)) == len(comp_scores) # compute MPS across full-game and across regions full_diff = bot_scores - comp_scores early_diff = bot_early - comp_early mid_diff = bot_mid - comp_mid late_diff = bot_late - comp_late norm_full = np.mean(np.abs(comp_scores)) norm_early = np.mean(np.abs(comp_early)) norm_mid = np.mean(np.abs(comp_mid)) norm_late = np.mean(np.abs(comp_late)) MPS_full = np.mean(full_diff / norm_full) MPS_early = np.mean(early_diff / norm_early) MPS_mid = np.mean(mid_diff / norm_mid) MPS_late = np.mean(late_diff / norm_late) return (bot_scores, comp_scores, MPS_full, MPS_early, MPS_mid, MPS_late)
def playStockFish(board, engine): result = engine.play(board, chess.engine.Limit(time=0.100)) msg = getDataPackage(result.move.uci(), board) board.push(result.move) info = engine.analyse(board, chess.engine.Limit(time=0.100)) return board, result.move.uci(), info, msg
def compare_model(time, stockfish=True): if stockfish: engine = chess.engine.SimpleEngine.popen_uci( "/home/albb762/chessai/stockfish/Linux/stockfish_10_x64") engine.configure({'Skill Level': 1}) first = 0 second = 0 draw = 0 for i in range(time): print(i) flag = 'stockfish' board = chess.Board() if random.randint(0, 1): board, _ = v.play_one_step(board) battle = 'model vs stockfish:' flag = 'model' while True: if not board.is_game_over(): if stockfish: result = engine.play( board, chess.engine.Limit(time=0.00100, depth=1)) board.push(result.move) else: board.push(random.choice(list(board.legal_moves))) else: break if not board.is_game_over(): board, _ = v.play_one_step(board) else: break res = board.result() if flag == 'stockfish': if '1-0' == res: first += 1 if '0-1' == res: second += 1 if '1/2-1/2' == res: draw += 1 if flag == 'model': if '1-0' == res: second += 1 if '0-1' == res: first += 1 if '1/2-1/2' == res: draw += 1 if stockfish: print('stockfish vs model : ', first, second, draw) else: print('random vs model : ', first, second, draw) engine.quit()
def make_move(psg_board, window, move_str, line, engine): if move_str == '0-0': move_str = castling_short(move_str, psg_board, window) elif move_str == '0-0-0': move_str = castling_long(move_str, psg_board, window) # en passante elif 'ep' in move_str: if board.turn: move_str = move_str[0:4] enemy_pawn = move_str[2] + str(int(move_str[3]) - 1) make_move_gui(move_str[0:2] + enemy_pawn, psg_board, window) make_move_gui(enemy_pawn + move_str[2:4], psg_board, window) else: move_str = move_str[0:4] enemy_pawn = move_str[2] + str(int(move_str[3]) + 1) make_move_gui(move_str[0:2] + enemy_pawn, psg_board, window) make_move_gui(enemy_pawn + move_str[2:4], psg_board, window) elif '=' in move_str: move_str = promotion(move_str, psg_board, window) else: make_move_gui(move_str, psg_board, window) move = chess.Move.from_uci(move_str) if not (board.is_legal(move) != board.is_castling(move)): if board.turn: sg.Popup(f'ERROR: {line} is not a legal move from white.\n') else: sg.Popup(f'ERROR: {line} is not a legal move from black.\n') engine.quit() sys.exit() board.push(move) analyzed_score = engine.analyse(board, chess.engine.Limit(time=0.2)) score = analyzed_score.get('score') depth = analyzed_score.get('depth') window.FindElement('_score_').Update( f'Score of your board is {score} with a analyzed depth of {depth}.', append=True) window.FindElement('_movelist_').Update(line + '\n', append=True) best_move = engine.play(board, chess.engine.Limit(time=0.2)).move window.FindElement('_best_move_').Update( f'{best_move} with a analyzed depth of {depth}.', append=True) if (board.is_checkmate()): sg.Popup('Checkmate!', board.result(), 'Thank you for playing', title='Game Over!') engine.quit() sys.exit() if (board.is_check()): sg.Popup('Check')