-3

Hey, developers I want to ask how to create a Rock, Paper, Scissor simulator ;-)

1 Answers1

-1

This is below algorithm is for the rock, paper, scissior. if you want to develop with Graphical user interface you have to learn about java applets supports only for JDK version 8 or below or swings in java. Step 1: Ask the user to enter in their move.

First, we import the Scanner class to help us get input from the user, by adding import java.util.Scanner to the top of our program. Then, we create our Scanner variable. Next, we print out a message asking the user to type in rock, paper, or scissors using System.out.print(). We store their input in a String called myMove. Step 2: Check if the user entered a valid move.

Use a conditional to check whether myMove is equal to rock, paper, or scissors. If it isn’t equal to rock, paper, or scissors, then we can print out a message to the user stating that their move wasn’t valid!

Step 3: Randomly generate the opponent’s move.

In order to randomly generate the opponent’s move, we can use Math.random(). Math.random() only generates a decimal in between 0 and 1, including 0 and excluding 1. But, since there are 3 possible moves in rock, paper, scissors, we need 3 possible random numbers! How can we do this?

Hint: We can use Math.random() * 3 to generate a random number in between 0 and 3, including 0 and excluding 3. We can then cast this value to an int so that our random numbers only include 0, 1, and 2 – no decimals in between.

Now we need to translate our random number, which can be 0, 1, or 2, into a String opponentMove which is either rock, paper, or scissors.

Hint: Use conditionals to perform the following check: if the random number is 0, then the opponent’s move will be rock; if the random number is 1, then the opponent’s move will be paper; if the random number is 2, then the opponent’s move will be scissors.

Step 4: Use conditionals to figure out if you won, lost, or tied.

Let’s first check if it was a tie. If the opponent’s move was the same as your move, then it is a tie.

Hint: We can use .equals() to check equality between two strings. If myMove is equal to opponentMove, then it is a tie.

We can now go into an else if and check if the user has won. Let’s go through all of the possible situations in which the user wins: the user has rock and the opponent has scissors, the user has scissors and the opponent has paper, or the user has paper and the opponent has rock.

Hints:

We can use && and || to make one giant if statement! Let’s translate what we just said into code: if (myMove equals rock and opponentMove equals scissors) or (myMove equals scissors and opponentMove equals paper) or (myMove equals paper and opponentMove equals rock), then print that the user has won. Else, we can print that the user has lost. Step 5: Use a loop to continue asking the user for their move.

We want to keep asking the user to enter their move. How can we do this?

Hint: We can put all of our code for asking the user for their move, checking if they entered a valid move, randomly generating the opponent’s move, and figuring out whether the user won, tied, or lost in a while(true) loop.

But now our game will run forever! How do we make the while loop stop?

In order for a while(true) loop to stop, we need a break statement in the body of the loop. We can ask the user if they want to quit the game.

Step 6: Check if the user wants to quit.

In our print statement asking the user for their move, we can also ask them to type quit if they want to end the game. Inside our while loop, let’s use an if statement to check whether or not the user entered quit. If the user did type quit, we can exit out of the while loop using a break statement.

source code: import java.util.Scanner;

class Main { public static void main(String[] args) {

    //Initialize the Scanner and print a welcome message
    Scanner in = new Scanner(System.in);        System.out.println("Welcome to Rock, Paper, Scissors!");

    //Use a while(true) loop and only break the loop if the user wants to quit
    while(true) {
    
        //Get the user's move through user input
        System.out.print("What is your move? To make a move, enter rock, paper, or scissors. To quit the game, enter quit. ");
        String myMove = in.nextLine();
        
        //Check if the user wants to quit the game
        if(myMove.equals("quit")) {
            break;
        }

        //Check if the user's move is valid (rock, paper, or scissors)
        if(!myMove.equals("rock") && !myMove.equals("paper") && !myMove.equals("scissors")) {

            System.out.println("Your move isn't valid!");
        
        } else {

            //Get a random number in between 0 and 3 and convert it to an integer so that the possibilities are 0, 1, or 2
            int rand = (int)(Math.random()*3);
            
            //Convert the random number to a string using conditionals and print the opponent's move
            String opponentMove = "";
            if(rand == 0) {
                opponentMove = "rock";
            } else if(rand == 1) {
                opponentMove = "paper";
            } else {
                opponentMove = "scissors";
            }
            System.out.println("Opponent move: " + opponentMove);
                
            //Print the results of the game: tie, lose, win
            if(myMove.equals(opponentMove)) {
                System.out.println("It's a tie!");
            } else if((myMove.equals("rock") && opponentMove.equals("scissors")) || (myMove.equals("scissors") && opponentMove.equals("paper")) || (myMove.equals("paper") && opponentMove.equals("rock"))) {
                System.out.println("You won!");
            } else {
                System.out.println("You lost!");
            }

        }

    }

    //Print a final message for the user
    System.out.println("Thanks for playing Rock, Paper, Scissors!");

} }