Phase 2 - Commentary

https://inst.eecs.berkeley.edu/~cs61a/su19/proj/hog/#phase-2-commentary

In the second phase, you will implement commentary functions that print remarks about the game after each turn, such as, "22 points! That's the biggest gain yet for Player 1."

A commentary function takes two arguments, Player 0's current score and Player 1's current score. It can print out commentary based on either or both current scores and possibly even previous scores. Since commentary can differ from turn to turn depending on the current point situation in the game, commentary functions return another commentary function to be called on the next turn. The only side effect of a commentary function should be to print.

Commentary examples

The function say_scores in hog.py is an example of a commentary function that simply announces both players' scores. Note that say_scores returns a reference to itself, meaning that the same commentary function will be called each turn.

def say_scores(score0, score1):
    """A commentary function that announces the score for each player."""
    print("Player 0 now has", score0, "and Player 1 now has", score1)
    return say_scores

The function announce_lead_changes is an example of a higher-order function that returns a commentary function that tracks lead changes.

def announce_lead_changes(previous_leader=None):
    """Return a commentary function that announces lead changes.

    >>> f0 = announce_lead_changes()
    >>> f1 = f0(5, 0)
    Player 0 takes the lead by 5
    >>> f2 = f1(5, 12)
    Player 1 takes the lead by 7
    >>> f3 = f2(8, 12)
    >>> f4 = f3(8, 13)
    >>> f5 = f4(15, 13)
    Player 0 takes the lead by 2
    """
    def say(score0, score1):
        if score0 > score1:
            leader = 0
        elif score1 > score0:
            leader = 1
        else:
            leader = None
        if leader != None and leader != previous_leader:
            print('Player', leader, 'takes the lead by', abs(score0 - score1))
        return announce_lead_changes(leader)
    return saypyt

You should also understand the function both, which takes two commentary functions (f and g) and returns a new commentary function. This returned commentary function returns another commentary function which calls the functions returned by calling f and g, in that order.

Problem 6

Update your play function so that a commentary function is called at the end of each turn. The return value of calling a commentary function gives you the commentary function to call on the next turn.

For example, say(score0, score1) should be called at the end of the first turn. Its return value (another commentary function) should be called at the end of the second turn. Each consecutive turn, call the function that was returned by the call to the previous turn's commentary function.

Solution:

Problem 7

Implement the announce_highest function, which is a higher-order function that returns a commentary function. This commentary function announces whenever a particular player gains more points in a turn than ever before. To compute the gain, it must compare the score from last turn to the score from this turn for the player of interest, which is designated by the who argument. This function must also keep track of the highest gain for the player so far.

The way in which announce_highest announces is very specific, and your implementation should match the doctests provided. Don't worry about singular versus plural when announcing point gains; you should simply use "point(s)" for both cases.

Solution:

Last updated