Question: /* * CPS150_Lab14.java */ package cps150_lab14; import java.io.*; import java.util.*; /** * CPS 150, Spring 2017 semester * * Section: 0 * * Lab Project

/*
* CPS150_Lab14.java */ package cps150_lab14;
import java.io.*; import java.util.*;
/** * CPS 150, Spring 2017 semester * * Section: 0 * * Lab Project 14: Rock, Paper, Scissors * * @author */ public class CPS150_Lab14 { // global named constant for random number generator static Random gen = new Random(); // global named constants for game choices static final int ROCK = 1; static final int PAPER = 2; static final int SCISSORS = 3; // global names constants for game outcomes static final int PLAYER1_WINS = 11; static final int PLAYER2_WINS = 12; static final int DRAW = 3; // global named constant for error condition static final int ERROR = -1;
/** * 1. Get human player's choice * 2. Get computer player's (random) choice * 3. Check human player's choice * 4. Check computer player's choice * 5. Announce winner */ public static void main(String[] args) { Scanner scan = new Scanner(System.in); PrintStream output = System.out;
int player1, player2;
// get player 1 input as 1 (rock), 2 (paper or 3 (scissors) output.print("Choose (rock), (paper), or (scissors): "); player1 = scan.nextInt();
// Lab 14.1 // // *** Add code here to validate that player1 has entered // an integer between 1 and 3 // otherwise, ABORT the program if(player1==1||player1==2||player1==3) { // echo human player's choice System.out.print(" You chose "); if (player1 == ROCK) { System.out.println("rock"); } else if (player1 == PAPER) { System.out.println("paper"); } else { System.out.println("scissors"); }
// now computer picks one randomly player2= gen.nextInt(3)+1;
output.println("Now I choose one ...");
/* Lab 14.2 *** Add code to and un-comment the following line so that player2 is set to a random integer between 1 and 3, using the gen Random object, ALREADY DECLARED AS A GLOBAL VARIABLE: */ // player2 = ...; System.out.print(" I choose "); // Lab 14.3 // // *** Add code here to output the computer's choice // as "rock", "paper" or "scissors" if (player2 == ROCK) { System.out.println("rock"); } else if (player2 == PAPER) { System.out.println("paper"); } else { System.out.println("scissors"); }
// Lab 14.4 // // *** Add code below to compare player input against // computer's choice and output results: // // if human player chose ROCK: // call rockChoice method with computer choice // output the game's outcome (returned from rockChoice) // otherwise, if human player chose PAPER: // call paperChoice method with computer choice // output the game's outcome (returned from paperChoice) // otherwise, if human player chose SCISSORS: // call scissorcChoice method with computer choice // output the game's outcome (returned from scissorcChoice) if (player1==ROCK){ int answer = rockChoice(player2); switch(answer){ case PLAYER1_WINS: output.println("PLAYER1 IS WINNER"); break; case PLAYER2_WINS: output.println("PLAYER2 IS WINNER"); break; default: output.println("MATCH DRAWS"); }
} else if (player1==PAPER){ int answer = paperChoice(player2); switch(answer){ case PLAYER1_WINS: output.println("PLAYER1 IS WINNER"); break; case PLAYER2_WINS: output.println("PLAYER2 IS WINNER"); break; default: output.println("MATCH DRAWS"); }
} else { int answer = scissorChoice(player2); switch(answer){ case PLAYER1_WINS: output.println("PLAYER1 IS WINNER"); break; case PLAYER2_WINS: output.println("PLAYER2 IS WINNER"); break; default: output.println("MATCH DRAWS"); }
} } else { System.out.println("WRONG CHOICE. PROGRAM ABORT"); System.exit(0); } } // end main /** * Lab 14.5 * * rockChoice(int) -> int * * method consumes the computer player's choice (ROCK, PAPER or SCISSORS), * assuming the human player has chosen ROCK * method produces game outcome (PLAYER1_WINS, PLAYER2_WINS, or DRAW) * * ex1: rockChoice(ROCK) -> DRAW * ex2: rockChoice(PAPER) -> PLAYER2_WINS * ex3: rockChoice(SCISSORS) -> PLAYER1_WINS * ex4: rockChoice(0) -> ERROR * ex5: rockChoice(-1) -> ERROR * ex6: rockChoice(4) -> ERROR * * *** ADD METHOD CODE HERE *** */ static int rockChoice(int input){ if (input==ROCK) return DRAW; else if (input==PAPER) return PLAYER2_WINS; else return PLAYER1_WINS; } /** * Lab 14.6 * * paperChoice(int) -> int * * method consumes the computer player's choice (ROCK, PAPER or SCISSORS), * assuming the human player has chosen PAPER * method produces game outcome (PLAYER1_WINS, PLAYER2_WINS, or DRAW) * * ex1: paperChoice(ROCK) -> PLAYER1_WINS * ex2: paperChoice(PAPER) -> DRAW * ex3: paperChoice(SCISSORS) -> PLAYER2_WINS * ex4: paperChoice(0) -> ERROR * ex5: paperChoice(-1) -> ERROR * ex6: paperChoice(4) -> ERROR * * *** ADD METHOD CODE HERE *** */ static int paperChoice(int input) { if (input==ROCK) return PLAYER1_WINS; else if (input==PAPER) return DRAW; else return PLAYER2_WINS; } /** * Lab 14.7 * * scissorsChoice(int) -> int * * method consumes the computer player's choice (ROCK, PAPER or SCISSORS), * assuming the human player has chosen SCISSORS * method produces game outcome (PLAYER1_WINS, PLAYER2_WINS, or DRAW) * * ex1: scissorsChoice(ROCK) -> PLAYER2_WINS * ex2: scissorsChoice(PAPER) -> PLAYER1_WINS * ex3: scissorsChoice(SCISSORS) -> DRAW * ex4: scissorsChoice(0) -> ERROR * ex5: scissorsChoice(-1) -> ERROR * ex6: scissorsChoice(4) -> ERROR * * *** ADD METHOD CODE HERE *** */ static int scissorChoice(int input) { if (input==ROCK) return PLAYER2_WINS; else if (input==PAPER) return PLAYER1_WINS; else return DRAW; }
} // end class CPS150_Lab14
* Sample Run (user input ):
run:
Welcome to the game of Rock Paper Scissors Lizard Spock
Here are the rules:
Scissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock
(and as it always has) Rock crushes scissors
Ready? Then let's begin!
Player 1, enter your choice ( rock, paper, scissors, lizard, Spock ): rock
OK, you chose rock
Player 2 (computer) chooses rock
It's a draw
Play again (yeso)? yes
Player 1, enter your choice ( rock, paper, scissors, lizard, Spock ): spock
OK, you chose spock
Player 2 (computer) chooses lizard
Lizard poisons Spock; Player 2 wins
Play again (yeso)? yes
Player 1, enter your choice ( rock, paper, scissors, lizard, Spock ): banana invalid input
Invalid choice "banana"; try again.
Update the Rock, Paper, Scissors program you wrote for Lab Project 14 so that it plays RPSLS. The Rules for RPSLS are described by Dr Sheldon Cooper on The Big Bang Theory (also quoted below), and shown in the diagram on the right. Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Rock Spock, Spock smashes scissors, scissors decapitates lizard, lizard eats paper, Covers crushes paper disproves Spock, Spock vaporizes rock, and as it always has, rock crushes vaporizes crushes scissors. cuts Paper Scissors decapitates RPSLS Features (Updates: disproves eats The user input should be text, not numerical (e.g., "rock" not 1 t should also be case-insensitive smashes (see the Sample Run, below). Suggestion: use the Scanner method nextLine instead of the Scanner method next for user Lizard Spock input. poisons Suggestion: to minimize re-write of your existing code, "translate" the user input into one of five values a good way to do this is to define and call a method static int textToNumber (String choice) After each round, the user is prompted as to whether they want to play again (i.e., yes or no) Suggestion: only play again if the user enters yes, otherwise, end the game Suggestion: use the Scanner method nextLine instead of the Scanner method next for user input. For each round, the user input of choice should be validated as one of the five valid choices; anything else causes the round to be forfeited and prompts the user as to whether they want to play another round. Suggestion: define a method static boolean isvalid(string choice) and call it with the user choice as an if condition You will have to add methods for the two new choices that the user can make int lizard Choice (int computerchoice) int Spockchoice (int computerchoice) As with the original three methods, each of these two new methods returns one of the three named int constants PLAYER1 WINS, PLAYER2 WINS, or DRAW. You will also have to re-define (expand) the code in the original three methods rockchoice, paperchoice, and scissorschoice to check whether the computer chose either of the two new choices (.e., lizard or Spock)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
