示例#1
0
def pawn_promotion_test():
    white_config = {'color': ChessColor.WHITE, 'name': 'Griffin'}
    white_player = Player(white_config)
    black_config = {'color': ChessColor.BLACK, 'name': 'Justin'}
    black_player = Player(black_config)
    piece_strings = ['w Kd2', 'w a7', 'b Kd7', 'b a2']
    g = Game(white_player, black_player, piece_strings)
    print(g.board)
    g.make_move((6, 0), (7, 0))
    g.make_move((1, 0), (0, 0))
    print(g.board)
示例#2
0
    def test_validate_initial_game_state(self, active_pieces_mock):
        """Tests for the private _validate_initial_game_state method."""
        # TODO: find a better way to test this. it's goofing up things like test_make_move
        # since it calls other functions that might be mocked
        active_pieces_mock.return_value = ([], [])

        # should raise if board was not populated
        with self.assertRaises(PiecePlacementException):
            test_game = Game(self.white_player, self.black_player)
示例#3
0
    def test_init(self, set_up_pieces_mock, validate_mock):
        """tests for the constructor."""

        # test behavior when board_config is none
        test_game = Game(self.white_player, self.black_player)
        self.assertIsInstance(test_game.board, StandardBoard)
        self.assertEqual(test_game.white_player, self.white_player)
        self.assertEqual(test_game.black_player, self.black_player)

        self.assertEqual(test_game.is_complete, False)
        self.assertEqual(test_game.is_white_turn, True)

        # make sure populate board is called
        set_up_pieces_mock.assert_called_once()
        validate_mock.assert_called_once()
示例#4
0
def test_input():
    white = Player({'name': 'John', 'color': ChessColor.WHITE})
    black = Player({'name': 'Bob', 'color': ChessColor.BLACK})
    piece_strings = ['w Kf7', 'b Kh8', 'w Qg1']
    game = Game(white, black, piece_strings)
    print(game.board)

    while True:
        cur_color = 'White' if game.is_white_turn else 'Black'
        print("{0}'s turn to move: ".format(cur_color))

        start_coords = None
        end_coords = None
        try:
            start_str = input('piece location: ')
            start_coords = parse_piece_location_string(start_str)
            end_str = input('piece destination: ')
            end_coords = parse_piece_location_string(end_str)
        except ValueError as err:
            print('ERROR: ' + str(err))
            continue
        except KeyboardInterrupt:
            print('\nGame stopped.')
            break
        try:
            game.make_move(start_coords, end_coords)
        except PawnPromotionException:
            piece_str = input('Promote pawn to which piece? ')
            piece_class = parse_piece_type_char(piece_str)
            game.promote_pawn(end_coords, piece_class)
        except Exception as bleh:
            print(bleh)
            continue

        if game.is_complete:
            print('{0} won!!'.format(cur_color))
            break

        print(game.board)
示例#5
0
    def test_make_move(self, move_mock, end_mock, validate_mock):
        """tests function that processes an attempted move of a piece."""
        test_game = Game(self.white_player, self.black_player)
        start_coords, end_coords = ((1, 1), (2, 1))
        start_square = test_game.board.squares[1][1]
        end_square = test_game.board.squares[2][1]
        move_mock.return_value = Move(start_square, end_square)

        # TODO: test adding captured piece to player

        # should use the proper players as cur_player/opponent
        test_game.make_move(start_coords, end_coords)
        move_mock.assert_called_with(start_coords, end_coords,
                                     ChessColor.WHITE)

        test_game.is_white_turn = False
        test_game.make_move(start_coords, end_coords)
        move_mock.assert_called_with(start_coords, end_coords,
                                     ChessColor.BLACK)

        test_game.is_white_turn = True
        # should raise if piece can't be moved
        move_mock.side_effect = InvalidMoveException('dummy exception')
        with self.assertRaises(InvalidMoveException):
            test_game.make_move(start_coords, end_coords)
        move_mock.side_effect = None

        # should raise if a pawn promotion is occuring
        move_mock.return_value = Move(start_square, end_square, None, None,
                                      MoveSideEffect.PAWN_PROMOTION)
        with self.assertRaises(PawnPromotionException):
            test_game.make_move(start_coords, end_coords)
        move_mock.return_value = Move(start_square, end_square)

        # should call end of game method and switch turns
        end_mock.reset_mock()
        test_game.make_move(start_coords, end_coords)
        end_mock.assert_called_once()
        test_game.is_white_turn = False
示例#6
0
    def test_check_for_end_of_game(self, check_mock, checkmate_mock,
                                   stalemate_mock):
        """Tests for the private _check_for_end_of_game method."""
        # TODO: move this to setUpClass (same with most other tests.)
        checkmate_mock.return_value = False
        stalemate_mock.return_value = False
        test_game = Game(self.white_player, self.black_player)

        check_mock.return_value = ['something']

        # make sure game doesn't end if not in checkmate
        test_game._check_for_end_of_game()
        self.assertEqual(test_game.is_complete, False)

        # make sure game ends if in checkmate
        checkmate_mock.return_value = True
        test_game._check_for_end_of_game()
        self.assertEqual(test_game.is_complete, True)

        test_game.is_complete = False
        check_mock.return_value = []

        # make sure game doesn't end if not in stalemate
        test_game._check_for_end_of_game()
        self.assertEqual(test_game.is_complete, False)

        # make sure game ends if in stalemate
        stalemate_mock.return_value = True
        test_game._check_for_end_of_game()
        self.assertEqual(test_game.is_complete, True)
示例#7
0
def main():
    Game().play_game()