def test_mate_in_two_puzzles(self): # Siegbert Tarrasch vs. Max Kurschner # 1. Qg6+ hxg6 2. Bxg6# board = chess.Board( 'r2qk2r/pb4pp/1n2Pb2/2B2Q2/p1p5/2P5/2B2PPP/RN2R1K1 w - - 1 0') move = next_move(3, board) self.assertEqual(move.uci(), 'f5g6')
def command(depth: int, board: chess.Board, msg: str): """ Accept UCI commands and respond. The board state is also updated. """ msg = msg.strip() tokens = msg.split(" ") while "" in tokens: tokens.remove("") if msg == "quit": sys.exit() if msg == "uci": print("id name Andoma") # Andrew/Roma -> And/oma print("id author Andrew Healey & Roma Parramore") print("uciok") return if msg == "isready": print("readyok") return if msg == "ucinewgame": return if msg.startswith("position"): if len(tokens) < 2: return # Set starting position if tokens[1] == "startpos": board.reset() moves_start = 2 elif tokens[1] == "fen": fen = " ".join(tokens[2:8]) board.set_fen(fen) moves_start = 8 else: return # Apply moves if len(tokens) <= moves_start or tokens[moves_start] != "moves": return for move in tokens[(moves_start + 1):]: board.push_uci(move) if msg == "d": # Non-standard command, but supported by Stockfish and helps debugging print(board) print(board.fen()) if msg[0:2] == "go": _move = next_move(depth, board) print(f"bestmove {_move}") return
def start(): """ Start the command line user interface. """ board = chess.Board() user_side = (chess.WHITE if input("Start as [w]hite or [b]lack:\n") == "w" else chess.BLACK) if user_side == chess.WHITE: print(render(board)) board.push(get_move(board)) while not board.is_game_over(): board.push(next_move(get_depth(), board, debug=False)) print(render(board)) board.push(get_move(board)) print(f"\nResult: [w] {board.result()} [b]")
def start(): ''' Start the command line user interface. ''' board = chess.Board() user_side = chess.WHITE if input( 'Start as [w]hite or [b]lack:\n') == 'w' else chess.BLACK if user_side == chess.WHITE: print(render(board)) board.push(get_move(board)) while not board.is_game_over(): board.push(next_move(get_depth(), board, debug=False)) print(render(board)) board.push(get_move(board)) print(f'\nResult: [w] {board.result()} [b]')
def command(depth, board, msg): ''' Accept UCI commands and respond. The board state is also updated. ''' if msg == 'quit': sys.exit() if msg == 'uci': print("id name schach-KI") # Name of chess engine print("id author Andreas Waider") # Name of creators print("uciok") return if msg == 'isready': print('readyok') return if msg == 'ucinewgame': return if 'position startpos moves' in msg: moves = msg.split(' ')[3:] board.clear() board.set_fen(chess.STARTING_FEN) for move in moves: board.push(chess.Move.from_uci(move)) return if 'position fen' in msg: fen = ' '.join(msg.split(' ')[2:]) board.set_fen(fen) return if msg[0:2] == 'go': move = next_move(board) print(f'bestmove {move}') return
def command(depth: int, board: chess.Board, msg: str): """ Accept UCI commands and respond. The board state is also updated. """ if msg == "quit": sys.exit() if msg == "uci": print("id name Andoma") # Andrew/Roma -> And/oma print("id author Andrew Healey & Roma Parramore") print("uciok") return if msg == "isready": print("readyok") return if msg == "ucinewgame": return if "position startpos moves" in msg: moves = msg.split(" ")[3:] board.clear() board.set_fen(chess.STARTING_FEN) for move in moves: board.push(chess.Move.from_uci(move)) return if "position fen" in msg: fen = " ".join(msg.split(" ")[2:]) board.set_fen(fen) return if msg[0:2] == "go": _move = next_move(depth, board) print(f"bestmove {_move}") return
def command(depth: int, board: chess.Board, msg: str): ''' Accept UCI commands and respond. The board state is also updated. ''' if msg == 'quit': sys.exit() if msg == 'uci': print("id name Andoma") # Andrew/Roma -> And/oma print("id author Andrew Healey & Roma Parramore") print("uciok") return if msg == 'isready': print('readyok') return if msg == 'ucinewgame': return if 'position startpos moves' in msg: moves = msg.split(' ')[3:] board.clear() board.set_fen(chess.STARTING_FEN) for move in moves: board.push(chess.Move.from_uci(move)) return if 'position fen' in msg: fen = ' '.join(msg.split(' ')[2:]) board.set_fen(fen) return if msg[0:2] == 'go': _move = next_move(depth, board) print(f'bestmove {_move}') return
def test_mate_in_one(self): # Multiple mate in 2s/3s, but only one mate in 1 board = chess.Board("6k1/8/8/5r2/8/8/4r3/2K5 b - - 1 1") move = next_move(3, board) self.assertEqual(move.uci(), "f5f1")