The objective of this assignment is to create a program that will play Rock Pape
ID: 3789979 • Letter: T
Question
The objective of this assignment is to create a program that will play Rock Paper Scissors (RPC) for two players (you will play both players). This assignment will test your ability to use loops and arrays. In addition, you will read input and use control statements.
The rules of rock, paper, scissors are simple: each player picks one of the three items. Rock wins over Scissors, Scissors win over Paper, and Paper wins over Rock. If two players enter the same item, it is considered a tie, and they must pick again until a winner is declared. Your program will play 5 games of RPC. At each game, each player will make their selection, and the program will identify the winner of that game. After all 5 games, the program will declare the overall winner (winner of the most games).
Rock Paper Scissors
: You must play 5 games of Rock Paper Scissors.
In each game, identify the game number.
In each game, ask each user for item choice. You need to print the player number, and display the choices.
For each input, check if it is valid. If not, display an error message, and ask for a new value until the value inputted is correct.
Once both players have picked the item, determine who won, or if it is a tie. In case of a tie, ask each of them for a new item.
Once a winner is identified, display which player won.
At the end, display the overall winner. Requirements:
Since the text and the choice of input are almost identical for both player, you will create a function that will do this. As the only thing that varies is the player’s number, so your function will receive that value (1 or 2) as an argument. Your function will return the player’s choice (1, 2 or 3).
You need an array called winner that will remember who won each game (player 1 or 2).
2 Your program needs to play 5 games. You must store this value (5) in a variable. The only other time you are allowed to use the number 5 in your code is when you will declare your winner array. You must use a loop to iterate through the games. You must use a loop to iterate through the array.
Explanation / Answer
import java.util.Scanner;
public class MainDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Player p1 = new Player("koti", 1);
Player p2 = new Player("david", 2);
Game game = null;
int i = 1;
while (i == 1) {
System.out.println("1.playgame");
System.out.println("2.playerscores");
System.out.println("3'quit");
System.out.println("enter your choice");
int choice = scanner.nextInt();
switch (choice) {
case 1:
int count = 1;
while (i <= 5) {
System.out.println("enter game no");
int no = scanner.nextInt();
game = new Game(no);
System.out.println("enter the player 1 choice");
String choose = scanner.next();
p1.setChoice(choose);
System.out.println("enter the player 2 choice");
String choose1 = scanner.next();
p2.setChoice(choose1);
if (choose.hashCode() != choose1.hashCode()) {
if (p1.getChoice().trim().equals("rock")
& p2.getChoice().trim().equals("scissors")) {
game.setPlayer1Score(game.getPlayer1Score() + 1);
}
if (p1.getChoice().trim().equals("scissors")
& p2.getChoice().trim().equals("rpck")) {
game.setPlayer2Score(game.getPlayer2Score() + 1);
}
if (p1.getChoice().trim().equals("scissors")
& p2.getChoice().trim().equals("paper")) {
game.setPlayer1Score(game.getPlayer1Score() + 1);
}
if (p1.getChoice().trim().equals("paper")
& p2.getChoice().trim().equals("scissors")) {
game.setPlayer2Score(game.getPlayer2Score() + 1);
}
if (p1.getChoice().trim().equals("paper")
& p2.getChoice().trim().equals("rock")) {
game.setPlayer1Score(game.getPlayer1Score() + 1);
}
if (p1.getChoice().trim().equals("rock")
& p2.getChoice().trim().equals("paper")) {
game.setPlayer2Score(game.getPlayer2Score() + 1);
}
} else {
String choose2 = "";
String choose3 = "";
System.out.println("enter the player 1 choice");
choose2 = scanner.next();
p1.setChoice(choose);
System.out.println("enter the player 2 choice");
choose3 = scanner.next();
p2.setChoice(choose1);
while (choose2.hashCode() != choose3.hashCode()) {
System.out.println("enter the player 1 choice");
choose2 = scanner.next();
p1.setChoice(choose);
System.out.println("enter the player 2 choice");
choose3 = scanner.next();
p2.setChoice(choose1);
}
if (p1.getChoice().trim().equals("rock")
& p2.getChoice().trim().equals("scissors")) {
game.setPlayer1Score(game.getPlayer1Score() + 1);
}
if (p1.getChoice().trim().equals("scissors")
& p2.getChoice().trim().equals("rpck")) {
game.setPlayer2Score(game.getPlayer2Score() + 1);
}
if (p1.getChoice().trim().equals("scissors")
& p2.getChoice().trim().equals("paper")) {
game.setPlayer1Score(game.getPlayer1Score() + 1);
}
if (p1.getChoice().trim().equals("paper")
& p2.getChoice().trim().equals("scissors")) {
game.setPlayer2Score(game.getPlayer2Score() + 1);
}
if (p1.getChoice().trim().equals("paper")
& p2.getChoice().trim().equals("rock")) {
game.setPlayer1Score(game.getPlayer1Score() + 1);
}
if (p1.getChoice().trim().equals("rock")
& p2.getChoice().trim().equals("paper")) {
game.setPlayer2Score(game.getPlayer2Score() + 1);
}
break;
}
}
case 2:
if (game.getPlayer1Score() > game.getPlayer2Score()) {
System.out.println(p1.toString() + game.getPlayer1Score());
} else
System.out.println(p2.toString() + game.getPlayer2Score());
break;
case 3:
System.out.println("exiting the game");
System.exit(0);
default:
System.out.println("you entered wrong option");
break;
}
}
}
}
*******************************************************************************
public class Player {
protected String playerName;
protected int playerNO;
protected String choice;
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
public Player(String playerName, int playerNO) {
this.playerName = playerName;
this.playerNO = playerNO;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getPlayerNO() {
return playerNO;
}
public void setPlayerNO(int playerNO) {
this.playerNO = playerNO;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + playerNO;
result = prime * result
+ ((playerName == null) ? 0 : playerName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Player other = (Player) obj;
if (playerNO != other.playerNO)
return false;
if (playerName == null) {
if (other.playerName != null)
return false;
} else if (!playerName.equals(other.playerName))
return false;
return true;
}
@Override
public String toString() {
return "Play [playerName=" + playerName + ", playerNO=" + playerNO
+ "]";
}
}
*************************************************************************************
public class Game {
protected static int player1Score;
protected static int player2Score;
protected int gameNo;
public Game(int gameNo) {
this.gameNo = gameNo;
}
@Override
public String toString() {
return "Game [gameNo=" + gameNo + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + gameNo;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Game other = (Game) obj;
if (gameNo != other.gameNo)
return false;
return true;
}
public static int getPlayer1Score() {
return player1Score;
}
public static void setPlayer1Score(int player1Score) {
Game.player1Score = player1Score;
}
public static int getPlayer2Score() {
return player2Score;
}
public static void setPlayer2Score(int player2Score) {
Game.player2Score = player2Score;
}
public int getGameNo() {
return gameNo;
}
public void setGameNo(int gameNo) {
this.gameNo = gameNo;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.