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.

// 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 againwhile(true) {playerDialogueReply = stdIn.nextInt(); // Use Scanner to get player's input

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.

1. 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

1

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...

2. 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 ).

3. 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?

4. 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.

1. I would do that at least, modularity is a big thing in Object Oriented programming. You could do something as simple as have a class that just holds static references to each NPC's dialog, or you could create a NPC super class and then have your NPC's extend that class to get all of it's talking methods. Something like this:

while(num == -999){System.out.print("Please enter a number: ");try {num = in.nextInt(); }catch(InputMismatchExceptionime) // InputMismatchException is thrown when the wrong type of input is given {System.out.println("That's not a number!\n");// Call in.next() to get rid of the input left behind// Input is left behind because in.nextInt() didn't actually read// the input due to the InputMismatchException being thrownin.next(); }}

System.out.println("The number you entered is: " + num);

4. I tend to prefer to do this but that's just my opinion, generally though you don't want to do it TOO much because then it's just excessive.

// Call in.next() to get rid of the input left behind// Input is left behind because in.nextInt() didn't actually read// the input due to the InputMismatchException being thrownin.next(); }}

Actually, the input is read and when determined that it is not an integer, an exception is thrown. However, when you press ENTER after typing a char/integer, it also includes the \r\n (if on Windows) or \n (if on Unix). in.next() simply clears those remaining chars.

I made some changes according to your suggestions. First of all I created a new class in which I store the NPC-Textlines until I figure out how to read them out of a file (as Bonbon-Chan suggested). Then I made a new class called Reply that handles all the replies for the player, in which the method getPlayerReply() reads the users input after each NPC-Dialog. Here I also included the try - catch statement (the tutorial really helped Z-Man). I didn't even know about the recursive Method problem but I think I have fixed it now - thanks Bonbon-Chan!

At the moment I couldn't use your suggestion about the Super-Class Z-Man, but I think I'll get to that in a few days.

Now it looks like this (I included only one of the "NPCs" - Ignore the awefully written german dialogues ):

EDIT: Removed the code

It still isn't pretty but it works for now. Looking forward to learning more about objects EDIT: I almost forgot to say thank you for the tutorial about the console. I managed to run it from the terminal of my mac... but only if I compiled it first in eclipse. I guess the problem is, that when I compile it using the command line I have to manually care about the packages, right? -> Note to me: Look into that!

You use an amfull number of switch/case. Must off them could be avoid using tables.You use a lot of static where I should not use them. You are still using recursion : getPlayerReply -> hauerPlayerReply -> getPlayerReply -> hauerPlayerReply -> ...

As a template, I should start with something like (I put everything abstract to not put all the code ) :

sorry for the late reply, but to be honest the last days I tried to learn more and more about objects. Thank's to Bonbon-Chan for your script-example - the more I progressed the more I understood about what you are actually doing

I managed to finish a small game to a point where it is playable. Now I would love to include a save function but I don't know how to write and read data from a file. Basically I just need to store all the player related variables in a file and load them... Could someone point me into the right direction (Tutorial)?

the DataInput- /OutputStream was really easy to use , took me about 5 mins to write the load and save

I have a question regarding recursion.If I understood it right, recursion happens when I call a method from within itself, or (as in my dialogue-script) call two methods from each other. So I guess it's also recursive if I make something like this right?

mainMenu() -> startGame() -> startFight() -> exit() -> mainMenu()

But how can I get back to the main menu, after the player exited the current game (by dying, or chosing to leave) without using recursion? I hope I don't annoy people with my questions

Oh no that's wrong. Menu, start, fight etc should be implemented as state machine. Each state has its own "next state" to put it simple. Each state has its own bussiness without bothering others... ok maybe passing some values for use.

Recursion is used to solve problem that has same pattern with different parameter(s), where the parameter is given first and the result is used by next recursion as parameter and so on. For example, finding route in grid based map (A* logic)

function(function(function(function(function(function(function...(function(function(x))))))))) = a result after process x with N times function which is end on some value so the flow climbs up again.

By itselft, recursion is NOT a problem. It can be use for algorithm (like sorting, parsing tree, ...) without make really complicated code. There is technics to "unroll" recursion but it will be a lit too mush to explain that .

When you invoke method within another method, you should ask yourself : is there case, where I will never get to the "return" or the end of the function ?

For example, if I continue to do fights without dying ?

Most of the time, you can avoid problem using a return value and a loop. For example :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

while(!finish){intstate = doDialog();

if (state == DO_FIGHT) { state = doFight(); }

if (state == DIE) { die(); finish = true; }}

It is what ReBirth call using a state machine (well this one is not nice but simple to understand).

To put it simple, use static for class that's not allowed to have multiple instance since all components inside your package only want it alone and same. For example, a class that load all resources for the game.

...is a terrible idea and shouldn't be made static. What happens when you want to go multiplayer and have both a client and server running in the same vm for the host? Or having separate resource pools for out-game and in-game? Or preloading the resources for the next level while still using the current level's resources?

You don't need "only ever one", you need "currently only one". The correct design pattern for that is to just create one object.

You don't need "only ever one", you need "currently only one". The correct design pattern for that is to just create one object.

Here is the problem I ran into. I created a new object of the type "player" in my combat class (since this is were all the magic happens and the player-class is needed the most) but now when I try to change one of it's variables, for example the player's name outside of the combat class I somehow need to access that certain object. So is that the right time and place to make it static (the pointer not the whole player-class I mean)?

Here is the problem I ran into. I created a new object of the type "player" in my combat class (since this is were all the magic happens and the player-class is needed the most) but now when I try to change one of it's variables, for example the player's name outside of the combat class I somehow need to access that certain object. So is that the right time and place to make it static (the pointer not the whole player-class I mean)?

I haven't studied your code in detail, but what I'd suggest is to create a single Player object at a higher level (in your Game or similar class), then pass it in as an argument to the Combat and Menus classes as needed.

The problem is, that the tutorials I read mostly only use two classes to show an example, so when I started writing this "game" I played around with what I already knew and get it to do what I want it to do (that's why I used static reference for example) . As I said it worked ... and now I wanted to try to make it better as I progress. But for example I didn't know that you should create all objects in the main class and then pass them on.

I guess I'm gonna stop doing this for now as I see I still lack the basic understanding of how things work. I'll finish the book I'm currently reading and then try again.

For me, it is more on a philosophy point of view. Static was not mean to be use like that... but a working code is a working code. And if not using a static make thing far more simple (not parse an object through 36 invocations...), I will not be embarased to use it

To EatenByAGrue (you are a french worm ?), you have choosen a good game to start with (aka not MMO guys ). Since it is your first time, you should have to redo your design 5 or 6 times. Since your game is working, you should take what we say as advice and not thinks that you must do. If you see that it will make your code more simple go for it, otherwise continue until you see a problem.

We will always be pleased to help you so don't hesitate to ask question.

It was really frustrating for me, that you guys tried so hard to explain something to me, that I could've learned by myself if I had just read a few more tutorials... That's why I sat down and read a lot about objects (and their usage) the last two days and now started rewriting my game. I think I now at least understand the basics I thought that you had to create an object "where it is needed" ... that's why I wanted to create it in the Combat class in the last version. I now know better

I also removed all the "method1() -> method2() -> method3() -> method1()" invocations (is that the correct word?) and improved some of my methods (for example creating only one method that can roll multiple different dice at once, instead of having multiple methods for each kind of dice like W4, W6 ... and so on).

Okay I finished updating my game. Maybe I should change the title of this thread as it basically isn't a Textadventure anymore but more a "Text-based Hack and Slay" I wanted to add Armor and Weapon Items but I can't motivate myself enough to do so atm. But I have to say I'm quite happy how it is right now. I also think the "balancing" is quite okay. This is the profile of my own char which died after 22 won fights because I wasn't able to find a merchant to buy potions

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org