CS21 Spring 2005
Due Sunday, Feb. 6, before 11:30pm
Answer the following Review Questions from the C book on your own,
but do not turn them in. We will post answers on the CS21 web page
- Chapt. 5 Review Questions 2, 5, 6-10, 14 (page 178)
Please complete the following 3 programs and turn them in. The main task
of this week's homework is to create a program that plays the game of
Rock-Paper-Scissors. This is a fairly simple game that can be implemented
with less than 10 functions. However, to help you get in the habit of
testing your functions as you code them in, we want you to write this
program in pieces. Program 1 asks you to write a menu function and
a GetUserChoice function (and a main function that tests them both).
Program 2 asks you to add a DetermineWinner function. Finally, Program 3
asks you to put the whole game together.
We will only grade Program 3. However, you should first implement Program 1,
then Program 2, and finally Program 3. By following this approach for future,
more complex programs, you will save yourself many hours of debugging time.
- Program 1, Implement a function, DisplayMenu, to display a
menu of the following options to a user:
Next, implement a function, GetUserChoice, that takes as input low and high
menu range values (1 and 3 representing the range of the three possible
choices, Rock, Paper and Scissors) and returns the users choice, one of Rock,
Paper, or Scissors, that is encoded as an int value 1, 2, or 3 (use
constants for these three values). GetUserChoice prompts the user to
enter a menu option, reads in the value, and checks to see that the
selection is within the range between low and high. If the value is
within range, GetUserChoice returns the value to the caller, if it is not
in range, then it prints a message stating that the user selected an
invalid value and prompts the user to try again (to enter a valid value).
GetUserChoice will repeat prompting the user to enter a value until the user
enters an in-range value. Your main function should call DisplayMenu to
display the menu, then call GetUserChoice to get the user's selection,
and finally print out a message stating which value the user has selected:
"You have selected (Rock, Paper, or Scissors)"
Remember to have comments at the top of your program file describing what the
program does, to comment every function with what it does, the values it
takes in and the value it returns, and to use good function and variable names.
Also, think about how you can use constants in your program.
- Program 2 Starting with the code you wrote for Program 1 (copy
your program1.c file to a program2.c file using the cp command), add a
function, DetermineWinner, that determines who is the winner of the rock, paper,
scissors game (either the user or the computer). Your function will take two
input values (the user's choice and the computer's choice), and will return a
value that indicates who won the game (0 for user, 1 for computer, 2 for tie).
To test your DetermineWinner function, after your program reads in the selection
from the user, it should call your DetermineWinner function three times from
your main function (one time for each possible computer selection). After
each call to DetermineWinner, print the value it returns (one of 0, 1, or 2).
The rules of the rock, paper, scissors game are the following:
- Rock beats Scissors
- Scissors beats Paper
- Paper beats Rock
- Two of the same are a tie
- Program 3 Putting it all together. Start
by copying your program2.c file to a program3.c file. You will finish
the implementation of a program that plays the rock, paper, scissors game.
Start by implementing the a function, GetYesOrNo, that prints
a prompt asking the user "do you want to play again (enter 1 for yes/0 for no)",
then makes a call to your GetUserChoice function to read in and return the user's
choice. Next, add a loop around the code in your main function
(print menu, get response, determine winner, print results, see if user wants
to play again) that will repeat until the user enters 0 for playing again.
The computer will play the game based on the Hannah Rule (invented by Jeff Knerr's
4 year old daughter): for the next round of play, the computer will select the
value that would beat what the user selected in the current round.
For example, if the user selects Rock in round i, then the computer will select
Paper in round i+1. You should pick one of Rock, Paper, or Scissors as the
computer's selection for the first round of play (e.g. every time you run
your program the computer will "pick" Rock as its choice for the first
game). You do not need to have the computer choose randomly among the
To get an idea of what a complete Program3 looks like when run,
HERE is sample output from a run of
mine. Yours does not need to be identical, but it should have all the
required parts described above.
If, as you implement your solution to this program, you find that there
are places where it makes sense to add more functions to your program, feel
free to add functions beyond the ones we have specified.
Please use the cs21handin command to turn in your homework.