Java interface-oriented – rock paper scissors

Interface development process

  1. Define the interface. The interface defines the method name and does not need to be implemented. All developers must implement the interface for development.

Inheritance is single inheritance, and cascading achieves multiple inheritance.

Interface (which module to develop), multiple inheritance

For methods that are not implemented, you only need to limit the method name and do not need to write a code body.

  1. If you are not sure how to implement some methods, Java provides a keyword, abstract, abstract. If the method is not implemented, define the method as an abstract method.

If a class has an abstract method, it is an abstract class, and the class is also modified by abstract. If the class is abstract, it cannot be instantiated, and the methods in the class cannot be called.

The person who modifies your function inherits the class, and super takes the code of the parent class to optimize your code.

(3) For algorithm classes or tool classes, there is no need to instantiate. Add static to the class to make it a static class. Static classes do not need to be instantiated and can be accessed directly using class names and methods.

The static characteristics ensure the computing speed and are executed first in the memory.

(4) Then consider encapsulation, inheritance and polymorphism based on object-oriented design methods.

(5) The code in each method actually executed is still process-oriented.

import java.util.Scanner;

public class MyMain {
    public static void main(String[] args) {
        MyComputer mycomputer=new MyComputer();
        MyPlayer myplayer=new MyPlayer();
        //According to the game logic, call the method in the class for concatenation.
        //If there are related problems in the call, there is a problem with the method in the class
        //Can display a menu
        System.out.println("---------------------------------");
        System.out.println("Game: Rock, Paper, Scissors");
        System.out.println("Version: v1.0");
        System.out.println(" Author: ZYH");
        System.out.println("---------------------------------");
        //After entering Enter, the game starts
        System.out.println("After entering Enter, the game starts");
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();
        System.out.println("Game starts....");
        //Start logic
        //Define player name
        myplayer.name_settings();
        //Select computers to participate in combatants
        mycomputer.name_settings();
        //Calling the player's punch
        myplayer.choice();
        //Calling the computer's punch
        mycomputer.choice();
        //Call the corresponding referee class to make a ruling. The judgment class here is a tool class. The tool class does not need it. It is similar to the Math class.
        Judge.judge_winner(myplayer,mycomputer);
    }
}
import java.util.Scanner;

public class MyComputer extends Player {
    @Override
    public void choice() {
        //Computer random 1,2,3
        this.setChoice_name((int)(Math.random()*3 + 1));
    }
    //There is another method that has not been rewritten. The participating users have always been anonymous users.
    //Prevent users from participating as anonymous users
    @Override
    public void name_settings() {
        super.name_settings();
        String[] computer_names={"Crayon Shin-chan","Calabash Baby"};
        System.out.println("****************");
        System.out.println("1----Crayon Shin-chan");
        System.out.println("2----Calabash");
        System.out.println("****************");
        System.out.println("Please select the hero number to play against");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Note: You must enter the content and then press Enter. It cannot be a space followed by Enter, otherwise the program will not execute");
        try{
            //Receive the scanner's nextInt first
            int tmp=scanner.nextInt();
            this.setName(computer_names[tmp-1]);
        }catch(Exception e) {

        }
    }
}
/*
Referee
 */
public class Judge {
    //The method to determine winning or losing. The method requires two roles. This role is the parameter of the method.
    public static void judge_winner(MyPlayer player,MyComputer computer){
        //You can use subtraction to determine the outcome
        int sub_player=player.getChoice_name()-computer.getChoice_name();
        //The result obtained by subtraction is sub_player is 1 0 2 -1 -2
        // 0 1 Consider the index of the array. A value of 0 is always a tie. A value of 0 is the first element in the array. A value of 1 is the win in the array.
        //The value is 2 corresponding to the input in the array
        //-1 arrs[-1] 0 -1 -2 There is no problem in applying it to the computer
        String[] arrs= {"Draw","Win","Loss"};
        try{
            System.out.println("player" + arrs[sub_player]);
        }catch(Exception e){
            //Computer application Math.abs absolute value function,
            System.out.println("computer" + arrs[Math.abs(sub_player)]);
        }
        System.out.println("Both sides' cards played");
        String[] player_choices={"scissors","stone","paper"};
        System.out.println("Player punches:" + player_choices[player.getChoice_name()-1]);
        System.out.println("Computer punches:" + player_choices[computer.getChoice_name()-1]);

    }
}
public interface Joiner {
    //Only define the method, no need to implement it
    public void choice();
    public void name_settings();
}
import java.util.Scanner;

public class MyPlayer extends Player{

    @Override
    public void choice() {
        System.out.println("----------------------");
        System.out.println("1-----Scissors");
        System.out.println("2-----Stone");
        System.out.println("3-----Bu");
        System.out.println("----------------------");
        System.out.println("Please select the number representing the punch name:");
        Scanner scanner = new Scanner(System.in);
        //Receive numbers directly here, consider that nextInt may report an error
        System.out.println("Note: You must enter the content and then press Enter. It cannot be a space followed by Enter, otherwise the program will not execute");
        try{
            int tmp=scanner.nextInt();
            this.setChoice_name(tmp);
        }catch(Exception e){
            this.setChoice_name((int)(Math.random()*3 + 1));
        }
    }
    //In order to prevent anonymous users, you also need to rewrite the naming method of name

    @Override
    public void name_settings() {
        super.name_settings();
        System.out.println("Please enter the player name:");
        Scanner scanner = new Scanner(System.in);
        //This name allows spaces, but the space is followed by a carriage return. If it is accepted as empty, it is meaningless.
        //Calling scanner.nextLine() this time to always receive screen input and only input once
        //The variable is only received once here
        String line=scanner.nextLine();
        if (!line.trim().equals("")) {
            this.setName(line);
        }
    }
}
public abstract class Player implements Joiner{
    private String name;
    private int choice_name;
    //2. Make parameterless and parameterized constructions
    public Player() {
    }
    public Player(String name, int choice_name) {
        this.name = name;
        this.choice_name = choice_name;
    }
    //Methods modified by abstract do not need to be implemented
    @Override
    public abstract void choice();
    //The abstract method completes the initialization of name
    @Override
    public void name_settings() {
        this.setName("Anonymous user");
    }
    //1. Do setter and getter methods

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChoice_name() {
        return choice_name;
    }

    public void setChoice_name(int choice_name) {
        this.choice_name = choice_name;
    }
    //3. Rewrite the toString method of the class to print out the content

    @Override
    public String toString() {
        return "Player{" +
                "name='" + name + '\'' +
                ", choice_name=" + choice_name +
                '}';
    }
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java skill treeClasses and interfacesInterfaces 139502 people are learning the system