Hello JGO-Community,
Currently I’m working my way through the “Introduction to Programming Using Java” by David J. Eck. As i proceed through the chapters I try to implement and use the new learned features in a simple textadventure (Zork-Style). As of now I have a “Area System” that gives information about the area you are in and a working “Dialogue System” that first shows the NPC-Speech and then gives the player multiple choice answers. I use a switch statement for that.
It basically looks like this:
public static void showDialogueNPC(int indexDialogue)
{
/*
* DIALOG 1: "NPC"
* Case 1: IntroductionText
* Case 2: AngryReply
* Case 3: Annoyed
*/
switch(indexDialogue)
{
case 1: // IntroductionText
{
System.out.println("blablabla"); // NPC Talks
System.out.println("");
System.out.println("1. blablabla"); // possible replies
System.out.println("2. blablabla");
System.out.println("> ");
// I use a loop here in the case the player types in 3 for example when there's only 2 possible answers so the game jumps
// back here and he can try again
while(true)
{
playerDialogueReply = stdIn.nextInt(); // Use Scanner to get player's input
if(playerDialogueReply == 1)
{
showDialogueNPC(2); // switch to case 2
break; // stop loop
}
else
{
.... // other replies
}
}
break;
} // end of case 1
case 2:
.
.
.
.
default:
{
System.out.println("DEBUG");
}
} // end of switch
} // end of showDialogueNPC
I know it’s probably (well rather most presumably) not the best way to solve this, but as I said I try to use whatever I learn. Long story short: I have four questions at the moment.
- Would it be reasonable to make a new Class called for example “Speech” and only use it to store the different “NPC-Dialouge lines” in strings? So that I only have to call
System.out.println(Speech.DialogNPC)
for example. The reason why I don’t want to put it to the other variable declarations at the beginning of my Dialogue class is because I have lot’s of text and it kind of overwhelmes my code…
-
At the moment I can only run this in eclipse. Is there a way to run this programm outside of eclipse without providing an actual GUI (for example starting it via console)? I guess this is a really noobish question but I’m not that far in the book and would like to show it to a friend of mine (because he can’t wait to criticise my lousy dialogues and area-descriptions :)).
-
One problem I have with the Scanner is, that if the user types in a char where an int is awaited by the programm it crashes. I guess this is where try… catch would kick in but I can’t quite wrap my head around it yet… Is there any other way to get the user’s input?
-
The last question is more of a general kind as I know how to use subroutines but I kind of don’t know WHEN to use them. Or in other words I have the feeling that I overuse them, trying to put every single bit of my programm in it’s own methods… I hope you understand what I mean. Is there a guideline or a “proper” way?
I haven’t planned too much fancy stuff. As I said I try to implement only new stuff I learn from the book.
Greetings, EatenByAGrue