Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need some Java assistance in making this program: PLEASE USE 0 1 and 2 TO REPR

ID: 3598520 • Letter: I

Question

I need some Java assistance in making this program: PLEASE USE 0 1 and 2 TO REPRESENT SCISSOR, ROCK, AND PAPER RESPECTIVELY

Instructions: Write a NetBeans program that allows users to play the popular scissor-rock-paper game multiple times against the computer. Basic game rule: a scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock. Name the project as RockPaperScissorYourTeamNumber. You can choose the input and output interfaces, names of variables, and ways to represent scissor, rock, and paper for this project, but you must meet the following specific requirements Requirements: 1. Define a method named as playGame outside the main method. This method receives two parameters representing the player's and computer's choices, and it returns an integer representing the game result for the player; specifically, it returns 1 if the player wins and 0 if the player does not win. You must use a switch statement to handle the player's choice. Within each switch case, use if-else statements to determine the game outcome and then display a message indicating whether the player or the computer wins, loses, or draws. Please include a default case for the switch statement. In the main method, write code to let users play the game multiple rounds against the computer 1) In each round of the game (apply to both the for-loop and while-loop below) 2. Let the computer randomly make a choice of scissor, rock, and paper. For example if you use 0, 1, and 2 to represent scissor, rock, and paper, respectively, you should randomly generate a number (of 0, 1, and 2) as the computer's choice Prompt the player to make the player's choice (of 0, 1, and 2) and then capture the player's choice Call the playGame method to play the game once a. b. c. Option 1: use a for-loop to let the player play this game 3 times. For each round of the game, display a message indicating whether the player or the computer wins, loses, or draws. After the player has played 3 times, display a message indicating how many times the player has won. 2)

Explanation / Answer

In my answer, I tried my best to keep up the object oriented coding standards. So I created three classes. Player, Computer and Game. The Player class is the base class for a player of this game, whether it is a human or computer. It has three attributes –name (to store player name), score (to store player score) and a scanner object to get the user input. It has a method called makeMove() which will prompt the player to enter a valid choice 0,1 or 2. The getName() and getScore() methods will fetch the player name and score respectively. Another method incrementScore() is also defined to increase the score of the player by one. The class Computer is derived from class Player ; and the makeMove() method is overridden so that the computer will generate a random valid choice instead of taking it from the user. The Game class consists of 2 Player objects; which can be either human or computer (so that you can also use this to play multiplayer games). One is initialized as a normal player and other one as a Computer as per the requirements. The playGame() method is defined as per the needs; it’ll take two inputs player1’s move and player2’s move; find the result, and returns 1 if player1 wins, 0 if player 2 wins and -1 if no result (when both the players make same choice). Find more info from the included comments. Thanks.

//Game.java file

public class Game {

      static Player player1, player2;

      static int rounds = 3;

      /** maximum rounds in one game */

      static int draws = 0;

      /** games with no results */

      public static void main(String[] args) {

            /**

            * creating a player named Alice

            */

            player1 = new Player("Alice");

            /**

            * creating second player as computer

            */

            player2 = new Computer();

            for (int counter = 0; counter < rounds; counter++) {

                  /**

                  * loops the playGame() method for 3 times (as denoted by rounds

                  * variable)

                  */

                  int res = playGame(player1.makeMove(), player2.makeMove());

                  /** getting the choices */

                  if (res == 1) {

                        player1.incrementScore();

                        System.out.println(player1.getName() + " wins this round");

                  } else if (res == 0) {

                        player2.incrementScore();

                        System.out.println(player2.getName() + " wins this round");

                  } else {

                        System.out.println("its a draw (this round)");

                        draws++;

                  }

            }

            /**

            * exits the loop; displays the statistics

            */

            System.out.println("-------------------");

            System.out.println("Game over");

            if (player1.getScore() > player2.getScore()) {

                  System.out.println(player1.getName() + " wins");

            } else if (player1.getScore() < player2.getScore()) {

                  System.out.println(player2.getName() + " wins");

            } else {

                  System.out.println("The game is drawn");

            }

            System.out.println("Scores");

            System.out.println(player1.getName() + ": " + player1.getScore());

            System.out.println(player2.getName() + ": " + player2.getScore());

            System.out.println("No result: " + draws);

            System.out.println("-------------------");

      }

      public static int playGame(int p1choice, int p2choice) {

            switch (p1choice) {

            case 0:

                  if (p2choice == 0) { /* player1=rock,player2=rock */

                        return -1;

                        /** draw */

                  } else if (p2choice == 1) { /* player1=rock,player2=paper */

                        return 0;

                        /** player2 wins this round */

                  } else if (p2choice == 2) {/* player1=rock,player2=scissor */

                        return 1;

                        /** player1 wins this round */

                  }

                  break;

            case 1:

                  if (p2choice == 1) { /* player1=paper,player2=paper */

                        return -1;

                        /** draw */

                  } else if (p2choice == 0) { /* player1=paper,player2=rock */

                        return 1;

                        /** player1 wins this round */

                  } else if (p2choice == 2) {/* player1=paper,player2=scissor */

                        return 0;

                        /** player2 wins this round */

                  }

                  break;

            case 2:

                  if (p2choice == 2) { /* player1=scissor,player2=scissor */

                        return -1;

                        /** draw */

                  } else if (p2choice == 1) { /* player1=scissor,player2=paper */

                        return 1;

                        /** player1 wins this round */

                  } else if (p2choice == 0) {/* player1=scissor,player2=rock */

                        return 0;

                        /** player2 wins this round */

                  }

                  break;

            default:/**

                  * Invalid input; for the time being,can be considered as a draw

                  * this will return -1 after exiting the switch block

                  */

                  break;

            }

            return -1; /* draw */

      }

}

//Player.java file

import java.util.Scanner;

public class Player {

      String name;

      Scanner scanner;

      int score;

      public Player(String name) {

            this.name=name;

            scanner=new Scanner(System.in);

            score=0;

      }

      public int makeMove(){

            int choice=-1;

            System.out.println("Enter your choice "+getName()+", Rock: 0 Paper: 1 Scissor: 2");

            try{

                  choice=Integer.parseInt(scanner.nextLine());

                  if(choice!=0 && choice !=1 && choice!=2){

                        throw new Exception();

                  }else{

                        System.out.println(getName()+" chose "+choice);

                  }

            }catch (Exception e) {

                  System.out.println("ERROR, Invalid input, please try again");

                  return makeMove();

            }

            return choice;

      }

      public String getName() {

            return name;

      }

      public void setName(String name) {

            this.name = name;

      }

      public int getScore() {

            return score;

      }

      public void incrementScore(){

            score++;

      }

}

//Computer.java file

import java.util.Random;

/**

* extending the class from Player so that it'll have

* all the features of Player class

*/

public class Computer extends Player {

      static String name="computer";

      Random random; /**An object for generating random choices*/

      public Computer() {          

            super(name); /*invoking the base class constructor*/

            random=new Random(); /*initializing Random object*/

           

      }

      @Override

      public int makeMove() {

            /**

            * this method overrides the super class method;

            * generates a value between 0 and 2

            */

            int choice=random.nextInt(3);

            System.out.println(this.getName()+" chose "+choice);

            return choice;

      }

}

//Output

Enter your choice Alice, Rock: 0 Paper: 1 Scissor: 2

0

Alice chose 0

computer chose 2

Alice wins this round

Enter your choice Alice, Rock: 0 Paper: 1 Scissor: 2

1

Alice chose 1

computer chose 0

Alice wins this round

Enter your choice Alice, Rock: 0 Paper: 1 Scissor: 2

6

ERROR, Invalid input, please try again

Enter your choice Alice, Rock: 0 Paper: 1 Scissor: 2

2

Alice chose 2

computer chose 1

Alice wins this round

-------------------

Game over

Alice wins

Scores

Alice: 3

computer: 0

No result: 0

-------------------