Java ~ How Do I Organizing Code Inside A Text Adventure To Make It More Efficient And Easier To Read

218 views Asked by At

I have just recently started on a new project of making a java text adventure game. I have a little bit of code down, but I feel like I'm being messy and inefficient. I feel like my code is everywhere and it is difficult to read even for me. I was wondering if there is a better way of achieving my goal more efficiently and more organized.

Here's my main class:

package dev.sve.game;

import java.util.Arrays;
import java.util.Scanner;

public class main {

@SuppressWarnings("null")
public static void main(String[] args) throws InterruptedException {

    Scanner input = new Scanner(System.in);
    play play = new play();

    String intro = "> You wake up in a different environment. You feel lighter than normal, and for some odd reason, people are staring at you. <";
    String intro2 = "> You slowly start to get up, but one thing that you notice is that there aren't any clouds in the sky. <";
    String intro3 = "> When you stand up, you realize that you only have the clothes on your body... and one flip flop... <";
    String instruction = "- When choices pop up such as [1] [2] [3] etc., enter the desired number and press [Enter] on your keyboard";
    String instruction2 = "- Try not to break the game";

    // String intro
    char[] chars = intro.toCharArray();

    for (int i = 0; i < chars.length; i++) {
        System.out.print(chars[i]);
        Thread.sleep(35);
    }

    // String intro2
    chars = intro2.toCharArray();

    System.out.println(" ");

    for (int i = 0; i < chars.length; i++) {
        System.out.print(chars[i]);
        Thread.sleep(35);
    }

    System.out.println(" ");

    // String intro3
    chars = intro3.toCharArray();

    for (int i = 0; i < chars.length; i++) {

        System.out.print(chars[i]);
        Thread.sleep(35);
    }

    Thread.sleep(1500);

    boolean running = true;

    GAME: while (running) {

        System.out.println("\n\n-------------------------------------------------------------------");
        System.out.println(">                            Main Menu                            <");
        System.out.println("-------------------------------------------------------------------");
        System.out.println("> [1] Play <");
        System.out.println("> [2] Instruction <");
        System.out.println("> [3] Credits <");
        System.out.println("> [4] Quit <\n");
        System.out.print("> ");
        String hold = input.nextLine();

        // Play
        if (hold.equalsIgnoreCase("1")) {

            play.story();

            // Instructions
        } else if (hold.equalsIgnoreCase("2")) {
            chars = instruction.toCharArray();
            System.out.println("\n\n-------------------------------------------------------------------");
            System.out.println(">                          Instructions                          < ");
            System.out.println("-------------------------------------------------------------------");

            // String instruction
            for (int i = 0; i < chars.length; i++) {
                System.out.print(chars[i]);
                Thread.sleep(35);
            }
            System.out.println(" ");
            // String intruction2
            chars = instruction2.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                System.out.print(chars[i]);
                Thread.sleep(35);
            }
            System.out.println(" \n");
            System.out.print("> ");
            hold = input.nextLine();

            // Credits
        } else if (hold.equalsIgnoreCase("3")) {

            // Quit
        } else if (hold.equalsIgnoreCase("4")) {
            break;
        }

    }

}

}

Here's my play class:

package dev.sve.game;

import java.util.Scanner;

public class play {

// Game Objects
Scanner input = new Scanner(System.in);
String hold;
char[] chars;
String story1 = "> You stand up < ";
public String question = "> What Would You Like To Do? <";
first first = new first();
boolean running = true;

public void story() throws InterruptedException {

    while (running) {

        System.out.println("\n\n");
        chars = story1.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
            Thread.sleep(35);
        }

        System.out.println("\n");

        // String question
        chars = question.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
            Thread.sleep(35);
        }
        System.out.println("\n");
        System.out.println("> [1] Ask someone where you are <");
        System.out.println("> [2] Observe your surroundings <");
        System.out.println("> [3] Walk to the nearest building <\n");
        System.out.print("> ");
        hold = input.nextLine();

        if (hold.equalsIgnoreCase("1")) {
            first.firstopt();
        } else if (hold.equalsIgnoreCase("2")) {

        }
    }

}

}

Here's my first class:

package dev.sve.game;

import java.util.Scanner;

public class first {

// Game Objects
Scanner input = new Scanner(System.in);
char chars[];
boolean running = true;

String question = "> What Would You Like To Do? <";
// Strings / Text
String a1 = "> You walk up to the first person that you see... <";
String a2 = "> You: Hey! Would you mind telling me where I am and how I got here? I don't remember a thing. <";
String a3 = "> Person: Uhh... don't you remember the revolution? <\n> You: Revolution? I told you, I don't remember a thing. <\n> Person: Does me saying KFC bring back any memories? <\n> You: Well isn't that like a fast food resturant? But what does that have to do with anything? <\n> Person: I'm gonna have a lot of explaing to do aren't I? <\n> Person: How about we go into this cafe over here to talk? <";
String b1 = "> You: Alright, sound like a plan!<\n> Console: *You enter the cafe* <";
String b2 = "> You: I can't right now, I'm in a hurry. <\n> Person: Don't be silly! You won't survive a day in this atmosphere if you don't know what's going on. <\n> Console: *You enter the cafe anyways* <";
String c1 = "> Console: *You sit down at the table* <";


public void firstopt() throws InterruptedException {

    while (running) {

        System.out.println("\n");

        // String a1
        chars = a1.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
            Thread.sleep(35);
        }
        System.out.println(" ");
        // String a2
        chars = a2.toCharArray();
        // Adds effect for the conversation not to be quick
        Thread.sleep(35);

        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
            Thread.sleep(45);
        }
        // String a3
        chars = a3.toCharArray();
        // Adds effect for the conversation not to be quick
        Thread.sleep(45);
        System.out.println(" ");

        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
            Thread.sleep(45);
        }
        System.out.println("\n");
        // Choices
        chars = question.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
            Thread.sleep(35);
        }

        System.out.println("\n");
        System.out.println("> [1] Sure <");
        System.out.println("> [2] I'm in a hurry right now <\n");
        System.out.print("> ");
        String hold = input.nextLine();

        // If User Input Does Not Meet Requirements
        while (!hold.equalsIgnoreCase("1") && !hold.equalsIgnoreCase("2")) {
            System.out.println("Invalid Command");
            break;
        }
        // First Option
        if (hold.equalsIgnoreCase("1")) {
            System.out.println("\n");
            chars = b1.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                System.out.print(chars[i]);
                Thread.sleep(40);
            }
        }
        // Second Option
        else if (hold.equalsIgnoreCase("2")) {
            System.out.println("\n");
            chars = b2.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                System.out.print(chars[i]);
                Thread.sleep(40);
            }
        }

        System.out.println("\nPRESS [ENTER] TO CONTINUE...\n");
        hold = input.nextLine();

        // String c1
        chars = c1.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
            Thread.sleep(40);
        }
        //String question
        chars = question.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
            Thread.sleep(40);
        }


    }

}

}

Keep in mind I have not finished yet if you see parts that have no outcome.

0

There are 0 answers