This is Java Declare the package rockPaperScissors. (5pts) For this review activ
ID: 3838322 • Letter: T
Question
This is Java
Declare the package rockPaperScissors. (5pts)
For this review activity write a program that simulates Rock, Paper, Scissors. Rules of the game are as follows:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
You are to write a program that plays the game against the computer (10pts).
Allow the user to input their name (5pts).
The computer should choose an object randomly and your program should prompt the user to enter his/her choice. (5pts) OR with a *GUI (10pts)
You are at liberty to have the user enter a number or the string.
Use a decision structure of your choice to determine a winner (15pts).
Allow the user to play a set amount of times (5pts) OR allow the user to determine the number of times they will play (15pts).
The program should then declare the winner, whether user or CPU and how the winner won the game. (5pts) OR with GUI (10pts)
Your program should keep track of the number of wins and losses for each and also the number of ties. (15pts) OR display the results GUI (20pts)
Output their name when giving them the summary of their games (5pts) OR GUI (15pts).
Anything unique after completing all the requirements or a more creative approach (5pts).
*GUI - Graphical User Interface, any one of your choice (JOptionPane)
Simplest Input and Output (you are encouraged to make it more interesting)
Sample Output: Please enter your name.
Sample Input: Marquez
Sample Output: Please choose, Rock, Paper, or Scissors (Explain the game to them here)
Sample Input: Either a 1, 2 or 3 OR the words "Rock", "Paper", "Scissors"
Sample Output: The computer chose Rock and You chose Paper, you win.
Sample Output: Would you like to play again?
Sample Output: Marquez, You won 1 of 1 games.
Explanation / Answer
package rockPaperScissors;
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
static String[] choices = {"Paper","Rock","Scissors"};
static String userName;
public static void main(String[] args) {
Random rand = new Random();
Scanner sc = new Scanner(System.in);
System.out.println("Enter Your Name :");
userName = sc.next();
System.out.println("==========RULES OF GAME==========");
System.out.println(" =>'Rock smashes Scissors'");
System.out.println(" =>'Paper covers Rock'");
System.out.println(" =>'Scissors cuts Paper'");
int totalRound=0;
int youWin=0;
int computerWin=0;
int tie=0;
while(true){
int computerCh = (Math.abs(rand.nextInt())+1)%3;
String cmpChoice = choices[computerCh];
System.out.println("Please choose [1]Rock, [2]Paper, or [3]Scissors :");
String yourCh = sc.next();
String result;
if(yourCh.equalsIgnoreCase("rock")||yourCh.equalsIgnoreCase("Scissors")||yourCh.equalsIgnoreCase("paper")){
result = calculateWiner(yourCh, cmpChoice);
}
else if(yourCh.length()==1 &&
Character.isDigit(yourCh.charAt(0))&&
Integer.parseInt(yourCh.substring(0,1))<4){
yourCh = choices[Integer.parseInt(yourCh.substring(0,1))];
result = calculateWiner(yourCh, cmpChoice);
}
else{
System.out.println("Invalid Choice!");
continue;
}
totalRound++;
if(result.equalsIgnoreCase("YOU")){
youWin++;
//System.out.println(userName+", You won "+youWin+" of "+totalRound+" games.");
}
else if(result.equalsIgnoreCase("COMPUTER")){
computerWin++;
//System.out.println("CPU , You won "+youWin+" of "+totalRound+" games.");
}
else{
tie++;
}
System.out.println("Do you want to play again (Y/N):");
String ch = sc.next();
if(ch.charAt(0)=='Y'||ch.charAt(0)=='y')
continue;
System.out.println(userName+", You won "+youWin+" of "+totalRound+" games.");
System.out.println("Computer won "+computerWin+" of "+totalRound+" games.");
System.out.println("Tie "+tie+" of "+totalRound+" games.");
break;
}
}
public static String calculateWiner(String yourChoice,String computerChoice){
System.out.println("you "+yourChoice+" cpu"+computerChoice);
if(yourChoice.equalsIgnoreCase(computerChoice))return "TIE";
if(yourChoice.equalsIgnoreCase("rock")&&computerChoice.equalsIgnoreCase("Scissors")||
yourChoice.equalsIgnoreCase("paper")&&computerChoice.equalsIgnoreCase("rock")||
yourChoice.equalsIgnoreCase("Scissors")&&computerChoice.equalsIgnoreCase("paper")){
return "YOU";
}
return "COMPUTER";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.