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
20 points, ~2% of total grade
Due: no later than 11 pm, Thursday, October 16
General Assignment Requirements

Description The purposes of this assignment are:
  1. To practice creating a structured program to solve a problem.
  2. To write more methods that use parameters.
  3. To write an interactive program.
  4. To practice writing programs that have conditional execution.

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:

  • ask the user for their name
  • ask the user how many rounds of Rock, Paper, Scissors they want to play
  • play that many rounds of the game
  • for each round of the game
    • ask the user what their choice is
    • have the computer make a random choice
    • print out each player's choice
    • print the results of that round
  • after playing the specified number of rounds display how time the user won, how many times the computer won, and how many draws occurred
  • declare who the better player was based on the number of wins

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

import java.util.Scanner;

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 nextInt() method from the Scanner class you will have to call the nextLine() method after that to advance the Scanner past the end of line character. The nextInt() method does not advance past the end of line character.

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 nextInt() method from the Scanner class you will have to call the nextLine() method after that to advance the Scanner past the end of line character. The nextInt() method does not advance past the end of line character.

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 Math.random() method.

computerChoice = (int)( Math.random() * 3 ) + 1;

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

(int)( Math.random() * sizeOfRange ) + initialValue

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.

(int)( Math.random() * 3 ) + -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

Computer Choice Human Choice Result
Rock Rock Draw
Rock Paper Human Wins
Rock Scissors Computer Wins
Paper Rock Computer Wins
Paper Paper Draw
Paper Scissors Human Wins
Scissors Rock Human Wins
Scissors Paper Computer Wins
Scissors Scissors Draw

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:

public static final int ROCK = 1;

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
File Responsibility
RockPaperScissors.java (A shell file with a main method and the header information.) Me and you, mostly you.
Checklist Did you remember to:
  • review the general assignment requirements?
  • worked on the assignment with at most one other person?
  • fill in the header in your file RockPaperScissors.java?
  • complete the RockPaperScissors.java so itplays the game correctly?
  • use good names for variables to make your program easy to understand?
  • ensure your program does not suffer a compile error or runtime error (unless the runtime error is caused by bad user input)?
  • turn in your Java source code in files named RockPaperScissors.java to the proper account in the Microlab via the turnin program before 11 pm, Thursday, October 16?
Sample Output User input is underlined and bold.

**** SAMPLE RUN 1 ****

Welcome to Rock Paper Scissors. I, the computer, will be your opponent.
Please type in your name and press return: Isabelle

Welcome Isabelle.
All right Isabelle. How many games would you like to play?
Enter the number of rounds you want to play and press return: 9


Round 1.
Isabelle, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked rock, Isabelle picked rock.

This round is a draw.


Round 2.
Isabelle, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 2
Computer picked rock, Isabelle picked paper.

Paper covers rock. You win.


Round 3.
Isabelle, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 3
Computer picked rock, Isabelle picked scissors.

Rock breaks scissors. I win.


Round 4.
Isabelle, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked paper, Isabelle picked rock.

Paper covers rock. I win.


Round 5.
Isabelle, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 2
Computer picked paper, Isabelle picked paper.

This round is a draw.


Round 6.
Isabelle, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 3
Computer picked paper, Isabelle picked scissors.

Scissors cut paper. You win.


Round 7.
Isabelle, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked scissors, Isabelle picked rock.

Rock breaks scissors. You win.


Round 8.
Isabelle, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 2
Computer picked scissors, Isabelle picked paper.

Scissors cut paper. I win.


Round 9.
Isabelle, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 3
Computer picked scissors, Isabelle picked scissors.

This round is a draw.


We played 9 games of Rock Paper Scissors.
The computer won 3 times.
Isabelle won 3 times.
There were 3 draws.
We are evenly matched at this game.


**** SAMPLE RUN 2 ****

Welcome to Rock Paper Scissors. I, the computer, will be your opponent.
Please type in your name and press return: Olivia

Welcome Olivia.
All right Olivia. How many games would you like to play?
Enter the number of rounds you want to play and press return: 7


Round 1.
Olivia, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked scissors, Olivia picked rock.

Rock breaks scissors. You win.


Round 2.
Olivia, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked rock, Olivia picked rock.

This round is a draw.


Round 3.
Olivia, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked rock, Olivia picked rock.

This round is a draw.


Round 4.
Olivia, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 1
Computer picked scissors, Olivia picked rock.

Rock breaks scissors. You win.


Round 5.
Olivia, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 3
Computer picked paper, Olivia picked scissors.

Scissors cut paper. You win.


Round 6.
Olivia, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 3
Computer picked scissors, Olivia picked scissors.

This round is a draw.


Round 7.
Olivia, please enter your choice for this round.
1 for rock, 2 for paper, and 3 for scissors: 2
Computer picked paper, Olivia picked paper.

This round is a draw.


We played 7 games of Rock Paper Scissors.
The computer won 0 times.
Olivia won 3 times.
There were 4 draws.
You are a master at Rock, Paper, Scissors.
 

 

Back to the CS 305j homepage.