CMSC104, Spring 2010
Programming Project 2
Rock, Paper, Scissors!
Out: Monday, April 12, 2010
Due Date: Monday, April 26, 2010 before 11:59 p.m.
The Objective
This project is designed to give you practice with loops and switch statements. You will also get practice designing and writing your first JavaScript program from scratch.
The Task
The program you write will implement a game called earth-wind-fire-water, based on the the classic game of Rock, Paper, Scissors. If you are not familiar with the game, please read the Wikipedia page about the game. Your program should allow the user to play against the computer. You should keep a running total of the number of player wins, losses, draws (ties) and games played. When the user is finished playing, you should display the statistics for that run of games.
- You should error check the user input to make sure it is either earth, fire, or water. If the user types anything else, you should keep prompting him/her to enter a new value until a correct value is entered. Notice that the words begin with capital letters. The input must match exactly. (Hint: You want to continue the error checking loop while the user choice is not equal to "earth" AND not equal to "fire" AND not equal to "water".)
- For input, the grader and the instructor will only use lower-case letters as input to your program. For example, we will not enter combinations containing mixtures of upper and lower case letters such as: Earth, WATER, firE
- In order to simulate the computer playing, you need to generate a random number. The
formula to do so is:
randomNum = Math.ceil((Math.random() * 3));
The formula will generate a random number in the range 1 - 3, inclusive. You should use a switch statement, or an if/else if structure, to assign the computer's choice according to the following: 1 corresponds to earth, 2 corresponds to fire and 3 corresponds to water. For example:switch(randomNum) { case 1: computerChoice = "earth"; break; etc...
- You should use a do/while loop to allow the user to continue playing games until ready to quit. You should ask the user if they would like to play another game and allow them to answer yes or no. You should error check to make sure the user types either yes or no and continue to prompt the until yes or no is entered. You should only accept "yes" or "no" exactly, no variations (no single letters "y" and "n"; no upper-case letters). The grader will only enter the words yes and no.
- All output displayed to the user should be done with alerts.
- Make sure that when you use the strings "earth", "fire", "water". code you surround them in quotes and, again, strictly in lower-case.
- I used the following sayings but you may choose your own if you'd like:
- "Earth smothers the fire. Earth wins!"
- "Fire boils away the water. Fire wins!"
- "Water erodes the earth. Water wins!"
- Note that messages can contain both upper and lower case letters!
Screenshots
The following are set of screen shots of the game Rock-Paper-Scissors. You should produce similar messages for your game of earth-fire-water when your game is being played. Note the game of Rock-Paper-Scissors uses upper-case letters. You game of earth-fire-water should strictly use all lower-case letters for input. Messages, of course, can contain upper and lower case letters.
Welcome screen
Get ready screen
User entering incorrect input
Error message for incorrect input
Getting the input
Showing the computer's guess
Displaying the results
Asking to play again - yes
Getting another input
Showing the computer's guess
Displaying the results
Asking to play again - no
Displaying the statistics
Submitting the Program
You do not have to do anything to submit the program. It should be in your
pub/www/cs104/proj2 directory.
Your program file name must be: proj2.html located in the above folder.
Extra credit
10 points extra maximum: Add two more items to the game - wind and life. You will then need to consider these combinations:
- Earth blocks the wind. Earth wins!
- Life covers the earth. Life wins!
- Water drowns life. Water wins!
- Wind parts the water. Wind wins!
- Fire consumes life. Fire wins!
- Wind blows out the fire. Wind wins!
- Life sails on the wind. Life wins!
Using these seven combinations along the original three, there will be ten combinations total. Each of the five elements (earth, water, fire, wind, and life) will win in two cases and lose in two cases. Of course, the sayings above are just suggestions and you may replace them with your own.