CS 305J Assignment 6, Conditional Execution
| Programming Assignment 6 | Pair Assignment. You may work with one other person (You
make work with one other person on this assignment using the pair
programming technique.. Read this
paper on pair programming. You may work with anyone in the class. They do not
have to be in the same discussion section as you. One solution will be turned in for the pair. Once you start
working with one partner on an assignment you may not switch partners. If
you do not wish to work with a partner after starting on an assignment you
must both complete it individually. The intent here is you work on the
assignment together, at the same time, at the same computer. Do not simply
try to work on different parts independently and then try to put it
together.) Placed online: Wednesday, October 8 |
||||||||||||||||||||||||||||||
| Description | The purposes of this assignment are:
For this assignment you are limited to the language features in chapters 1 through 4 of the textbook. Write a program to allow a human to play a game of Rock, Paper, Scissors versus the computer. Look at he sample output which shows 2 sample runs from my solution to this problem. This shows you what the output of your program should be. (In the sample data the input from the user was their name, the number of rounds to play, and a choice for rock, paper, or scissors each round.) The program should:
This is not an easy program, mostly due to the size of the program. The individual steps are not too difficult, but their are many steps. The program description above gives you a rough idea of how to break the program up into parts. Approach the problem is steps as we have done in class. Have a high level structure and then implement parts of that structure (the individual methods) one at a time, testing to make sure they work before going on. You may have to write some testing code that will not be part of the final program. Here are some tips on the various parts of the program. 1. main method. In the main program declare a Scanner variable that is hooked up to System.in. You need to include the line of code
at the top of your program. Pass the Scanner object you create as a parameter to any methods that need it. The main method should not have a lot of statements, instead it will be calling other methods. 2. ask the user for their name. This is a good candidate for a separate method that returns a String. 3. ask the user how many rounds of Rock, Paper, Scissors they want to play. This is another good candidate for a separate method that returns an int. You do not have to do any error checking on the user input. If they enter something that is not an int it is appropriate for the program to end due to a runtime error. Recall that if you use the
4. playing the rounds of the game Given our current programming tools this will be the largest and most complex method. It is in turn broken down into several parts. You will need a number of local variables in this method and they should be declared at the top of the method. Obviously you will need a loop to play the appropriate number of rounds. 5. ask the
user what their choice is.
The user will enter an integer as
their choice. You do not need to error check their input. Recall that
if you use the 6. have the
computer make a random choice.
To do this you need the computer
to pick a random number between 1 and 3. The easiest way to do this is to
use the
More generally if you want to pick a random integer in a given range with all elements in the range being equally likely as the result using Math.random the expression would be
So for example if you wanted to have the computer pick a random number between -1 and 1 the size of the range is 3 and the initial value is -1.
7. print out each player's choice. You will find it useful to have a method that is passed an int parameters and returns the correct String for that int. In this program 1 represents "Rock", 2 represents "Paper", and 3 represents "Scissors". 8. print the results of that round. This is the most algorithmically difficult part of the assignment because their are nine possible outcomes and using the programming tools of chapters 1 - 4 it is difficult to remove redundancy. The nine possible outcomes are
You can eliminate some redundancy with these nine outcomes, but not much. 9. after playing the specified number of rounds display how time the user won, how many times the computer won, and how many draws occurred. The method that runs the rounds should call a method to display this information. 10. declare who the better player was based on the number of wins. This can be part of the results method but will again require some conditional execution with if statements. Additional tips. You may find it useful to have class constants for the integers representing rock, paper, and scissors. Recall class constants are declared at the top of the class outside any methods:
Style issues. Not only will we be grading whether your program works, but does it have good style. Did you provide a good structure to the program using static methods? Did you use meaningful identifiers? Did you provide consistent tabbing and spacing for code inside loops and if statements? Did you provide comments for you methods and your code? Extras: If you are looking to do something extra and interesting on this project, consider make modifications to how the computer makes its choice. If the human opponent is truly making random guesses than making random guesses is the best computer strategy. It turns out is very hard for people to make truly random guesses. They fall into tendencies. For example people will often will make a choice different than the one they last made. An interesting approach would be to try a different computer picking algorithm. For example what if the computer looks at a human's last guess and assumes they won't make that guess for this round. How could the computer make its choice to increase its chances of winning using this strategy? When finished turn in your RockPaperScissors.java program using the turnin program. If you are working with another person turn the assignment in to only one person's account, but ensure the header is filled in with both of your names and the unique class ID for your sections. |
||||||||||||||||||||||||||||||
| Files |
|
||||||||||||||||||||||||||||||
| Checklist | Did you remember to:
|
||||||||||||||||||||||||||||||
| Sample Output | User input is underlined and bold.
**** SAMPLE RUN 1 ****
|