Expected levels of documentation.
- Pay attention to the naming for #defines and variables
- Note the comment style -- the format of comments,
when and where comments are provided.
- The file header comment
Top-Down Design
The purpose of this project was to practice top-down design and
implement that design with functions. Any program that presents a
list of choices almost always has
a switch statement with a case for (almost) each choice, and a function
that processes that choice. That's how this program is designed.
As you read the project description the following tasks can be identified
- print a greeting to the user
- seed the random number genertor
- present the user with a menu
- process the menu selection until the user types 'Q' or 'q'
The last item above can be further refined into
- if the user types 'H' or 'h', print the help menu
- if the user types 'G' or 'g', print the current game statistics
- if the user types 'Q' or 'q', print final stats and exit
- if the user type 'P', 'p', 'R', 'r', 'S', or 's', play the game
The last item above can be further refined into
- generate a choice for the system
- decide if player wins, losses or ties
- update appropriate count of wins, losses or ties
You may have refined your tasks differently, or to a greater extent than
I did.
As a result of my top-down design, I identified these functions
- void PrintGreeting (void) -- displays the greeting on the screen
- void GetSeed ( void ) -- gets random number seed from user and calls srand()
- void PrintHelp ( void ) -- prints the help menu
- int GetPlayerSelection ( void ) -- input the user's menu selection
as a single char and translates it to an integer
- int GetSystemSelection ( void ) -- chooses Rock, Paper or Scissors
for the "system"
- int CompareChoices ( int playerChoice, int systemChoice ) -- compares
the user's and system's choice and returns the result (player wins,
player loses, or tie)
- void ReportOutcome ( int result ) -- displays the result of the last
comparison to the user
- void PrintGameStatus ( int wins, int loses, int ties ) -- displays the
number of wins, losses and ties to the user
- void PrintFinalStatus ( int wins, int loses, int ties ) -- displays
good-bye message and final game statistics to the user
Notes
There a few interesting techniques in
the code that are worth mentioning. If you have
any questions about the code, feel free to ask.
- This code doesn't use the "priming read" technique, even though
a 'while' loop is used. Instead the variable that is the user's input
and controls the while loop
is initialized to INVALID (it could be anything that's not QUIT) so that
the execution will enter the while loop the first time. This is often
easier than the "priming read"
- I translated the single character input into an int because integers
seem easier to work with than chars.
- Should there be a function like GetSystemChoice() which is just
one line of code? I chose to do so because
- a meaningful function name helps make the code easier to read and understand
- Someday the method for generating the system choice may be much more
complex and not be just on line of code
- Someday we may want to generate the system choice in more than one place.
Note that 2 of these reasons have to do with possible future changes to
the code -- that's maintainability.
- The function ReportOutcome() has a default case in its switch statement
which should never be
executed (other functions could have done the same). This is done as
defensive mechanism against myself. During the development of my program,
a coding error may have caused the default case to be executed, alerting
me to my error.
- Note how some functions (like PrintFinalStatus) use other functions
to keep from duplicating code.
- Note the obvious use of #defines for the menu choices.