예제 #1
0
파일: puzzler.py 프로젝트: z0k/CSC108
def play_game(puzzle, view, game_type):
    """(str, str, str) -> (bool, int)

    Prompt the player(s) to try to guess the puzzle. game_type is one of
    pf.HUMAN, pf.HUMAN_HUMAN, or pf.HUMAN_COMPUTER.
    Return whether there was a winner and the final score.
    """

    player_one_score = 0
    player_two_score = 0
    current_player = pf.PLAYER_ONE

    # The player move choice; will be one of pf.CONSONANT, pf.VOWEL,
    # pf.SOLVE and pf.QUIT.
    move_type = ''

    # The letters that have not yet been guessed.
    unguessed_consonants = pf.CONSONANTS
    unguessed_vowels = pf.VOWELS

    # This is None if there is no computer player.
    difficulty = select_computer_difficulty(game_type)

    # Note: you may find it helpful to display the solution while you
    # are testing. To do this, uncomment the following line:
    #print('Solution: {0}'.format(puzzle))

    while not pf.game_over(puzzle, view, move_type):
        quantity = 0
        if current_player == pf.PLAYER_ONE:
            score = player_one_score
        else:
            score = player_two_score

        # Set up function aliases based on whether it's a human's turn or a
        # computer's turn.
        if is_human(current_player, game_type):
            get_move = get_player_move
            get_guess = get_player_guess
            get_letter = get_player_letter
        else:
            get_move = get_computer_move
            get_guess = get_computer_guess
            get_letter = get_computer_letter

        move_type = get_move(current_player, view, difficulty,
            score, unguessed_consonants, unguessed_vowels)

        if move_type == pf.SOLVE:
            guess = get_guess(view)

            if guess == puzzle:
                newview = puzzle
                score = pf.finalize_score(puzzle, view,
                    unguessed_consonants, score)
                view = newview
            else:
                print("The guess '{0}' is Incorrect :-(".format(guess))

        elif move_type == pf.CONSONANT or move_type == pf.VOWEL:

            letter = get_letter(
                unguessed_consonants, unguessed_vowels, move_type, difficulty)

            quantity = puzzle.count(letter)
            view = pf.update_view(puzzle, view, letter)
            score = pf.calculate_score(score, quantity, move_type)
            unguessed_consonants, unguessed_vowels = pf.make_guessed(
                unguessed_consonants, unguessed_vowels, letter)

            print('The guess was {0}, which occurs {1} time(s).  '\
                  .format(letter,quantity), end='')
            print('Your score is {0}.'.format(score))
            

            player_one_score, player_two_score = pf.update_score(
                              player_one_score, player_two_score,
                              score, current_player)

        if game_type != pf.HUMAN:
            current_player = pf.next_player(current_player, quantity)

    # The game is over.
    display_outcome(pf.is_win(view, puzzle), score)
예제 #2
0
# Type check pf.get_view
result = pf.get_view('happy')
assert isinstance(result, str), \
       """pf.get_view should return a str, but returned {0}.""" \
       .format(type(result))


# Type check pf.update_view
result = pf.update_view('apple', 'a^^l^', 'e')
assert isinstance(result, str), \
       """pf.update_view should return a str, but returned {0}.""" \
       .format(type(result))


# Type check pf.make_guessed
result = pf.make_guessed('jklmn', 'aeo', 'm')
assert isinstance(result, tuple) \
       and isinstance(result[0], str) \
       and isinstance(result[1], str), \
       """pf.make_guessed should return a tuple of (str, str),
but returned {0}.""" \
       .format(type(result))


# Type check pf.calculate_score
result = pf.calculate_score(4, 2, pf.CONSONANT)
assert isinstance(result, int), \
       """pf.calculate_score should return an int, but returned {0}.""" \
       .format(type(result))

# Type check pf.finalize_score