def KQvKP(king_position: int): _BLACK_KING_POSITION = chess.H1 _BLACK_PAWN_POSITION = chess.H2 board = chess.Board() board.clear() board.set_piece_at(_BLACK_KING_POSITION, Piece(chess.KING, chess.BLACK)) board.set_piece_at(_BLACK_PAWN_POSITION, Piece(chess.PAWN, chess.BLACK)) board.set_piece_at(king_position, Piece(chess.KING, chess.WHITE)) possible_boards = [] for square in chess.SQUARES: if square in [_BLACK_KING_POSITION, _BLACK_PAWN_POSITION, king_position]: continue possible_board = board.copy() possible_board.set_piece_at(square, Piece(chess.QUEEN, chess.WHITE)) if not possible_board.is_attacked_by(chess.WHITE, _BLACK_KING_POSITION) and possible_board.is_valid(): possible_boards.append(possible_board) filtered_boards = list(filter(lambda b: Tablebase.check(b) == 2, possible_boards)) return len(possible_boards) == len(filtered_boards)
def exposed_king(puzzle: Puzzle) -> bool: if puzzle.pov: pov = puzzle.pov board = puzzle.mainline[0].board() else: pov = not puzzle.pov board = puzzle.mainline[0].board().mirror() king = board.king(not pov) assert king is not None if chess.square_rank(king) < 5: return False squares = SquareSet.from_square(king - 8) if chess.square_file(king) > 0: squares.add(king - 1) squares.add(king - 9) if chess.square_file(king) < 7: squares.add(king + 1) squares.add(king - 7) for square in squares: if board.piece_at(square) == Piece(PAWN, not pov): return False for node in puzzle.mainline[1::2][1:-1]: if node.board().is_check(): return True return False
def from_dict(occupied_squares): ''' Build a BlindBoard from a dictionary with the structure {square: color} ''' board = BlindBoard() for square, color in occupied_squares.items(): board.set_piece_at(square, Piece(PAWN, color)) return board
def set_piece_at(self, square, piece): ''' In `BaseBoard`, this method expects a square and a Piece object. But for BlindBoards, we only need the second argument to be a color (we don't need the piece type). ''' if piece in chess.COLORS: piece = Piece(PAWN, piece) elif not isinstance(piece, Piece): raise ValueError("`%s` is neither a `bool` nor a Piece object" % str(piece)) return BaseBoard.set_piece_at(self, square, piece)
def human_move(src, dest): piece = BOARD.remove_piece_at(src) if dest > 55: BOARD.set_piece_at(dest, Piece(QUEEN, Color(WHITE))) else: BOARD.set_piece_at(dest, piece) jump = play_move(state_board, [src, dest], Turn.WHITE) if jump is not None: BOARD.remove_piece_at(jump) print_board(state_board)
def computer_move(move): jump = play_move(state_board, move, Turn.BLACK) piece = BOARD.remove_piece_at(move[0]) if move[1] < 8: BOARD.set_piece_at(move[1], Piece(QUEEN, BLACK)) else: BOARD.set_piece_at(move[1], piece) if jump is not None: BOARD.remove_piece_at(jump) print_board(state_board)
def board_to_blindboard(board): ''' Takes as input a `Board` object and makes it 'blind' by turning all pieces into pawns. ''' assert isinstance(board, Board) blindboard = BlindBoard() for color in (BLACK, WHITE): # occupied_pieces is a set of integers occupied_squares = BlindBoard.Diff.get_squares_from_mask(board.occupied_co[color]) for square in occupied_squares: blindboard.set_piece_at(square, Piece(PAWN, color)) return blindboard
def testPeiceClassMethods(self): # test is_valid_xy_position p = Piece() # Positive Test self.assertTrue(p.is_valid_xy_position([1, 1])) self.assertTrue(p.is_valid_xy_position([8, 8])) # Negative Test self.assertFalse(p.is_valid_xy_position([0, 0])) self.assertFalse(p.is_valid_xy_position([9, 9])) # position_to_xy self.assertEqual(p.position_to_xy("A1"), [1, 1]) # xy_to_position(self, xy) self.assertEqual(p.xy_to_position([1, 1]), "A1")
def test_imgage_processor(): '''Test image processor (multiple tests)''' expected_board = BlindBoard.get_starting_board() expected_board.remove_piece_at(chess.E2) expected_board.set_piece_at(chess.E4, Piece(PAWN, WHITE)) # retrieve all the images paths and sort images = collect_test_images('tests/pictures/game000') debug("Calibrating image processor...") processor = ImageProcessor(images[0], images[1]) for img, expected_board in zip(images[2:], expected_boards()): debug("Processing `{}`...".format(os.path.basename(img))) processor.process(img) board = processor.get_blindboard() yield compare_blindboards, expected_board, board, img
from chess import PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, WHITE, BLACK, Piece W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING = Piece( PAWN, WHITE), Piece(KNIGHT, WHITE), Piece(BISHOP, WHITE), Piece( ROOK, WHITE), Piece(QUEEN, WHITE), Piece(KING, WHITE) B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING = Piece( PAWN, BLACK), Piece(KNIGHT, BLACK), Piece(BISHOP, BLACK), Piece( ROOK, BLACK), Piece(QUEEN, BLACK), Piece(KING, BLACK) semantical_piece_order = [ W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING, B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING ] # Piece Index map is how pieces are defined in Scam, # see https://github.com/fabianvdW/Scam/blob/81f8f85bc4f52655b852f87be43546c7dfea6c8c/src/types.rs#L72-L84 PIECE_INDEX_MAP = { W_PAWN: 1, W_KNIGHT: 2, W_BISHOP: 3, W_ROOK: 4, W_QUEEN: 5, W_KING: 6, B_PAWN: 9, B_KNIGHT: 10, B_BISHOP: 11, B_ROOK: 12, B_QUEEN: 13, B_KING: 14 } PIECE_MAX_INDEX = 15