Story Adventure

Putting it all together!

Now that we have all the game obects we actually need a way to play! We start the game in the main function. We should probably display an intro of some sort to provide context as to what the game is about. Feel free to be creative and make your own story.

            
                public static void intro() {
                    System.out.println("Last night, you went to sleep in your own home.");
                    System.out.println("Now, you wake up to an unfamiliar environment.");
                    System.out.println("As your eyes flutter open, darkness greets you. Your surroundings are blind to you.");
                    System.out.println("You are sat on a bed, and a wall hugs your back. Beside you is a table, whose outline you can vaguely determine.");
                    System.out.println("You check the table and find a lamp. With a loud click, your surroundings are illuminated.");
                    System.out.println("You also check under the bed and find a note.");
                    System.out.println("");
                    System.out.println("The note reads:");
                    System.out.println("My dearest participant,");
                    System.out.println("    If you notice, the door contains a padlock with a three digit code.");
                    System.out.println("    You will find this code hidden within the room, and must solve it. You shall find three numbers, and must order them correctly.");
                    System.out.println("    I wish you the best of luck.");
                    System.out.println("Sincerely, your gracious host.");
                    System.out.println("");
                    System.out.println("You examine the room and see:");
                }
            
        

Next we should have a way of showing the player what is in the room. We need to show all of the game interactable objects. For this a menu function is handy.

A menu function should go through a list of items in the room and collect input so we know which item the player intends to interact with. Using the getNumber function and the following template you can write a menu.

            
                public static void menu(String item_list[]) {
                    // Loop through items in item_list and print them to the console
                    // Collect a number corresponding to the item in the list
                    // Return that number
                }
            
        

Now in our main method we should write the game loop, but we need to keep track of a few variables first. We need a way to know if our game should run. We need a list of objects in the room. We need to know what choice the player has made, and we need to know the code and the code locations.

This can look something like the following.

            
                public static void main(String[] args) {

                    // The list of game objects in our room
                    String choices[] = {"Door", "Backpack", "Dresser"};

                    // The code we are searching for
                    int code[] = {1, 2, 3};

                    // The location of the game object which part of the code is found in
                    int code_location[] = {2, 4, 1};

                    // A way of knowing if we found the code
                    boolean found_code = false; 

                    // The choice the player makes
                    int choice = -1;
                }
            
        

Now it's just a matter of putting it all together. We need to make a choice and search an object in the room unill we are ready to guess the code.

The final loop should could look like this.

            
                while (!found_code) {
                    choice = menu(choices);
        
                    switch (choice) {
                        case 1:
                            found_code = door(code);
                            break;
                        case 2:
                            backpack(choice, code_location[0], code[0]);
                            break;
                        case 3:
                            dresser(choice, code_location[1], code[1]);
                            break;
                    }
                }
            
        

The rest of the game is up to you! You should probably add a message after the player guesses the code. You can even add another level. The game will play better with more objects in the room, and you can make those objects more interactable if you want. A final thing to add is randomness. Randomly generate a code and code locations using java.util.Random and random.nextInt.