[2021 Spring] CS61A Project 1: The Game of Hog (Phase 2)

项目说明: https://inst.eecs.berkeley.edu/~cs61a/sp21/proj/hog/#phase-2-commentary
Phase1: https://www.cnblogs.com/ikventure/p/14815119.html
Phase3: https://www.cnblogs.com/ikventure/p/14887555.html

Phase 2: Commentary

Commentary examples


unlock python ok -q 06 -u --local

注意!!!没有逗号

Problem 6


由于每轮都要输出commentary,不适合使用continue,把who = next_player(who)放到if-else语句里了。

def play(strategy0, strategy1, score0=0, score1=0, dice=six_sided,
         goal=GOAL_SCORE, say=silence):
    """Simulate a game and return the final scores of both players, with Player
    0's score first, and Player 1's score second.

    A strategy is a function that takes two total scores as arguments (the
    current player's score, and the opponent's score), and returns a number of
    dice that the current player will roll this turn.

    strategy0:  The strategy function for Player 0, who plays first.
    strategy1:  The strategy function for Player 1, who plays second.
    score0:     Starting score for Player 0
    score1:     Starting score for Player 1
    dice:       A function of zero arguments that simulates a dice roll.
    goal:       The game ends and someone wins when this score is reached.
    say:        The commentary function to call at the end of the first turn.
    """
    who = 0  # Who is about to take a turn, 0 (first) or 1 (second)
    # BEGIN PROBLEM 5
    "*** YOUR CODE HERE ***"
    while score0 < goal and score1 < goal:
        if who == 0:
            score0 += take_turn(strategy0(score0,score1), score1, dice, goal)
            if not more_boar(score0, score1):
                who = next_player(who)
        else:
            score1 += take_turn(strategy1(score1,score0), score0, dice, goal)
            if not more_boar(score1, score0):
                who = next_player(who)
    # END PROBLEM 5
    # (note that the indentation for the problem 6 prompt (***YOUR CODE HERE***) might be misleading)
    # BEGIN PROBLEM 6
        "*** YOUR CODE HERE ***"
        say = say(score0, score1)
    # END PROBLEM 6
    return score0, score1
Problem 7


参考announce_lead_changes函数,注意local变量。

def announce_highest(who, last_score=0, running_high=0):
    """Return a commentary function that announces when WHO's score
    increases by more than ever before in the game.

    NOTE: the following game is not possible under the rules, it's just
    an example for the sake of the doctest

    >>> f0 = announce_highest(1) # Only announce Player 1 score gains
    >>> f1 = f0(12, 0)
    >>> f2 = f1(12, 9)
    Player 1 has reached a new maximum point gain. 9 point(s)!
    >>> f3 = f2(20, 9)
    >>> f4 = f3(20, 30)
    Player 1 has reached a new maximum point gain. 21 point(s)!
    >>> f5 = f4(20, 47) # Player 1 gets 17 points; not enough for a new high
    >>> f6 = f5(21, 47)
    >>> f7 = f6(21, 77)
    Player 1 has reached a new maximum point gain. 30 point(s)!
    """
    assert who == 0 or who == 1, 'The who argument should indicate a player.'
    # BEGIN PROBLEM 7
    "*** YOUR CODE HERE ***"
    def say(score0, score1):
        score = score0 if who == 0 else score1
        running_h = running_high
        if score > last_score:
            running = score - last_score
            if running > running_high:
                running_h = running
                print('Player', who, 'has reached a new maximum point gain.', running_h, 'point(s)!')
        return announce_highest(who, score, running_h)
    return say
    # END PROBLEM 7

Phase2 效果

posted @ 2021-06-15 16:12  ikventure  阅读(3323)  评论(0编辑  收藏  举报