“This is the 20th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

❤️ Author’s home page: Xiao Xu Zhu

❤️ About the author: Hello everyone, I am Xiao Xu Zhu. Java field quality creator 🏆, CSDN blog expert 🏆, Huawei cloud enjoy expert 🏆, Nuggets of the year popular author 🏆, Ali Cloud expert blogger 🏆

❤️ technology live, the appreciation

❤️ like 👍 collect ⭐ look again, form a habit

preface

Game background

The Hundred changes villa is located in the Pacific Ocean on a small island, the villa owner is also the master of the island. The mysterious host was meticulous and thoughtful. The invitation letter included not only the air ticket to and from the port, but also the special line to the island by the port mail ship. First landing on the island, the grand atmosphere of the villa straight into the eye, through the porch, resplendent hall shocking people. The invited detectives begin to arrive, the waiter stands politely aside, and he arranges for you to sit around a strange ten-sided table and wait a little. The top five detectives in the world took up positions on either side of the table, exchanging small talk about their recent discoveries. “Detectives, welcome to the Heights of change.” An old, heavy voice sounded and the lights went out. At the same time, a strange figure was projected on the milky wall. “Only the best detectives can solve the mystery of the Mountain of Changes, and I will not disappoint you. I hope you can stand my tests, too. Details of the mystery will be presented after dinner. Enjoy your meal, distinguished detectives!” The picture disappears, the lights come back on, and [waiter] pushes the cart slowly towards us. The mystery owner was not present at the scene. [Detective Royal sister] very dissatisfied with this, she took [detective’s hand] to the bathroom, the two exquisite beautiful detectives to repair makeup before dinner. [Waiter] held up a tray of wine and motioned for everyone to help themselves. You look at the wine cup in front of you, the owner of the villa has a persistent pursuit of art, even the wine cup is also ten sided, speaking of the unusual place, this wine cup has a cup ear. As soon as the two beautiful detectives took their glasses and sat down, the well-respected detective [easy chair] made a toast, and everyone drank it. Young detective [red scarf] trying to and [royal sister] detective cup was refused, suddenly [detective palm] frowning spit out the taste of wine is not right, side of the [waiter] hurriedly passed the handkerchief wipe. ‘To put us off with this cheap table wine! It’s so…” Suddenly his hands were around his neck and he fell to the ground. When… . When… When…… An antique clock in one corner of the hall struck eight. The young detective [Red Scarf] examined the body of [Detective hand] the first time and announced her death to everyone. Has the challenge already begun? [Detective fingertips] thought to himself. The detectives were in a panic, only to be told by [the waiter] that they could not leave the premises. There was no signal on the island, and the cruise ship to pick us up had left. An interesting host, [Detective Fingertips] smiled.

“Role-playing detective” is a homemade detective word game, in the JAVA language.

The main requirements

Using some basic JAVA syntax and some logical judgment, string the story together, go ahead, find the killer.

The main design

1. Obtain the corresponding identity of the player through random number

2, design the plot background, write a good script

3. Players can interact with the story by entering specified commands on the console. This can be done in code:

Scanner sc = new Scanner(System.in)
    // Gets the character input from the console
    String input = sc.next();
Copy the code

4, through logical judgment, you can flow in different story flow.

Screenshot function

Acquire player identity randomly

Character is introduced

The story background

Your task

To explore the scene

So, did you find the killer

Code implementation

Game main interface


/* Here is the game executive detective */
public class MainGame extends Story {
    public static void main(String[] args) {
        // Realize guess numbers small game, through the random x number below the number, get your corresponding identity
        Random r = new Random();
        int id = r.nextInt(5) + 1;

        inChoice(id);
        // switchChoice(id);

        sleep(1000);
        Scanner sc = new Scanner(System.in);
        System.out.println("Now learn more about who the others are! (Please enter "yes" or "no")");
        String input = sc.next();
        String yes = "yes";
        if(! yes.equals(input)) { System.out.println("Are you sure you don't want to know who the others are?");
            System.out.println("These clues can help you find the answer faster!");
            System.out.println("Then to the next step! (Please enter "yes")");
        } else {
            character();
            System.out.println("Then to the reasoning of the case! (Please enter "yes")");
        }
        String input2 = sc.next();
        if (yes.equals(input2)) {
            Antecedent();
        }

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


        // Start the game
        Game game = new Game();
        Scanner in = new Scanner(System.in);
        while (true) {
            String line = in.nextLine();
            String[] command = line.split("");
            if (command[0].equals("help")) {
                game.printHelp();
            } else if (command[0].equals("go")) {
                game.goRoom(command[1]);
            } else if (command[0].equals("bye")) {
                System.out.println("Now for the closing vote, please choose who you think is the killer:" +
                        1. Easy chair 2. Detective Royal 3. Detective Red scarf 4. 6. Waiter (enter the serial number)");
                Scanner sca = new Scanner(System.in);
                int ordinal = sca.nextInt();
                Story.Truth(ordinal);
                System.out.println();
                System.out.println("Murder solved. You're great. Welcome to the next one.");
                System.exit(0);
                break; }}}public static void misson(a) {
        System.out.println("Your Mission \n" +
                "1. Find the real killer; \n" +
                "2. Infer where the detective's fingertips are on the world's detective list.");
    }

    public static void inChoice(int id) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the number you guessed (an integer between 1-6, which will correspond to your identity) :");
        while (true) {
            int input = sc.nextInt();
            if (input > id) {
                System.out.println("The data you guessed" + input + "Big");
            } else if (input < id) {
                System.out.println("The data you guessed" + input + "Small");
            } else {
                switchChoice(id);
                break; }}}public static void switchChoice(int id) {
        System.out.print("Now proceed... Randomly assigned, your identity is:");
        sleep(1000);
        switch (id) {
            case 1:
                System.out.println("Easy Chair Detective.");
                break;
            case 2:
                System.out.println(Detective Royal);
                break;
            case 3:
                System.out.println("Detective Red Scarf.");
                break;
            case 4:
                System.out.println("Phone Booth Detective");
                break;
            case 5:
                System.out.println("Detective Fingertips.");
                break;
            case 6:
                System.out.println("Waiter");
                break; }}public static void sleep(int mills) {
        try {
            Thread.sleep(mills);
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

The master bedroom

public class BedRoom extends Room {

    public BedRoom(a) {
        super("The Room at the Fingertips.");
    }

    @Override
    public void narration(a) {
        System.out.println("Detective notes at my fingertips.");
        System.out.println("Left-handed" A person who is left-handed.); }}Copy the code

Initialize the


public class Initialization {
    // Initialize the current room
    Room currentRoom;

    // Displays the welcome message
    public void welcome(a) {
        System.out.println("Type 'go+ room' to go to the room you want to go to start search, such as' go main_bed '");
        System.out.println("If you want to end the game and vote please type" bye "");
        System.out.println("Please remember to type" help "if you need help.");
    }


    // Set all rooms
    public void setRoom(a) {
        // Initialize all rooms
        Room bedroom = new BedRoom();
        Room lobby = new Lobby();
        Room secBed = new secBedroom();
        Room thBed = new thBedroom();


        // The lobby is initialized
        lobby.setExit("thr_bed", thBed);
        lobby.setExit("sec_bed", secBed);
        lobby.setExit("main_bed", bedroom);

        // Initialize the fingertip
        bedroom.setExit("sec_bed", secBed);
        bedroom.setExit("thr_bed", thBed);

        // The server initializes
        secBed.setExit("thr_bed", thBed);
        secBed.setExit("main_bed", bedroom);

        // The initialization of my sister
        thBed.setExit("sec_bed", secBed);
        thBed.setExit("main_bed", bedroom);


        // Set the starting room
        currentRoom = lobby;
    }

    // Get the current room
    public Room getCurrentRoom(a) {
        returncurrentRoom; }}Copy the code

The hall

public class Lobby extends Room {
    public Lobby(a) {
        super("Hall");
    }

    @Override
    public void narration(a) {
        System.out.println("Here are the cups of detective Red Scarf (1) and detective easy chair (2), please enter the cups you want to view.");
        Scanner sc = new Scanner(System.in);
        String cup = sc.next();
        String hCup = "1";// The red scarf detective's cup
        String ACup = "2";// Easy chair detective's cup
        if (cup.equals(hCup)) {
            System.out.println(You notice detective Red Scarf acting strangely while drinking and decide to check his glass. He had poison on one side of his cup, and everyone had poison on the other! \n" +
                    "You remember that detective Red Scarf and the dead man were left-handed!");
        } else if (cup.equals(ACup)) {
            System.out.println(You notice the easy chair detective's peculiar look as he receives the drink and decide to examine his glass, which is ten sides! Others have the same decagon shape as the table. \n ");
        } else {
            System.out.println("Your input is wrong. Please enter 1 or 2."); }}}Copy the code

conclusion

Through this “role-playing detective” game implementation, let me have a further understanding of JAVA related knowledge, the Language of JAVA also have a deeper understanding than before.

Basic Java syntax, such as data types, operators, program flow control, and arrays, is better understood. Java is the core of the core object oriented thought, for this concept, finally realized some.

The source code for

After clicking “like” and following the blogger, the private blogger will get it for free