JAVA INTRO COURSE You will have to write the following file for the PRSGame.java
ID: 3760470 • Letter: J
Question
JAVA INTRO COURSE
You will have to write the following file for the PRSGame.java There will be two private fields score: int p2choice: int Constructor You will to create a constructor that will use the setScore0 to set the score to 0 and set the p2choice to a random number between 1 and 3 setScore0 There will be a setter called setScore0, it wil return nothing, but take in int s. It will check to see if the int s passed to it is equal to or less than 0 and set score to zero, else it will set the score to s. getScorel0 The getter getScore0 will return the field score. checkWinner0 This method will take in a variable int c and compare that to p2choice. First it will use a for loop to count to 3, then it will check the results. Use the scanner.nextLine to take in the pressing of enter. Then compare the two player choices(hint: may be useful to use a switch) This will return a string that will let the player know if they won or lost. If they win, score increases by one, if they lose score decreases by one, if they tie, no change. newChoice0 This will be a public method that resets the computer player to a random choice from 1-3 in p2choice.Explanation / Answer
public class Player
{
private String PlayerName; // Player's Identifying Name
private String State; // Player's Rock/Paper/Scissors State
private int stateValue; // State Value for switch statement
private String output; // Result string of current game winner
private int wins; // number of wins for this player
public Player(String n)
{ // constructor that sets PlayerName identifier and State of Player with P,R or S
PlayerName = n;
stateValue = n.length()%3; // determine state on length of name
switch (stateValue)
{
case 0: State = "Paper"; break;
case 1: State = "Rock"; break;
case 2: State = "Scissors"; break;
default: State = "Rock";
}
}
/* create method that returns the players name*/
public String getName()
{
return PlayerName;
}
public String returnState() // method that returns player's current state
{
changeState(); // change state before returning
return State; // return state string value
}
public void addWin()
{
wins++;
}
public int getWins()
{
return wins;
}
/* create method that returns win total */
public String getOutput()
{
return output;
}
/* create method that returns the current winner output*/
public void changeState()
{
switch (stateValue)
{
case 0: State = "Paper"; break;
case 1: State = "Rock"; break;
case 2: State = "Scissors"; break;
default: State = "Rock";
}
/* add code that increases the state value and then changes the
* Player's State accordingly. Look at the constructor for help and be sure
* the state value never goes above 2!*/
}
public void challenge(Player p1, Player p2) // called when game is played
{
String p2State = p2.returnState(); // get the value of the other player's State
if (State.equals(p2State)) changeState(); // change current player state if same as p2
if (State.equals("Paper") && p2State.equals("Rock"))
{
/* set the output string field to display which player won over
* which other player and what their states were to win*/
System.out.println(p1 + "'s " + State + " has won over " + p2 + "'s " + p2State);
/* call the method of the winner to add a win to their win total */
}
else if (State.equals("Rock") && p2State.equals("Rock"))
{
System.out.println(p1 + "'s " + State + " has tied with " + p2 + "'s " + p2State);
}
else if (State.equals("Scissors") && p2State.equals("Rock"))
{
System.out.println(p2 + "'s " + p2State + " has won over " + p1 + "'s " + State);
}
else if (State.equals("Rock") && p2State.equals("Paper"))
{
System.out.println(p2 + "'s " + p2State + " has won over " + p1 + "'s " + State);
}
else if (State.equals("Paper") && p2State.equals("Paper"))
{
System.out.println(p1 + "'s " + State + " has tied with " + p2 + "'s " + p2State);
}
else if (State.equals("Scissors") && p2State.equals("Paper"))
{
System.out.println(p1 + "'s " + State + " has won over " + p2 + "'s " + p2State);
}
else if (State.equals("Rock") && p2State.equals("Scissors"))
{
System.out.println(p1 + "'s " + State + " has won over " + p2 + "'s " + p2State);
}
else if (State.equals("Paper") && p2State.equals("Scissors"))
{
System.out.println(p1 + "'s " + State + " has won over " + p2 + "'s " + p2State);
}
else if (State.equals("Scissors") && p2State.equals("Scissors"))
{
System.out.println(p1 + "'s " + State + " has tied with " + p2 + "'s " + p2State);
}
// need comparisons for paper to scissors, rock to paper, scissors to paper, etc
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.