Exemplo n.º 1
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')
Exemplo n.º 2
0
    def test_pawn_capture_promotion(self):
        """
        Perform piece capture with a pawn that will place the pawn on the last row.
        Expected result is the pawn can be promoted.
        :return:
        """
        self.p1.color = Color.WHITE
        self.p2.color = Color.BLACK

        fen = '2b1k2r/5pP1/2n1p3/1P5p/3B1P2/2P3N1/7P/3RK3 w - -'
        game = ChessGame(fen=fen, white_player=self.p1, black_player=self.p2)

        # Capture the black pawn
        result = game.move_piece('g7', 'h8')

        expected_move_result = MoveResult()
        expected_move_result.pawn_promote_info = {
            'player': self.p1,
            'promote_types': game.get_pawn_promote_types()
        }
        self.assertEqual(expected_move_result, result)

        # Promote the piece and confirm move result
        result = game.promote_pawn('g7', 'h8', Type.QUEEN)
        expected_promote_result = MoveResult()
        expected_promote_result.update_positions = {'g7': None, 'h8': Queen(Color.WHITE)}
        expected_promote_result.king_in_check = self.p2
        self.assertEqual(expected_promote_result, result)
Exemplo n.º 3
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')
Exemplo n.º 4
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.')
Exemplo n.º 5
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)
Exemplo n.º 6
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')
Exemplo n.º 7
0
    def test_move_result_checkmate(self):
        """
        Move a piece to place the king in checkmate.
        Expected result is the king is placed in checkmate an the is_over flag is set to True.
        :return:
        """
        self.p1.color = Color.WHITE
        self.p2.color = Color.BLACK

        # Confirm checkmate and game_over flag set to True
        # Minimal
        fen = '8/8/8/8/6q1/3k4/8/4K3 b - -'
        game = ChessGame(fen=fen, white_player=self.p1, black_player=self.p2)
        self.assertFalse(game.is_over)

        result = game.move_piece('g4', 'e2')
        expected_move_result = MoveResult()
        expected_move_result.update_positions = {'g4': None, 'e2': Queen(Color.BLACK)}
        expected_move_result.king_in_checkmate = self.p1
        expected_move_result.king_in_check = self.p1
        self.assertEqual(expected_move_result, result)
        self.assertTrue(game.is_over)

        # Medium
        fen = '8/1r6/8/4k3/1q6/8/8/K7 b KQkq -'
        game = ChessGame(fen=fen, white_player=self.p1, black_player=self.p2)
        self.assertFalse(game.is_over)

        result = game.move_piece('b7', 'a7')
        expected_move_result = MoveResult()
        expected_move_result.update_positions = {'b7': None, 'a7': Rook(Color.BLACK)}
        expected_move_result.king_in_checkmate = self.p1
        expected_move_result.king_in_check = self.p1
        self.assertEqual(expected_move_result, result)
        self.assertTrue(game.is_over)

        # Complex
        fen = '8/3n4/4b3/2q1k3/K7/2P5/3RB1p1/8 b - -'
        game = ChessGame(fen=fen, white_player=self.p1, black_player=self.p2)
        self.assertFalse(game.is_over)

        result = game.move_piece('d7', 'b6')
        expected_move_result = MoveResult()
        expected_move_result.update_positions = {'d7': None, 'b6': Knight(Color.BLACK)}
        expected_move_result.king_in_checkmate = self.p1
        expected_move_result.king_in_check = self.p1
        self.assertEqual(expected_move_result, result)
        self.assertTrue(game.is_over)
Exemplo n.º 8
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()
             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')
Exemplo n.º 9
0
    def test_move_result_king_in_check(self):
        """
        Put a king in check.
        Game should return the game state indicating which king is in check but game over flag is not set.
        :return:
        """
        self.p1.color = Color.WHITE
        self.p2.color = Color.BLACK

        fen = 'rnbqkbnr/ppp2ppp/4p3/3p4/8/2P5/PP1PPPPP/RNBQKBNR w KQkq -'
        game = ChessGame(fen=fen, white_player=self.p1, black_player=self.p2)
        self.assertFalse(game.is_over)

        result = game.move_piece('d1', 'a4')
        expected_promote_result = MoveResult()
        expected_promote_result.update_positions = {'d1': None, 'a4': Queen(Color.WHITE)}
        expected_promote_result.king_in_check = self.p2
        self.assertEqual(expected_promote_result, result)
        self.assertFalse(game.is_over)
Exemplo n.º 10
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')
Exemplo n.º 11
0
    def test_move_result_pawn_promotion(self):
        self.p1.color = Color.WHITE
        self.p2.color = Color.BLACK

        fen = '8/P2n4/4b3/2q1k3/K7/8/3RB1p1/8 b - -'
        game = ChessGame(fen=fen, white_player=self.p1, black_player=self.p2)

        # Move black pawn
        result = game.move_piece('g2', 'g1')
        expected_move_result = MoveResult()
        expected_move_result.pawn_promote_info = {
            'player': self.p2,
            'promote_types': game.get_pawn_promote_types()
        }

        self.assertEqual(expected_move_result, result)

        result = game.promote_pawn('g2', 'g1', Type.QUEEN)
        expected_move_result = MoveResult()
        expected_move_result.update_positions = {'g2': None, 'g1': Queen(Color.BLACK)}
        self.assertEqual(expected_move_result, result)

        # Move white pawn
        result = game.move_piece('a7', 'a8')
        expected_move_result = MoveResult()
        expected_move_result.pawn_promote_info = {
            'player': self.p1,
            'promote_types': game.get_pawn_promote_types()
        }

        self.assertEqual(expected_move_result, result)

        result = game.promote_pawn('a7', 'a8', Type.KNIGHT)
        expected_promote_result = MoveResult()
        expected_promote_result.update_positions = {'a7': None, 'a8': Knight(Color.WHITE)}
        self.assertEqual(expected_promote_result, result)
Exemplo n.º 12
0
    def test_queen_legal_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 = {
            Color.WHITE: {
                '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'
                ]
            },
            Color.BLACK: {
                '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 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] = Queen(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)
Exemplo n.º 13
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)