Example #1
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)
Example #2
0
    def test_queen_possible_moves(self):
        """
        Move a queen 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', 'a4', 'a5', 'a6', 'a7', 'a8',
                                  'b1', 'b2', 'c1', 'c3', 'd1', 'd4', 'e1',
                                  'e5', 'f1', 'f6', 'g1', 'g7', 'h1', 'h8'],
                           'a8': ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7',
                                  'b7', 'b8', 'c6', 'c8', 'd5', 'd8', 'e4',
                                  'e8', 'f3', 'f8', 'g2', 'g8', 'h1', 'h8'],
                           'h1': ['a1', 'a8', 'b1', 'b7', 'c1', 'c6', 'd1',
                                  'd5', 'e1', 'e4', 'f1', 'f3', 'g1', 'g2',
                                  'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8'],
                           'h8': ['a1', 'a8', 'b2', 'b8', 'c3', 'c8', 'd4',
                                  'd8', 'e5', 'e8', 'f6', 'f8', 'g7', 'g8',
                                  'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7'],
                           'd4': ['a1', 'a4', 'a7', 'b2', 'b4', 'b6', 'c3',
                                  'c4', 'c5', 'd1', 'd2', 'd3', 'd5', 'd6',
                                  'd7', 'd8', 'e3', 'e4', 'e5', 'f2', 'f4',
                                  'f6', 'g1', 'g4', 'g7', 'h4', 'h8']
                           }
        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] = Queen(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(expected_possible_moves, possible_moves, message)
Example #3
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)
Example #4
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')
Example #5
0
 def test_rook_checkmate(self):
     """
     Test that a rook will put a king of the opposite color in checkmate
     :return:
     """
     board = ChessBoard(empty_board=True)
     board['a3'] = King(Color.white)
     board['a5'] = Rook(Color.black)
     board['b8'] = Rook(Color.black)
     self.assertTrue(board.is_checkmate(Color.white), 'King should be in checkmate')
Example #6
0
 def test_bishop_checkmate(self):
     """
     Test that a queen will put a king of the opposite color in checkmate
     :return:
     """
     board = ChessBoard(empty_board=True)
     board['a8'] = King(Color.white)
     board['a6'] = Knight(Color.black)
     board['b6'] = King(Color.black)
     board['c6'] = Bishop(Color.black)
     self.assertTrue(board.is_checkmate(Color.white), 'King should be in checkmate')
Example #7
0
 def test_pawn_checkmate(self):
     """
     Test that a pawn will put a king of the opposite color in checkmate
     :return:
     """
     board = ChessBoard(empty_board=True)
     board['a1'] = King(Color.white)
     board['a2'] = Pawn(Color.white)
     board['b1'] = Bishop(Color.white)
     board['b2'] = Pawn(Color.black)
     board['c3'] = Pawn(Color.black)
     self.assertTrue(board.is_checkmate(Color.white), 'King should be in checkmate')
Example #8
0
 def test_pawn_check(self):
     """
     Test that a pawn will put a king of the opposite color in check
     :return:
     """
     piece_positions = [('c2', 'b1'), ('a2', 'b1')]
     for positions in piece_positions:
         pawn_position, king_position = positions
         with self.subTest(pawn_position=pawn_position, king_position=king_position):
             board = ChessBoard(empty_board=True)
             board[pawn_position] = Pawn(Color.black)
             board[king_position] = King(Color.white)
             self.assertTrue(board.is_check(Color.white), 'Pawn should put king in check')
Example #9
0
 def test_queen_checkmate(self):
     """
     Test that a queen will put a king of the opposite color in checkmate
     :return:
     """
     board = ChessBoard(empty_board=True)
     board['d6'] = King(Color.black)
     board['c5'] = Pawn(Color.black)
     board['e5'] = Pawn(Color.black)
     board['a5'] = Bishop(Color.white)
     board['d5'] = Queen(Color.white)
     board['d2'] = Rook(Color.white)
     board['g6'] = Knight(Color.white)
     self.assertTrue(board.is_checkmate(Color.black), 'King should be in checkmate')
Example #10
0
 def test_bishop_check(self):
     """
     Test that a bishop will put a king of the opposite color in check
     :return:
     """
     piece_positions = [('a1', 'h8'), ('a8', 'h1'),
                        ('h8', 'a1'), ('h1', 'a8')]
     for positions in piece_positions:
         bishop_position, king_position = positions
         with self.subTest(bishop_position=bishop_position, king_position=king_position):
             board = ChessBoard(empty_board=True)
             board[bishop_position] = Bishop(Color.black)
             board[king_position] = King(Color.white)
             self.assertTrue(board.is_check(Color.white), 'Bishop should put king in check')
Example #11
0
 def test_queen_check(self):
     """
     Test that a queen will put a king of the opposite color in check
     :return:
     """
     piece_positions = [('a1', 'a8'), ('a1', 'h1'), ('a1', 'h8'),
                        ('a8', 'a1'), ('a8', 'h8'), ('a8', 'h8'),
                        ('h8', 'a1'), ('h8', 'a8'), ('h8', 'h1'),
                        ('h1', 'a1'), ('h1', 'a8'), ('h1', 'h8')]
     for positions in piece_positions:
         queen_position, king_position = positions
         with self.subTest(queen_position=queen_position, king_position=king_position):
             board = ChessBoard(empty_board=True)
             board[queen_position] = Queen(Color.black)
             board[king_position] = King(Color.white)
             self.assertTrue(board.is_check(Color.white), 'Queen should put king in check')
Example #12
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)
Example #13
0
 def test_knight_check(self):
     """
     Test that a knight will put a king of the opposite color in check
     :return:
     """
     piece_positions = [('a1', 'b3'), ('a1', 'c2'),
                        ('d4', 'e6'), ('d4', 'f5'),
                        ('d4', 'f3'), ('d4', 'e2'),
                        ('d4', 'c2'), ('d4', 'b3'),
                        ('d4', 'b5'), ('d4', 'c6')]
     for positions in piece_positions:
         knight_position, king_position = positions
         with self.subTest(knight_position=knight_position, king_position=king_position):
             board = ChessBoard(empty_board=True)
             board[knight_position] = Knight(Color.black)
             board[king_position] = King(Color.white)
             self.assertTrue(board.is_check(Color.white), 'Knight should put king in check')
Example #14
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)
Example #15
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)
Example #16
0
    def test_pawn_cant_capture(self):
        """
        Move a pawn to a square where there is a piece of the same color on one of the most immediate diagonal
        squares.
        Expected result is that the square that contains the piece of the same color is not in the list of possible
        moves for the pawn.
        :return:
        """
        board = ChessBoard(empty_board=True)
        start_position = 'b1'
        board[start_position] = Pawn(Color.white)
        board['c2'] = Bishop(Color.white)
        expected_possible_moves = ['b2', 'b3']
        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)
Example #17
0
    def test_bishop_possible_moves(self):
        """
        Move a bishop to each corner and one middle square.
        Expected result is that all the possible moves match the expected list.
        :return:
        """
        start_positions = {'a1': ['b2', 'c3', 'd4', 'e5', 'f6', 'g7', 'h8'],
                           'a8': ['b7', 'c6', 'd5', 'e4', 'f3', 'g2', 'h1'],
                           'h1': ['a8', 'b7', 'c6', 'd5', 'e4', 'f3', 'g2'],
                           'h8': ['a1', 'b2', 'c3', 'd4', 'e5', 'f6', 'g7'],
                           'd4': ['a1', 'a7', 'b2', 'b6', 'c3', 'c5', 'e3', 'e5', 'f2', 'f6', 'g1', 'g7', 'h8']
                           }
        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] = Bishop(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(expected_possible_moves, possible_moves, message)
Example #18
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 ')
Example #19
0
    def test_knight_possible_moves(self):
        """
        Move a knight to each corner and one middle square.
        Expected result is that all the possible moves match the expected list.
        :return:
        """
        start_positions = {'a1': ['b3', 'c2'],
                           'a8': ['b6', 'c7'],
                           'h1': ['f2', 'g3'],
                           'h8': ['f7', 'g6'],
                           'd4': ['b3', 'b5', 'c2', 'c6', 'e2', 'e6', 'f3', 'f5'],
                           'g7': ['e6', 'e8', 'f5', 'h5']
                           }
        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] = Knight(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(expected_possible_moves, possible_moves, message)
Example #20
0
    def test_possible_squares(self):
        # Bottom left corner
        board = ChessBoard(empty_board=True)
        start_position = 'a1'
        directions = {MoveDirection.forward: ['a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8'],
                      MoveDirection.f_right_diag: ['b2', 'c3', 'd4', 'e5', 'f6', 'g7', 'h8'],
                      MoveDirection.right: ['b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1'],
                      MoveDirection.b_right_diag: [],
                      MoveDirection.backward: [],
                      MoveDirection.b_left_diag: [],
                      MoveDirection.left: [],
                      MoveDirection.f_left_diag: [],
                      MoveDirection.l_shape: ['b3', 'c2']
                      }
        for move_direction, expected_possible_squares in directions.items():
            with self.subTest(move_direction=move_direction, expected_possible_squares=expected_possible_squares):
                actual_possible_squares = board.get_possible_positions(start_position, move_direction, Color.white)

                if move_direction == MoveDirection.l_shape:
                    actual_possible_squares.sort()
                message = 'Expected squares do not match actual squares'
                self.assertListEqual(expected_possible_squares, actual_possible_squares, message)

        # Top right corner
        board = ChessBoard(empty_board=True)
        start_position = 'h8'
        directions = {MoveDirection.forward: [],
                      MoveDirection.f_right_diag: [],
                      MoveDirection.right: [],
                      MoveDirection.b_right_diag: [],
                      MoveDirection.backward: ['h7', 'h6', 'h5', 'h4', 'h3', 'h2', 'h1'],
                      MoveDirection.b_left_diag: ['g7', 'f6', 'e5', 'd4', 'c3', 'b2', 'a1'],
                      MoveDirection.left: ['g8', 'f8', 'e8', 'd8', 'c8', 'b8', 'a8'],
                      MoveDirection.f_left_diag: [],
                      MoveDirection.l_shape: ['f7', 'g6']
                      }
        for move_direction, expected_possible_squares in directions.items():
            with self.subTest(move_direction=move_direction, expected_possible_squares=expected_possible_squares):
                actual_possible_squares = board.get_possible_positions(start_position, move_direction, Color.white)

                if move_direction == MoveDirection.l_shape:
                    actual_possible_squares.sort()
                message = 'Expected squares do not match actual squares'
                self.assertListEqual(expected_possible_squares, actual_possible_squares, message)

        # Middle square
        board = ChessBoard(empty_board=True)
        start_position = 'd4'
        directions = {MoveDirection.forward: ['d5', 'd6', 'd7', 'd8'],
                      MoveDirection.f_right_diag: ['e5', 'f6', 'g7', 'h8'],
                      MoveDirection.right: ['e4', 'f4', 'g4', 'h4'],
                      MoveDirection.b_right_diag: ['e3', 'f2', 'g1'],
                      MoveDirection.backward: ['d3', 'd2', 'd1'],
                      MoveDirection.b_left_diag: ['c3', 'b2', 'a1'],
                      MoveDirection.left: ['c4', 'b4', 'a4'],
                      MoveDirection.f_left_diag: ['c5', 'b6', 'a7'],
                      MoveDirection.l_shape: ['b3', 'b5', 'c2', 'c6', 'e2', 'e6', 'f3', 'f5']
                      }
        for move_direction, expected_possible_squares in directions.items():
            with self.subTest(move_direction=move_direction, expected_possible_squares=expected_possible_squares):
                actual_possible_squares = board.get_possible_positions(start_position, move_direction, Color.white)

                if move_direction == MoveDirection.l_shape:
                    actual_possible_squares.sort()
                message = 'Expected squares do not match actual squares'
                self.assertListEqual(expected_possible_squares, actual_possible_squares, message)