Example #1
0
    def test_is_stalemated(self, active_pieces_mock, valid_move_mock):
        """Tests for the player_is_stalemated method."""
        white_config = {'color': ChessColor.WHITE, 'name': 'Griffin'}
        white_player = Player(white_config)

        white_king = King(ChessColor.WHITE)
        white_mapping = [(white_king, self.board.squares[1][1]),
                         (Piece(ChessColor.WHITE), self.board.squares[1][2]),
                         (Piece(ChessColor.WHITE), self.board.squares[1][3])]
        black_king = King(ChessColor.BLACK)
        black_mapping = [(black_king, self.board.squares[7][1]),
                         (Piece(ChessColor.BLACK), self.board.squares[7][2]),
                         (Piece(ChessColor.BLACK), self.board.squares[7][3])]
        active_pieces_mock.return_value = (white_mapping, black_mapping)
        # should use the correct piece mapping
        # TODO

        # should return false if any piece has a valid move
        valid_move_mock.side_effect = [False, True]
        res = white_player.is_stalemated(self.board)
        self.assertFalse(res)
        valid_move_mock.side_effect = None

        # should return false otherwise
        valid_move_mock.return_value = False
        res = white_player.is_stalemated(self.board)
        self.assertTrue(res)
Example #2
0
    def test_is_checkmated(self, check_mock, active_pieces_mock,
                           valid_move_mock, block_mock):
        """Tests for the player_is_checkmated method."""
        white_config = {'color': ChessColor.WHITE, 'name': 'Griffin'}
        white_player = Player(white_config)

        check_path = ['check path']
        white_king = King(ChessColor.WHITE)
        white_mapping = [(white_king, self.board.squares[1][1]),
                         (Piece(ChessColor.WHITE), self.board.squares[1][2]),
                         (Piece(ChessColor.WHITE), self.board.squares[1][3])]
        black_king = King(ChessColor.BLACK)
        black_mapping = [(black_king, self.board.squares[7][1]),
                         (Piece(ChessColor.BLACK), self.board.squares[7][2]),
                         (Piece(ChessColor.BLACK), self.board.squares[7][3])]
        active_pieces_mock.return_value = (white_mapping, black_mapping)

        # return false if no checking pieces
        check_mock.return_value = []
        res = white_player.is_checkmated(self.board)
        self.assertFalse(res)

        check_mock.return_value = [CheckingReturnType('piece', 'path')]

        # should return false if king can move
        valid_move_mock.return_value = True
        res = white_player.is_checkmated(self.board)
        self.assertFalse(res)
        valid_move_mock.return_value = False

        # should use the correct piece mapping
        # TODO

        # should handle case with > 2 checking pieces
        check_mock.return_value = ['more', 'than', 'one']
        res = white_player.is_checkmated(self.board)
        self.assertTrue(res)
        block_mock.assert_not_called()

        check_path = 'path'
        check_mock.return_value = [CheckingReturnType('piece', check_path)]

        # if king cannot move and 1 checking piece, should call can_block
        block_mock.reset_mock()
        block_mock.return_value = False
        res = white_player.is_checkmated(self.board)
        self.assertTrue(res)
        block_mock.assert_called_with(check_path, self.board,
                                      white_mapping[1:])

        block_mock.return_value = True
        res = white_player.is_checkmated(self.board)
        self.assertFalse(res)
Example #3
0
def board():
    board = Board()

    board.board[4][4].piece = King()
    board.board[4][5].piece = Pawn(is_white=False)

    board.board[6][5].piece = None
    board.board[7][1].piece = None
    board.board[7][2].piece = None
    board.board[7][3].piece = None
    board.board[7][5].piece = None
    board.board[7][6].piece = None

    """ board
    wr0 wh0 wb0 wk0 wq0 wb0 wh0 wr0
    wp0 wp0 wp0 wp0 wp0 wp0 wp0 wp0
    ### ### ### ### ### ### ### ###
    ### ### ### ### ### ### ### ###
    ### ### ### ### wk0 bp0 ### ###
    ### ### ### ### ### ### ### ###
    bp0 bp0 bp0 bp0 bp0 ### bp0 bp0
    br0 ### ### ### bq0 ### ### br0
    """

    return board
Example #4
0
    def __init__(self):
        board = []

        for row_index in range(1, 9):
            board_row = []

            for column_index in range(1, 9):
                cell = Cell(row_index, column_index)
                board_row.append(cell)

                if row_index == 2 or row_index == 7:
                    is_white = row_index == 2
                    pawn = Pawn(is_white=is_white)
                    cell.piece = pawn

            if row_index == 1 or row_index == 8:
                is_white = row_index == 1

                board_row[0].piece = Rook(is_white=is_white)
                board_row[1].piece = Knight(is_white=is_white)
                board_row[2].piece = Bishop(is_white=is_white)
                board_row[3].piece = King(is_white=is_white)
                board_row[4].piece = Queen(is_white=is_white)
                board_row[5].piece = Bishop(is_white=is_white)
                board_row[6].piece = Knight(is_white=is_white)
                board_row[7].piece = Rook(is_white=is_white)

            board.append(board_row)

        self.board = board
Example #5
0
def test_piece_init():
    pawn = Pawn()
    rook = Rook(is_white=False)
    bishop = Bishop()
    knight = Knight()
    queen = Queen()
    king = King()

    assert pawn.color == 'white'
    assert rook.color == 'black'
    assert bishop.color == 'white'
    assert knight.color == 'white'
    assert queen.color == 'white'
    assert king.color == 'white'
Example #6
0
def castle_test():
    b = StandardBoard()
    k = King(ChessColor.WHITE)
    lr = Rook(ChessColor.WHITE)
    rr = Rook(ChessColor.WHITE)
    b.squares[0][0].piece = lr
    b.squares[0][7].piece = rr
    b.squares[0][4].piece = k
    print(b)
    print('Castling...')
    b.move_piece((0, 4), (0, 6), ChessColor.WHITE)
    print(b)
    print('Undoing castling...')
    b.undo_move()
    print(b)
Example #7
0
    def _cell_factory(x: int, y: int, piece_str: str):
        is_white = True if piece_str[0] == 'w' else False
        piece_name = piece_str[1]
        has_moved = piece_str[2] == "1"

        piece = None
        if piece_name == 'p':
            piece = Pawn(is_white=is_white)
        elif piece_name == 'r':
            piece = Rook(is_white=is_white)
        elif piece_name == 'h':
            piece = Knight(is_white=is_white)
        elif piece_name == 'b':
            piece = Bishop(is_white=is_white)
        elif piece_name == 'k':
            piece = King(is_white=is_white)
        elif piece_name == 'q':
            piece = Queen(is_white=is_white)

        if piece:
            piece.has_moved = has_moved

        return Cell(x + 1, y + 1, piece, is_white=is_white)
Example #8
0
def test_move():
    board = Board()

    board.board[3][3].piece = King()

    """ board
    wr0 wh0 wb0 wk0 wq0 wb0 wh0 wr0
    wp0 wp0 wp0 wp0 wp0 wp0 wp0 wp0
    ### ### ### ### ### ### ### ###
    ### ### ### wk0 ### ### ### ###
    ### ### ### ### ### ### ### ###
    ### ### ### ### ### ### ### ###
    bp0 bp0 bp0 bp0 bp0 bp0 bp0 bp0
    br0 bh0 bb0 bk0 bq0 bb0 bh0 br0
    """

    expected_hints = [
        [3, 3], [3, 4], [3, 5], [4, 3], [4, 5], [5, 3], [5, 4], [5, 5]
    ]
    king = board.board[3][3].piece

    hints = king.hints(board.board)

    assert_lists_equivalent(expected_hints, hints)
Example #9
0
 def setUpClass(cls):
     cls.king = King(ChessColor.WHITE)
     cls.board = StandardBoard()