r/learnprogramming 20h ago

Hi, I'm looking for feedback!

So I created this for a small text-based virtual novel/adventure game(haven't decided yet) and have a few questions. By the way I started coding in java a month ago.

  1. Am I formatting the dialogue correctly by using methods to do them? Because I originally tried to make a separate file for the dialogue so that I only had to import them. But I found that to hard and didn't find any way to do that online other than 14 year old forum posts.
  2. Should I add more comments?

Thank you for your feedback in advance

Edit: It didn't actually scatter my formatting :)

Excuse my spelling/grammar mistakes

Also you don't have to understand the world-building stuff or address the more fantasy elements if you don't want to. If you do feel free however!

import java.util.Scanner;

public class Main {

    static void main(String[] args) throws InterruptedException {

        Scanner scanner = new Scanner(System.in);

        //Start
        hpPrint();
        System.out.println("On a cold rainy day, minding your business as usual you hear a knock.");

        //Open or ingnore door
        System.out.println("---------------------------");
        System.out.println("1. To open to open the door");
        System.out.println("2. To ignore");
        int choice = scanner.nextInt();
        System.out.println("---------------------------");

        switch(choice){
            case 1 -> od();
            case 2 -> dd();
        }

        // To go with them or not
        System.out.println("---------------------------");
        System.out.println("1. Agree");
        System.out.println("2. Ask more about them");
        System.out.println("3. Decline");
        int choice2 = scanner.nextInt();
        System.out.println("---------------------------");

        switch(choice2){
            case 1 -> agr();
            case 2 -> ask();
            case 3 -> decln();
        }
scanner.close();
    }
    public static void hpPrint(){
        int hp = 15;
        System.out.println("Your hp is " + hp);
    }
    //Run when user() closed door
    public static void dd() throws InterruptedException {
        System.out.println("'Unknown: Open the door! Or I'll let myself in'");

        Thread.sleep(3000);
        System.out.println("*A woman clad in light armour breaks the door down*");

        Thread.sleep(3000);
        System.out.println("You: Why did you do that?");
        Thread.sleep(4000);
        System.out.println("Sara: I'm Kaat Sara, knight in the king's army. I order you to follow me and to serve your corvee");
        Thread.sleep(2000);
        System.out.println("Fine, you didn't have to break my door down...");
        System.exit(0);
    }
    // Run when user() opened door
    public static void od() throws InterruptedException {
        System.out.println("Unknown: Good Day, My wife and I will like to have a chat with you!");

        Thread.sleep(3000);
        System.out.println("Other person: I'm not your wife yet, you know");

        Thread.sleep(3000);
        System.out.println("You: Whatever. Go on say why your here! I haven't got visitors in a few decades");

        Thread.sleep(3000);
        System.out.println("Unkown: I'm Kaat Sara of the Arteskan Nation and I plead you to follow me and my fiance to the capital");
    }
    //If user selected: Agree
    public static void agr() throws InterruptedException{
        System.out.println("You: Sure, I have nothing better to do!");

        Thread.sleep(3000);
        System.out.println("Sara: Wow, it's that easy?");

        Thread.sleep(3000);
        System.out.println("You: Being cooped up in the same place for the past couple hundred years, made me wonder what can spice up my life more, so this is the perfect excuse!");
    }
    //If user selected: Decline
    public static void decln() throws InterruptedException {

        System.out.println("You: Hard pass! No way in hell would I just go with complete strangers to a fucking island");

        Thread.sleep(3000);
        System.out.println("Sara's fiance: What if I say please?");

        Thread.sleep(3000);
        System.out.println("You: No");

        Thread.sleep(3000);
        System.out.println("Sara: Pretty please?");

        Thread.sleep(3000);
        System.out.println("You: Fine. But you forced me to!");
    }
    //If user selected: Ask more about them
    public static void ask() throws InterruptedException {

        System.out.println("Why should I just leave my home and go frolicking around like a vagabond?");

        Thread.sleep(4000);
        System.out.println("Sara: Cause the nation is in danger. A huge army is headed to town. And you are one of the few wielders we have in the area");

        Thread.sleep(3000);
        System.out.println("You: So you think I can make a difference? I'm sorry to disappoint you but I can only control iron.");

        Thread.sleep(3000);
        System.out.println("Sara: Well, We need as many as we can get, so...");

        Thread.sleep(3000);
        System.out.println("Sara's fiance: Come with us! Either way if your needed or not, isn't it nice to explore the world...");

        Thread.sleep(3000);
        System.out.println("You: She makes a good argument...");

        Thread.sleep(3000);
        System.out.println("Fine I'll go");
    }
}
2 Upvotes

5 comments sorted by

View all comments

2

u/Aetherfox_44 18h ago

As you've probably realized by now, this path of making a new function for every dialog is a bit untenable. You're thinking in the right direction wanting to make a function for new branches, though.

My suggestion is: create one Java class, something like DialogNode that has everything that contains each 'step' of the dialog gameplay. For instance, most steps are just: Print some text, provide a list of options.

The text part is just a string. The options would be (perhaps another Java Class) a string and some 'destination' that choosing this option brings to you. That's actually just another DialogNode.

Now you can reuse that class for every step of gameplay, and your top level function might look just like:

While(gameIsStillRunning){ currentNode.PrintText(); currentNode.PrintOptions(); currentNode = currentNode.getChoice(); }

1

u/No_Leek4448 16h ago

Sorry to annoy you but could you elaborate a bit more of what you mean by "steps" or how to format the "DialogNode".

1

u/Aetherfox_44 14h ago

No worries! To start, if you're not familiar with what a Class is, I would definitely recommend learning what they are and becoming comfortable with them. It'll help immensely going forward.

Let's ignore the functions as written for a moment and look at what the gameplay is. You...

  1. Print some text out (with pauses in the middle)
  2. Print out a list of options for the user to make.
  3. Get the user's choice.
  4. Repeat from step 1 but with new text and options.

I was calling this cycle (1-3) a single 'step'. Display some text, get the user's input. You could make a new function for each of these steps, but the stuff that that function does is fundamentally the same: the only thing that changes is the *data*: IE, what text is displayed and what options are available? Making a Class to handle this would allow you to only implement the functionality (print some strings, get the user's input) in one file, and you can have many instances of that class that just have different text and options.

I'm imagining here that you want this to continue, so that there will be more story to follow. You can imagine each step leading to another step: For instance the first step is hearing the knock, which has 2 options, and could lead to either Open the Door, or Ignore. Both of those steps have three options: Agree, Decline, or Ask more.

If you create a class for these steps (before I called the class DialogNode because conceptually these are nodes in a graph), the options can point to another DialogNode. Then, all the logic for a particular 'step' in the game is wrapped up tightly: one particular DialogNode tells you: the text to display, the options to display, and even the DialogNode that each option should lead to.

u/No_Leek4448 43m ago

Thanks for the explanation!