## HW 3 - Fishing

CS 161 Homework 3
Bart Massey
2011-10-27

It's time to write your first "interesting" game.

This problem requires a computer program: please place the programs in a file with the name "fishing.py".

Submit the assignment writeup to the Moodle as a main text, and attach the file containing the program to the assignment. You may submit the main text either as notes or as a separate attachement. If you submit it as an attachment, please send either a textfile, HTML or PDF: No office document formats please, as they are sometimes hard for us to open and read correctly.

You're going to write a simple fishing game.

• When the user starts up the program, it reads commands from the user and then executes them.

• The available commands must include:

        register <username>
Register a new user named <username>.
numbers, and the characters '-' and '_'.
cast
The current user tries to catch a
fish, as detailed below.
scores
List the user's score, then
the scores of all registered users.
quit
Quit the program. The program should
run the "scores" command on the way out.
help
List the fishing commands and their effects.


If the user "ends input" (input() raises an EOFError) then treat it as a quit command. If the user enters an empty command (blank line) ignore it. Otherwise, give an error for any input that is not a valid command, and allow the user to continue fishing.

• When the "cast" command is issued, the program decides what is caught and how it is scored based on the following table:

FishChanceScore
Nothing20%0
Minnow20%10
Bluegill30%30
Bass15%100
Trout10%200
Salmon4%500
Gold Boot1%5000

The program must respond to the cast command by showing what is caught, how much it is worth, and the user's current score.

• The program must save the scores on disk. Load and save the scores using "shelving" of scores keyed by username. Use a shelf with the filename "fishing". Make sure that the score shelf file is always current.

• Each command should be implemented as a separate function that is called from the main loop.

Your writeup should include test cases that your program should pass. I included a testing command that casts 10000 times and reports the percentages of fish caught; this convinced me my catch algorithm was working.

Some notes on the assignment:

• I've uploaded a sample run from my fishing program here.

• Your "cast" command should refuse to work if there is no current fisher.

• You can keep the score file up to date by using the sync() method of your shelf.

• You will probably want to keep information about the current fisher in global variables. If so, don't forget about the Python global declaration when working inside a function, or you will get confusing bugs.

• You can implement the logic of the "cast" command as a bunch of if statements. It would be better, though, to have a list of tuples (or even objects) representing the fishing table. Each element of the list would comprise a fish name, a fish percentage, and a fish score. To implement "cast" you'd need to generate a random number between 1 and 100 and then walk down the table looking for the corresponding entry. This requires a while loop that is only a little tricky to write.

• I used the string split() method to split the user's command into words for processing.

• You may want to implement the main loop by searching a dictionary of command descriptions as well. I just used a bunch of if statements in my first version, though.

• Don't be afraid to define helper functions beyond what the assignment requires.

Things you might do once you've handed the assignment in: