Esempio n. 1
0
    def test_knight_capture(self):
        """
        Move knight to a middle square with a piece on a capture square.
        Expected result is position with opponent piece is in legal move list and piece is captured
        when knight moves to that position.
        :return:
        """
        board = ChessBoard()
        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_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 knight to capture a piece
        move_result = board.move_piece(start_position, capture_position)
        message = 'Knight should have captured piece on ' + capture_position + ' square'
        self.assertIsInstance(board[capture_position], Knight, message)

        # Test move result
        expected_move_result = {
            start_position: None,
            capture_position: Knight(Color.WHITE)
        }
        self.assertDictEqual(expected_move_result, move_result,
                             'Expected move result does not match actual')
Esempio n. 2
0
    def test_move_l_shape(self):
        """
        Move knight in every possible direction.
        Expected result is that the knight no longer exist on the starting square, but on the ending square.
        :return:
        """
        start_position = 'd4'
        end_positions = ['b3', 'b5', 'c2', 'c6', 'e2', 'e6', 'f3', 'f5']
        piece_colors = [Color.WHITE, Color.BLACK]

        for color in piece_colors:
            for end_position in end_positions:
                with self.subTest(color=color, end_position=end_position):
                    board = ChessBoard()
                    board[start_position] = Knight(color)

                    move_result = board.move_piece(start_position,
                                                   end_position)
                    self.assertIsNone(
                        board[start_position],
                        'There should no longer be a piece on start position')
                    self.assertIsInstance(
                        board[end_position], Knight,
                        'There should be a piece on end position')

                    expected_result = {
                        start_position: None,
                        end_position: Knight(color)
                    }
                    self.assertDictEqual(
                        expected_result, move_result,
                        'Expected move result does not match actual')
Esempio n. 3
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)
Esempio 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.')
Esempio n. 5
0
 def test_bishop_checkmate(self):
     """
     Test that a queen will put a king of the opposite color in checkmate
     :return:
     """
     board = ChessBoard()
     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')
Esempio n. 6
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')
Esempio n. 7
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)
Esempio n. 8
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')
Esempio n. 9
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)
Esempio n. 10
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()
             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')
Esempio n. 11
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)
Esempio n. 12
0
    def test_knight_legal_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 = {
            Color.WHITE: {
                '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']
            },
            Color.BLACK: {
                '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 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] = Knight(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)
Esempio n. 13
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)