Пример #1
0
    def test_king_cant_capture(self):
        """
        Test a few scenarios where the king cannot capture another piece.
        Expected result is piece next to king cannot be captured and will not be in legal moves list.
        :return:
        """
        # Test scenario where opponent piece backing up other opponent piece.
        board = ChessBoard()
        start_position = 'd4'
        capture_position = 'd5'
        board[start_position] = King(Color.WHITE)
        board['e4'] = Pawn(Color.BLACK)
        board[capture_position] = Pawn(Color.BLACK)

        expected_legal_moves = ['c3', 'c5', 'd5', 'e3', 'e5']
        legal_moves = board.get_legal_moves(start_position)
        legal_moves.sort()

        self.assertListEqual(
            expected_legal_moves, legal_moves,
            'Expected move list does not match actual move list')

        # Test king has piece of same color directly in front of it
        board = ChessBoard()
        board['d4'] = King(Color.WHITE)
        board['d5'] = Pawn(Color.WHITE)

        expected_legal_moves = ['c3', 'c4', 'c5', 'd3', 'e3', 'e4', 'e5']
        legal_moves = board.get_legal_moves('d4')
        legal_moves.sort()

        self.assertListEqual(
            expected_legal_moves, legal_moves,
            'Expected move list does not match actual move list')
Пример #2
0
    def test_queen_capture(self):
        """
        Move queen to position where an opponents piece is in the capture path.
        Expected result is opponents piece is in legal move list and piece is captured when queen moves to that
        position.
        :return:
        """
        board = ChessBoard()
        start_position = 'd4'
        capture_position = 'e4'
        board[start_position] = Queen(Color.WHITE)
        board['d5'] = Pawn(Color.BLACK)
        board[capture_position] = Pawn(Color.BLACK)
        expected_possible_moves = [
            'a1', 'a4', 'a7', 'b2', 'b4', 'b6', 'c3', 'c4', 'c5', 'd1', 'd2',
            'd3', 'd5', 'e3', 'e4', 'e5', 'f2', 'f6', 'g1', 'g7', 'h8'
        ]
        possible_moves = board.get_legal_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 queen to capture a piece
        move_result = board.move_piece(start_position, capture_position)
        message = 'Queen should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], Queen, message)

        # Test move result
        expected_move_result = {
            start_position: None,
            capture_position: Queen(Color.WHITE)
        }
        self.assertDictEqual(expected_move_result, move_result,
                             'Expected move result does not match actual')
Пример #3
0
    def test_pawn_legal_moves_piece_blocking(self):
        """
        Place a pawn on a square and another piece 2 positions in front of it.
        Expected result is there are is one legal move for the pawn that is blocked.
        :return:
        """
        opposing_colors = [[Color.BLACK, Color.WHITE],
                           [Color.WHITE, Color.BLACK]]
        for opposing_color in opposing_colors:
            start_positions = ['b2', 'g7']
            blocking_positions = ['b4', 'g5']
            expected_moves = [['b3'], ['g6']]
            pawn_colors = [Color.WHITE, Color.BLACK]
            for start, blocking, expected, pawn_color, opposing in zip(
                    start_positions, blocking_positions, expected_moves,
                    pawn_colors, opposing_color):
                with self.subTest(start=start,
                                  blocking=blocking,
                                  expected=expected,
                                  pawn_color=pawn_color,
                                  opposing=opposing):
                    board = ChessBoard()
                    board[start] = Pawn(pawn_color)
                    board[blocking] = Pawn(opposing)

                    legal_moves = board.get_legal_moves(start)
                    legal_moves.sort()

                    message = 'Pawn should only have one legal move'
                    self.assertListEqual(expected, legal_moves, message)
Пример #4
0
 def test_pawn_checkmate(self):
     """
     Test that a pawn will put a king of the opposite color in checkmate
     :return:
     """
     board = ChessBoard()
     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')
Пример #5
0
 def test_queen_checkmate(self):
     """
     Test that a queen will put a king of the opposite color in checkmate
     :return:
     """
     board = ChessBoard()
     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')
Пример #6
0
    def test_king_capture(self):
        """
        Move king to square right next to piece of opposing color with nothing backing it up.
        Expected result is position with opponent piece is in legal move list and piece is captured
        when king moves to that position.
        :return:
        """
        board = ChessBoard()
        start_position = 'd4'
        capture_position = 'e4'
        board[start_position] = King(Color.WHITE)
        board[capture_position] = Pawn(Color.BLACK)

        expected_legal_moves = ['c3', 'c4', 'c5', 'd5', 'e3', 'e4', 'e5']
        possible_moves = board.get_legal_moves(start_position)
        possible_moves.sort()

        self.assertListEqual(
            expected_legal_moves, possible_moves,
            'Expected move list does not match actual move list')

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

        # Test move result
        expected_move_result = {
            start_position: None,
            capture_position: King(Color.WHITE)
        }
        self.assertDictEqual(expected_move_result, move_result,
                             'Expected move result does not match actual')
Пример #7
0
    def test_is_not_stalemate(self):
        """
        Configure board where white player has one legal move.
        Expected result is stalemate has not occurred.
        :return:
        """
        # Try for king
        board = ChessBoard()
        board['a8'] = King(Color.BLACK)
        board['c1'] = Rook(Color.WHITE)
        board['d7'] = Rook(Color.WHITE)

        is_stalemate = board.is_stalemate(Color.BLACK)
        self.assertFalse(is_stalemate,
                         'Board configuration should not result in stalemate')

        # Try for piece other than king
        board = ChessBoard()
        board['a8'] = King(Color.BLACK)
        board['b1'] = Rook(Color.WHITE)
        board['d7'] = Rook(Color.WHITE)
        board['f2'] = Pawn(Color.BLACK)

        is_stalemate = board.is_stalemate(Color.BLACK)
        self.assertFalse(is_stalemate,
                         'Board configuration should not result in stalemate')
Пример #8
0
    def test_bishop_capture(self):
        """
        Move a bishop to square where there is a piece of the opposite color on capture diagonal.
        Expected result is position with opponent piece is in legal move list and piece is captured
        when bishop moves to that position.
        :return:
        """
        board = ChessBoard()
        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_legal_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
        move_result = board.move_piece(start_position, capture_position)
        message = 'Bishop should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], Bishop, message)

        # Test move result
        expected_move_result = {
            start_position: None,
            capture_position: Bishop(Color.WHITE)
        }
        self.assertDictEqual(expected_move_result, move_result,
                             'Expected move result does not match actual')
Пример #9
0
    def test_pawn_legal_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 = {
            Color.WHITE: {
                'a1': ['a2', 'a3'],
                'a8': [],
                'h1': ['h2', 'h3'],
                'h8': [],
                'd4': ['d5', 'd6']
            },
            Color.BLACK: {
                'a1': [],
                'a8': ['a6', 'a7'],
                'h1': [],
                'h8': ['h6', 'h7'],
                'd4': ['d2', 'd3']
            }
        }
        for color, positions in start_positions.items():
            for start_position, expected_moves in positions.items():
                with self.subTest(color=color,
                                  start_position=start_position,
                                  expected_moves=expected_moves):
                    board = ChessBoard()
                    board[start_position] = Pawn(color)
                    possible_moves = board.get_legal_moves(start_position)
                    possible_moves.sort()

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

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

        self.assertListEqual(expected_possible_moves, possible_moves,
                             'Pawn should not be able to ')
Пример #10
0
    def test_piece_pinned(self):
        """
        Test moving a piece of every type that is the same color as king but pinned by opponents piece.
        Expected result is legal move list for piece should be empty.
        :return:
        """
        # Pawn pined
        board = ChessBoard()
        board['c3'] = King(Color.WHITE)
        board['d4'] = Pawn(Color.WHITE)
        board['f6'] = Bishop(Color.BLACK)

        legal_moves = board.get_legal_moves('d4')
        self.assertListEqual([], legal_moves,
                             'Piece should not have any legal moves.')

        # Rook pined
        board = ChessBoard()
        board['c3'] = King(Color.WHITE)
        board['d4'] = Rook(Color.WHITE)
        board['f6'] = Bishop(Color.BLACK)

        legal_moves = board.get_legal_moves('d4')
        self.assertListEqual([], legal_moves,
                             'Piece should not have any legal moves.')

        # Knight pined
        board = ChessBoard()
        board['c3'] = King(Color.WHITE)
        board['d4'] = Knight(Color.WHITE)
        board['f6'] = Bishop(Color.BLACK)

        legal_moves = board.get_legal_moves('d4')
        self.assertListEqual([], legal_moves,
                             'Piece should not have any legal moves.')

        # Bishop pined
        board = ChessBoard()
        board['c3'] = King(Color.WHITE)
        board['c5'] = Bishop(Color.WHITE)
        board['c6'] = Rook(Color.BLACK)

        legal_moves = board.get_legal_moves('c5')
        self.assertListEqual([], legal_moves,
                             'Piece should not have any legal moves.')

        # Queen kinda pined
        board = ChessBoard()
        board['c3'] = King(Color.WHITE)
        board['c4'] = Queen(Color.WHITE)
        board['c6'] = Rook(Color.BLACK)

        legal_moves = board.get_legal_moves('c4')
        self.assertListEqual(['c5', 'c6'], legal_moves,
                             'Legal moves dont match expected moves.')
Пример #11
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()
        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_legal_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_legal_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
        move_result = board.move_piece(start_position, capture_position)
        message = 'Pawn should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], Pawn, message)

        # Test move result
        expected_move_result = {
            start_position: None,
            capture_position: Pawn(Color.WHITE)
        }
        self.assertDictEqual(expected_move_result, move_result,
                             'Expected move result does not match actual')
Пример #12
0
    def test_can_en_passant(self):
        """
        Test that a pawn can perform en passant move.
        :return:
        """
        board = ChessBoard()

        # En passant from white's perspective.
        board['b5'] = Pawn(Color.WHITE)
        board['a7'] = Pawn(Color.BLACK)
        board.move_piece('a7', 'a5')
        self.assertTrue(board.can_en_passant('b5', MoveDirection.F_LEFT_DIAG),
                        'Pawn should be able to perform en passant')

        # En passant from black's perspective.
        board['g4'] = Pawn(Color.BLACK)
        board['h2'] = Pawn(Color.WHITE)
        board.move_piece('h2', 'h4')
        self.assertTrue(board.can_en_passant('g4', MoveDirection.F_LEFT_DIAG),
                        'Pawn should be able to perform en passant')
Пример #13
0
    def test_move_result_updated_positions(self):
        """
        Move a piece as a player.
        Expected result is start position is empty and end position contains the moved piece.
        :return:
        """
        game = ChessGame(white_player=self.p1, black_player=self.p2)
        result = game.move_piece('d2', 'd4')

        expected_move_result = MoveResult()
        expected_move_result.update_positions = {'d2': None, 'd4': Pawn(Color.WHITE)}
        self.assertEqual(expected_move_result, result)
Пример #14
0
    def test_cannot_en_passant(self):
        """
        Test that a pawn cannot perform en passant move.
        :return:
        """
        board = ChessBoard()

        # Confirm pawn that just moved 2 spaces can't perform enpassant.
        board['b5'] = Pawn(Color.WHITE)
        board['a7'] = Pawn(Color.BLACK)
        board.move_piece('a7', 'a5')
        self.assertFalse(board.can_en_passant('a5', MoveDirection.F_LEFT_DIAG),
                         'Pawn should not be able to perform en passant')

        # Confirm when all condition have been met but push pawn moved one square twice, en passant can't happen.
        board['g4'] = Pawn(Color.BLACK)
        board['h2'] = Pawn(Color.WHITE)
        board.move_piece('h2', 'h3')
        board.move_piece('h3', 'h4')
        self.assertFalse(board.can_en_passant('g4', MoveDirection.F_LEFT_DIAG),
                         'Pawn should be able to perform en passant')
Пример #15
0
 def test_knight_checkmate(self):
     """
     Test that a knight will put a king of the opposite color in checkmate
     :return:
     """
     board = ChessBoard()
     board['b4'] = Bishop(Color.BLACK)
     board['c5'] = Rook(Color.BLACK)
     board['d1'] = King(Color.WHITE)
     board['e3'] = Knight(Color.BLACK)
     board['f3'] = Pawn(Color.BLACK)
     self.assertTrue(board.is_checkmate(Color.WHITE),
                     'King should be in checkmate')
Пример #16
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()
             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')
Пример #17
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()
        start_position = 'b1'
        board[start_position] = Pawn(Color.WHITE)
        board['c2'] = Bishop(Color.WHITE)
        expected_possible_moves = ['b2', 'b3']
        possible_moves = board.get_legal_moves(start_position)
        possible_moves.sort()

        message = 'Expected move list does not match actual move list'
        self.assertListEqual(expected_possible_moves, possible_moves, message)
Пример #18
0
    def test_move_result_capture(self):
        """
        Perform piece capture.
        Expected result is move result states captured piece no longer on board, piece that moved is in new location,
        and previous location for piece that moved is empty.
        :return:
        """
        fen = 'rnbqkb1r/pppppppp/5n2/8/4P3/3P4/PPP2PPP/RNBQKBNR b KQkq -'
        game = ChessGame(fen=fen, white_player=self.p1, black_player=self.p2)
        result = game.move_piece('f6', 'e4')

        expected_move_result = MoveResult()
        expected_move_result.update_positions = {'f6': None, 'e4': Knight(Color.BLACK)}
        self.assertEqual(expected_move_result, result)

        result = game.move_piece('d3', 'e4')
        expected_move_result.update_positions = {'d3': None, 'e4': Pawn(Color.WHITE)}
        self.assertEqual(result, expected_move_result)
Пример #19
0
    def test_bishop_cant_capture(self):
        """
        Move bishop to square and place piece of same color in movement path.
        Expected result is that the square containing the piece of the same color is not in the list of legal
        moves.
        :return:
        """
        color_group = [Color.WHITE, Color.BLACK]
        for color in color_group:
            with self.subTest(color=color):
                board = ChessBoard()
                board['b2'] = Bishop(color)
                board['c3'] = Pawn(color)

                expected_moves = ['a1', 'a3', 'c1']
                legal_moves = board.get_legal_moves('b2')
                legal_moves.sort()
                self.assertListEqual(
                    expected_moves, legal_moves,
                    'Expected list does not match actual legal move list')
Пример #20
0
    def test_queen_cant_capture(self):
        """
        Move queen to a square and place piece of same color in movement path.
        Expected result is that the square containing the piece of the same color is not in the list of legal
        moves.
        :return:
        """
        board = ChessBoard()
        board['b2'] = Queen(Color.WHITE)
        board['b3'] = Pawn(Color.WHITE)

        expected_legal_moves = [
            'a1', 'a2', 'a3', 'b1', 'c1', 'c2', 'c3', 'd2', 'd4', 'e2', 'e5',
            'f2', 'f6', 'g2', 'g7', 'h2', 'h8'
        ]
        legal_moves = board.get_legal_moves('b2')
        legal_moves.sort()

        self.assertListEqual(expected_legal_moves, legal_moves,
                             'Expected move list does not match actual')
Пример #21
0
    def test_rook_cant_capture(self):
        """
        Move rook to a square and another piece of the same color immediately to the right of the rook.
        Expected result is that the square containing the pieces of the same color is not in the list of legal
        moves for the rook.
        :return:
        """
        color_group = [Color.WHITE, Color.BLACK]
        for color in color_group:
            with self.subTest(color=color):
                board = ChessBoard()
                board['d4'] = Rook(color)
                board['e4'] = Pawn(color)

                expected_moves = [
                    'a4', 'b4', 'c4', 'd1', 'd2', 'd3', 'd5', 'd6', 'd7', 'd8'
                ]
                legal_moves = board.get_legal_moves('d4')
                legal_moves.sort()
                self.assertListEqual(
                    expected_moves, legal_moves,
                    'Expected moves does not match legal moves')
Пример #22
0
    def test_pawn_movement_adjusted_after_moving(self):
        """
        Move a pawn of each color
        Expected result is pawn cannot move forward two squares once it has moved.
        :return:
        """
        board = ChessBoard()
        positions = {Color.WHITE: ('b2', 'b3'), Color.BLACK: ('b7', 'b6')}
        expected_directions = {
            MoveDirection.FORWARD: 1,
            MoveDirection.F_RIGHT_DIAG: 1,
            MoveDirection.F_LEFT_DIAG: 1
        }
        for color in [Color.BLACK, Color.WHITE]:
            with self.subTest(color=color, movement=positions[color]):
                start_pos, end_pos = positions[color]
                board[start_pos] = Pawn(color)
                board.move_piece(start_pos, end_pos)

                self.assertDictEqual(expected_directions,
                                     board[end_pos].move_directions,
                                     'Incorrect move_directions')
Пример #23
0
    def test_rook_capture(self):
        """
        Move a rook to square where there are pieces of the opposite color on capture file and rank.
        Expected result is that the squares that contain the pieces of the opposite color are in the list of possible
        moves for the rook. One opposing piece is also successfully captured by rook.
        :return:
        """
        board = ChessBoard()
        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_legal_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
        move_result = board.move_piece(start_position, capture_position)
        message = 'Rook should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], Rook, message)

        # Test move result
        expected_move_result = {
            start_position: None,
            capture_position: Rook(Color.WHITE)
        }
        self.assertDictEqual(expected_move_result, move_result,
                             'Expected move result does not match actual')
Пример #24
0
    def test_en_passant_capture(self):
        """
        Test capturing an opponents pawn via en passant.
        Expected result is opponents piece is successfully captured when en passant move is performed.
        :return:
        """
        board = ChessBoard()

        # Check from white perspective
        board['b5'] = Pawn(Color.WHITE)
        board['a7'] = Pawn(Color.BLACK)
        board.move_piece('a7', 'a5')
        move_result = board.move_piece('b5', 'a6')
        self.assertIsNone(board['a5'], 'Expected black pawn to be captured')

        # Test move result
        expected_move_result = {
            'b5': None,
            'a5': None,
            'a6': Pawn(Color.WHITE)
        }
        self.assertDictEqual(expected_move_result, move_result,
                             'Expected move result does not match actual')

        # Check from black perspective
        board['e4'] = Pawn(Color.BLACK)
        board['d2'] = Pawn(Color.WHITE)
        board.move_piece('d2', 'd4')
        move_result = board.move_piece('e4', 'd3')
        self.assertIsNone(board['d4'], 'Expected black pawn to be captured')

        # Test move result
        expected_move_result = {
            'e4': None,
            'd4': None,
            'd3': Pawn(Color.BLACK)
        }
        self.assertDictEqual(expected_move_result, move_result,
                             'Expected move result does not match actual')
Пример #25
0
    def test_empty_fen(self):
        """
        Create fen object without supplying a FEN string
        Expect default chess board
        :return:
        """
        fen = Fen()

        expected_board = {
            'a1': Rook(Color.WHITE), 'b1': Knight(Color.WHITE), 'c1': Bishop(Color.WHITE), 'd1': Queen(Color.WHITE),
            'e1': King(Color.WHITE), 'f1': Bishop(Color.WHITE), 'g1': Knight(Color.WHITE), 'h1': Rook(Color.WHITE),
            'a2': Pawn(Color.WHITE), 'b2': Pawn(Color.WHITE), 'c2': Pawn(Color.WHITE), 'd2': Pawn(Color.WHITE),
            'e2': Pawn(Color.WHITE), 'f2': Pawn(Color.WHITE), 'g2': Pawn(Color.WHITE), 'h2': Pawn(Color.WHITE),
            'a8': Rook(Color.BLACK), 'b8': Knight(Color.BLACK), 'c8': Bishop(Color.BLACK), 'd8': Queen(Color.BLACK),
            'e8': King(Color.BLACK), 'f8': Bishop(Color.BLACK), 'g8': Knight(Color.BLACK), 'h8': Rook(Color.BLACK),
            'a7': Pawn(Color.BLACK), 'b7': Pawn(Color.BLACK), 'c7': Pawn(Color.BLACK), 'd7': Pawn(Color.BLACK),
            'e7': Pawn(Color.BLACK), 'f7': Pawn(Color.BLACK), 'g7': Pawn(Color.BLACK), 'h7': Pawn(Color.BLACK),
        }
        self.assertEqual(expected_board, fen.board)
Пример #26
0
    def test_different_board_setups(self):
        """
        Create Fen object using empty board, middle game, end game
        Expect board config to match expected value
        :return:
        """
        # Empty board
        fen_str = '8/8/8/8/8/8/8/8 w - -'
        expected_board1 = {}
        fen = Fen(fen_str)
        self.assertEqual(expected_board1, fen.board)

        # Partial game
        fen_str = 'r2qkbnr/2pp1ppp/b1n5/1N2p3/4P1Q1/8/PPPP1PPP/R1B1K1NR w KQkq -'
        expected_board2 = {
            'a1': Rook(Color.WHITE), 'c1': Bishop(Color.WHITE), 'e1': King(Color.WHITE), 'g1': Knight(Color.WHITE),
            'h1': Rook(Color.WHITE), 'a2': Pawn(Color.WHITE), 'b2': Pawn(Color.WHITE), 'c2': Pawn(Color.WHITE),
            'd2': Pawn(Color.WHITE), 'f2': Pawn(Color.WHITE), 'g2': Pawn(Color.WHITE), 'h2': Pawn(Color.WHITE),
            'a8': Rook(Color.BLACK), 'd8': Queen(Color.BLACK), 'e8': King(Color.BLACK), 'f8': Bishop(Color.BLACK),
            'g8': Knight(Color.BLACK), 'h8': Rook(Color.BLACK),'c7': Pawn(Color.BLACK), 'd7': Pawn(Color.BLACK),
            'f7': Pawn(Color.BLACK), 'g7': Pawn(Color.BLACK), 'h7': Pawn(Color.BLACK), 'a6': Bishop(Color.BLACK),
            'c6': Knight(Color.BLACK), 'e5': Pawn(Color.BLACK), 'b5': Knight(Color.WHITE), 'e4': Pawn(Color.WHITE),
            'g4': Queen(Color.WHITE)
        }
        fen = Fen(fen_str)
        self.assertEqual(expected_board2, fen.board)

        # End game
        fen_str = '5rk1/7p/8/4p1p1/2nP4/2PB4/bP4QP/2K4R w - -'
        expected_board3 = {
            'c1': King(Color.WHITE), 'h1': Rook(Color.WHITE), 'b2': Pawn(Color.WHITE), 'g2': Queen(Color.WHITE),
            'h2': Pawn(Color.WHITE), 'c3': Pawn(Color.WHITE), 'd3': Bishop(Color.WHITE), 'd4': Pawn(Color.WHITE),
            'a2': Bishop(Color.BLACK), 'c4': Knight(Color.BLACK), 'e5': Pawn(Color.BLACK), 'g5': Pawn(Color.BLACK),
            'h7': Pawn(Color.BLACK), 'f8': Rook(Color.BLACK), 'g8': King(Color.BLACK)
        }
        fen = Fen(fen_str)
        self.assertEqual(expected_board3, fen.board)