Solved–Project 2–Solution

$35.00 $24.00

Purpose: This program will provide experience decomposing a problem, writing methods, and more practice using decision statements and loops. You need to write a paper-scissors-rock game that supports one human player and one computer player. To help you understand how the program should work, a sample program run is shown on the next several pages.…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

5/5 – (2 votes)

Purpose: This program will provide experience decomposing a problem, writing methods, and more practice using decision statements and loops.

You need to write a paper-scissors-rock game that supports one human player and one computer player.

To help you understand how the program should work, a sample program run is shown on the next several pages. The basic requirements are as follows:

  1. Ask the user to choose paper, scissors or rock by typing 1, 2, or 3, respectively. If the user enters an invalid number, repeat the request for input. State the item (paper, scissors, or rock) the user has chosen.

  1. Your program should then randomly choose paper, scissors, or rock for the computer player. State the item the computer has chosen.

  1. Determine who won and display the appropriate error message. The rules of paper scissors rock are that paper beats rock, scissors beats paper, and rock beats scissors.

    • If the computer won, output “You have met your doom!”

    • If the human won, output “I have been vanquished!”

    • If both human and computer chose the same object, output “We are equally matched. You are a worthy adversary.”

  1. Keep track of how many times the user has played and their current score. The user should earn one point for a win and lose one point for a loss. A tie does not affect the user’s score. After each round, display the number of times the user has played and their score and ask if they would like to play again. Then react accordingly.

Other requirements:

  • Use methods appropriately to structure your code. Your main method should consist primarily of method calls within a loop.

Generating random numbers:

Use the following formula to generate random numbers.

(int)(lowerBound + Math.random() * (upperBound – lowerBound + 1)

You may use literals in place of the upper and lower bounds. For instance, to display values between 1 and 10 (inclusive on both ends), the formula would look like this:

(int)(1 + Math.random() * 10)

Reading in a single character using a Scanner object:

When asking the user if they want to play again, you can have them enter y for yes and n for no. To read this in to a character variable, use the following statement, where “answer” is the variable used to store the user’s response to the question and myScanner is the Scanner object:

char answer = myScanner.next().charAt(0);

The grading rubric follows on the last page of this document.

Sample program run:

Choose your weapon!

  1. Paper

  1. Scissors

  1. Rock

5

Choose your weapon!

  1. Paper

  1. Scissors

  1. Rock

1

You chose paper!

I choose rock!

I have been vanquished!

We have matched wits 1 times, and your score is 1 Do you want to play again (y or n)? y

Choose your weapon!

  1. Paper

  1. Scissors

  1. Rock

1

You chose paper!

I choose paper!

We are equally matched. You are a worthy adversary.

We have matched wits 2 times, and your score is 1 Do you want to play again (y or n)? y

Choose your weapon!

  1. Paper

  1. Scissors

  1. Rock

2

You chose scissors!

I choose paper!

I have been vanquished!

We have matched wits 3 times, and your score is 2 Do you want to play again (y or n)? y

Choose your weapon!

  1. Paper

  1. Scissors

  1. Rock

3

You chose rock!

I choose rock!

We are equally matched. You are a worthy adversary.

We have matched wits 4 times, and your score is 2 Do you want to play again (y or n)? y

Choose your weapon!

  1. Paper

  1. Scissors

  1. Rock

3

You chose rock!

I choose rock!

We are equally matched. You are a worthy adversary.

We have matched wits 5 times, and your score is 2 Do you want to play again (y or n)? y

Choose your weapon!

  1. Paper

  1. Scissors

  1. Rock

3

You chose rock!

I choose paper!

You have met your doom!

We have matched wits 6 times, and your score is 1 Do you want to play again (y or n)? n

Grading (100 points total):

(20) The program consists of methods that each do a single task.

  1. Allows a user to pick paper, scissors, or rock. Invalid choices have no effect.

  1. Generates a random choice of item for the computer.

  1. Correctly determines who won the round.

  1. Correctly keeps track of the number of times the user has played.

  1. Correctly keeps track of the user’s score.

  1. Output messages match the format provided in the requirements and shown in the sample program

run.

  1. The user can play repeatedly during a single execution of the program by entering ‘y’ when asked if they want to play again and stop playing by entering ‘n’ when asked if they want to play again.

(10) The code is well commented and formatted in accordance with the style guidelines