JAVA Lesson 3 Childhood Memories Rock Paper Scissors

Today we learned how to use programming to create a rock-paper-scissors game that evokes childhood memories. I think all students should have played this game when they were young. When friends play a game together and divide into teams, don’t they always play rock-paper-scissors, and two out of three games win? I was divided into teammates. Thinking about it when I was a kid, I really wanted to laugh.

rock paper scissors game

How to play:

two players

Computer (random) player

1. Player participation: Enter user name.

2. Computer: Provides character selection to start with.

3. Player choice: 1-Scissors 2-Rock 3-Paper

4. The computer randomly selects a number 1, 2, 3

5. Determine right or wrong (to achieve the result of player winning or computer winning)

The code comments are as follows:

import java.util.Scanner;

public class game1_modi2 {
    public static void main(String[] args) {
        //Define global variables, player name player_name, computer name com_name, player punch player_choice, computer punch com_choice
        String player_name;
        String com_name;
        int player_choice;
        int com_choice;
        //Added bug variable, player's punch name player_choice_name Computer punch name com_choice_name
        String player_choice_name;
        String com_choice_name;
        //Write the program according to the process
        //Welcome Screen
        System.out.println("**********************************");
        System.out.println("Game: Rock Paper Scissors");
        System.out.println(" Author:XXXXXX");
        System.out.println("Version: v1.2");
        System.out.println("************************************");
        System.out.println("Welcome to this game, please enter the name of the game player:");
        //Get input device
        Scanner scanner = new Scanner(System.in);
        //Combined with context variables to determine the variables of the current equation, nextLine() game player's name can have spaces.
        player_name=scanner.nextLine();
        //The player may enter a space, whichever result is true is the variable content of player_name
// System.out.println(player_name.equals(""));
        //This variable may also have multiple spaces, including spaces. contains contains spaces, but there are also spaces in the middle. The spaces in the middle are legal.
        //Only the player variable is all spaces, which is illegal. The trim() method removes the spaces at the beginning and end of the string.
// System.out.println(player_name.trim().equals(""));
        if(player_name.trim().equals("")){
            player_name="Anonymous player";
        }
// if(player_name)
        //Select the player of the computer
        System.out.println("************************************");
        System.out.println("1-----ikun");
        System.out.println("2-----jkun");
        System.out.println("************************************");
        //Requires the user to select a player
        String com_no=scanner.next();
        //Replace if with switch.switch structure
        /*
            swtich (variable name){
            case value 1:break;
            case value 2:break;
            default (other):
         */
        switch(com_no){
            case "1":
                com_name="ikun";
                break;
            case "2":
                com_name="jkun";
                break;
            default:
                com_name="Anonymous kun";
        }
// if (com_no.equals("1")){
// com_name="Crayon Shin-chan";
// }else if(com_no.equals("2")){
// com_name="Calabash Baby";
// }else{
// com_name="Anonymous user";
// }
        //Player punches
        System.out.println("****Please choose the representative number of the punch *********");
        System.out.println("1------Scissors");
        System.out.println("2------stone");
        System.out.println("3------Bu");
        System.out.println("************************************");
        //For the convenience of calculation, the player punches using numbers. next() receives a string. If the user enters 1.2
// player_choice=scanner.nextInt();
        //Because the variable player_choice is of type int, an error may be reported when receiving it directly. A string variable is used here.
        String tmp_choice=scanner.next();
        //Change if to switch
        switch(tmp_choice){
            case "1":
                player_choice=Integer.parseInt(tmp_choice);
                break;
            case "2":
                player_choice=Integer.parseInt(tmp_choice);
                break;
            case "3":
                player_choice=Integer.parseInt(tmp_choice);
                break;
            default:
                player_choice=(int)Math.round(Math.random()*2 + 1);

        }
// if(tmp_choice.equals("1") || tmp_choice.equals("2")||tmp_choice.equals("3")){
// //Because different types of String are converted into collation, the uppercase Integer of the integer type is used, and the parseInt method is called, except for char type and int type.
// //The full name of the previous type is uppercase .parse type, such as Integer.parseInt, convert the type of the following variable into the required type
// player_choice=Integer.parseInt(tmp_choice);
// }else{
// //If the user's choice is outside 1,2,3, give one randomly
// player_choice=(int)Math.round(Math.random()*2 + 1);
// }
        //Determine the name of player_choice_name based on the name of player_choice. Here player_choice only has 1, 2, and 3
// if(player_choice==1){
// player_choice_name="Scissors";
// }else if(player_choice==2){
// player_choice_name="stone";
// }else{
// player_choice_name="stone";
// }
        //Change if to switch
        switch(player_choice){
            case 1:
                player_choice_name="Scissors";
                break;
            case 2:
                player_choice_name="stone";
                break;
            default:
                player_choice_name="Bu";

        }
        //When the user punches 1.2, look at player_choice
// System.out.println(player_choice);
        //The computer punches (Math.random() produces 0-1 + 1 1-2 Math.round is rounded (1,2), if Math.random() is expanded by 2 times, 0-2 + 1 1-3
        //Expand Math.random()*2 by 2 times, add 1, and then round, the numbers will become 1, 2, 3
        //Here (int) is forced to convert, and the 8 bytes of the long type are stored in only 4 bytes. 4 bytes are lost, and there are only 1, 2, 3 here.
        com_choice=(int)Math.round(Math.random()*2 + 1);
        //If you use 0,1,2, directly use (int)Math.random()*3
        //Also determine the name of the computer's punch
// if(com_choice==1){
// com_choice_name="Scissors";
// }else if(com_choice==2){
// com_choice_name="stone";
// }else{
// com_choice_name="Bu";
// }
        switch(com_choice){
            case 1:
                com_choice_name="Scissors";
                break;
            case 2:
                com_choice_name="stone";
                break;
            default:
                com_choice_name="Bu";

        }
        //Judge numerical calculation using ==
        if(player_choice-com_choice==1 || player_choice-com_choice==-2){
            System.out.println("Player:" + player_name + ",Punch:" + player_choice_name + "Win");
            System.out.println("Computer:" + com_name + ", Punch:" + com_choice_name + "Loss");
        }else if(com_choice-player_choice==1 || com_choice-player_choice==-2){
            System.out.println("Computer:" + com_name + ", Punch:" + com_choice_name + "Win");
            System.out.println("Player:" + player_name + ",Punch:" + player_choice_name + "Loss");
        }else{
            System.out.println("Computer:" + com_name + ", Punch:" + com_choice_name + "Draw");
            System.out.println("Player:" + player_name + ",Punch:" + player_choice_name + "Draw");
        }
    }
}

Let’s take a look at the running results?

Come and play rock, paper, scissors, let’s get back the fun of childhood!