Problem 7 (400pts)

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. For example, announce_highest(1) ignores Player 0 entirely and just prints information about Player 1. (So does its return value; another commentary function about only Player 1.)

To compute the gain, it must compare the score from last turn (last_score) to the score from this turn for the player of interest (designated by the who argument). This function must also keep track of the highest gain for the player so far, which is stored as running_high.

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.

Hint: The announce_lead_changes function provided to you is an example of how to keep track of information using commentary functions. If you are stuck, first make sure you understand how announce_lead_changes works.

Hint: If you're getting a local variable [var] reference before assignment error:

This happens because in Python, you aren't normally allowed to modify variables defined in parent frames. Instead of reassigning [var], the interpreter thinks you're trying to define a new variable within the current frame. We'll learn about how to work around this in a future lecture, but it is not required for this problem.

To fix this, you have two options:

  1. Rather than reassigning [var] to its new value, create a new variable to hold that new value. Use that new variable in future calculations.
  2. For this problem specifically, avoid this issue entirely by not using assignment statements at all. Instead, pass new values in as arguments to a call to announce_highest.

Before writing any code, unlock the tests to verify your understanding of the question.

$ python ok -q 07 -u

Once you are done unlocking, begin implementing your solution. You can check your correctness with:

$ python ok -q 07

When you are done, you will see commentary in the GUI:

$ python hog_gui.py

The commentary in the GUI is generated by passing the following function as the say argument to play.

both(announce_highest(0), both(announce_highest(1), announce_lead_changes()))

Great work! You just finished Phase 2 of the project!