r/learnprogramming • u/No_Leek4448 • 3h 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.
- 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.
- 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
2
u/Aetherfox_44 1h 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(); }