Ejemplo n.º 1
0
    def test_rook_capture(self):
        """
        Place a black piece on a square on file 2 and another on rank . Move the rook to the square occupied by a
        Expected result is that the squares that contain the pieces of the opposite color are in the list of possible
        moves for the rook. Opposing piece is also successfully captured by pawn.
        :return:
        """
        board = ChessBoard(empty_board=True)
        start_position = 'b1'
        capture_position = 'e1'
        board[start_position] = Rook(Color.white)
        board['c2'] = Bishop(Color.black)
        board['c8'] = Pawn(Color.black)
        board['e1'] = Bishop(Color.black)

        # Test possible moves with several pieces on possible capture squares
        expected_possible_moves = ['a1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'c1', 'd1', 'e1']
        possible_moves = board.get_possible_moves(start_position)
        possible_moves.sort()

        message = 'Expected move list does not match actual move list'
        self.assertListEqual(expected_possible_moves, possible_moves, message)

        # Confirm piece is captured
        board.move_piece(start_position, capture_position)
        message = 'Rook should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], Rook, message)
Ejemplo n.º 2
0
    def test_move_right(self):
        """
        Move a piece of every type one square to the right.
        Expected result is that the piece no longer exist on the starting square, but on the ending square.
        :return:
        """
        start_positions = ['a1', 'h8']
        end_positions = ['b1', 'g8']
        piece_colors = [Color.white, Color.black]
        piece_types = {
            Type.king: King,
            Type.queen: Queen,
            Type.rook: Rook
        }
        for start, end, color in zip(start_positions, end_positions, piece_colors):
            for t, piece_class in piece_types.items():
                with self.subTest(t=t, piece_class=piece_class, start=start, end=end, color=color):
                    board = ChessBoard(empty_board=True)
                    board[start] = piece_class(Color.white)
                    self.assertFalse(board[start].has_moved, 'Piece has never moved')

                    board.move_piece(start, end)
                    self.assertIsNone(board[start], 'There should no longer be a piece on square ' + start)
                    self.assertIsInstance(board[end], piece_class, 'There should be a piece on square ' + end)
                    self.assertTrue(board[end].has_moved, 'Piece has moved')
Ejemplo n.º 3
0
    def test_pawn_capture(self):
        """
        Move a pawn to a square where there is a piece of the opposite color on one of the most immediate diagonal
        squares.
        Expected result is that the square that contains the piece of the opposite color is in the list of possible
        moves for the pawn. Opposing piece is also successfully captured by pawn.
        :return:
        """
        # Test diagonal move when a piece of the opposite color is present
        board = ChessBoard(empty_board=True)
        start_position = 'b1'
        capture_position = 'c2'
        board[start_position] = Pawn(Color.white)
        board['c2'] = Bishop(Color.black)
        expected_possible_moves = ['b2', 'b3', 'c2']
        possible_moves = board.get_possible_moves(start_position)
        possible_moves.sort()

        message = 'Expected pawn to be able to move diagonally'
        self.assertListEqual(expected_possible_moves, possible_moves, message)

        # place a second piece and confirm both diagonals show as possible moves
        board['a2'] = Rook(Color.black)
        expected_possible_moves = ['a2', 'b2', 'b3', 'c2']
        possible_moves = board.get_possible_moves(start_position)
        possible_moves.sort()

        message = 'Expected pawn to be able to move diagonally in both directions'
        self.assertListEqual(expected_possible_moves, possible_moves, message)

        # Move pawn to capture a piece
        board.move_piece(start_position, capture_position)
        message = 'Pawn should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], Pawn, message)
Ejemplo n.º 4
0
    def test_pawn_possible_moves(self):
        """
        Move a pawn to each corner and one middle square.
        Expected result is that all the possible moves match the expected list.
        :return:
        """
        start_positions = {'a1': ['a2', 'a3'],
                           'a8': [],
                           'h1': ['h2', 'h3'],
                           'h8': [],
                           'd4': ['d5', 'd6']
                           }
        for start_position, expected_possible_moves in start_positions.items():
            with self.subTest(start_position=start_position, expected_possible_moves=expected_possible_moves):
                board = ChessBoard(empty_board=True)
                board[start_position] = Pawn(Color.white)
                possible_moves = board.get_possible_moves(start_position)
                possible_moves.sort()

                message = 'Expected move list does not match actual move list'
                self.assertListEqual(start_positions[start_position], possible_moves, message)

        # Confirm pawn can only move one square after it is moved
        board = ChessBoard(empty_board=True)
        board['a1'] = Pawn(Color.white)
        board.move_piece('a1', 'a3')
        possible_moves = board.get_possible_moves('a3')
        expected_possible_moves = ['a4']

        self.assertListEqual(expected_possible_moves, possible_moves, 'Pawn should not be able to ')
Ejemplo n.º 5
0
    def test_knight_capture(self):
        board = ChessBoard(empty_board=True)
        start_position = 'd4'
        capture_position = 'f5'
        board[start_position] = Knight(Color.white)
        board[capture_position] = Bishop(Color.black)
        expected_possible_moves = ['b3', 'b5', 'c2', 'c6', 'e2', 'e6', 'f3', 'f5']
        possible_moves = board.get_possible_moves(start_position)
        possible_moves.sort()

        message = 'Expected move list does not match actual move list'
        self.assertListEqual(expected_possible_moves, possible_moves, message)

        # Move knight to capture a piece
        board.move_piece(start_position, capture_position)
        message = 'Knight should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], Knight, message)
Ejemplo n.º 6
0
    def test_king_capture(self):
        board = ChessBoard(empty_board=True)
        start_position = 'd4'
        capture_position = 'd5'
        board[start_position] = King(Color.white)
        board['e4'] = Pawn(Color.black)
        board[capture_position] = Pawn(Color.black)
        expected_possible_moves = ['c3', 'c5', 'd5', 'e3', 'e5']
        possible_moves = board.get_possible_moves(start_position)
        possible_moves.sort()

        message = 'Expected move list does not match actual move list'
        self.assertListEqual(expected_possible_moves, possible_moves, message)

        # Move bishop to capture a piece
        board.move_piece(start_position, capture_position)
        message = 'King should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], King, message)
Ejemplo n.º 7
0
    def test_bishop_capture(self):
        board = ChessBoard(empty_board=True)
        start_position = 'd4'
        capture_position = 'h8'
        board[start_position] = Bishop(Color.white)
        board['c5'] = Queen(Color.black)
        board[capture_position] = Pawn(Color.black)
        expected_possible_moves = ['a1', 'b2', 'c3', 'c5', 'e3', 'e5', 'f2', 'f6', 'g1', 'g7', 'h8']
        possible_moves = board.get_possible_moves(start_position)
        possible_moves.sort()

        message = 'Expected move list does not match actual move list'
        self.assertListEqual(expected_possible_moves, possible_moves, message)

        # Move bishop to capture a piece
        board.move_piece(start_position, capture_position)
        message = 'Bishop should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], Bishop, message)