aspose file tools*
The moose likes Beginning Java and the fly likes [HELP]Trying to make a decision cmd adventure! How to branch decisions? NEED HELP AGAIN :S! NOT done Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[HELP]Trying to make a decision cmd adventure! How to branch decisions? NEED HELP AGAIN :S! NOT done" Watch "[HELP]Trying to make a decision cmd adventure! How to branch decisions? NEED HELP AGAIN :S! NOT done" New topic
Author

[HELP]Trying to make a decision cmd adventure! How to branch decisions? NEED HELP AGAIN :S! NOT done

niels corn
Greenhorn

Joined: Jan 11, 2012
Posts: 4

Hello!

This is my first post!
I'm learning java atm(not that good yet, just really new beginner!) and try to experiment with everything i learn as i go!
So i thought it might be fun to make a little command prompt adventure game like in the adventure books, where you can decide the fate of your character by making decisions.
I'm really at the beginning but still encountered a problem... i have no idea how to make efficient or even working branches of my decision, i'll explain:

Decision 1:
1a: route A
1b: route B
1c: route C

I have no idea how to effectively branch them so i can continue on with the decision the player makes first, this keeps going exponantially so there must be something out there right?
so decision 1 = 3 options! Fine no problem! i just use a switch statement! Seems good! but wait... now how will i let them continue on 1 path?

tl;dr: Want to make little adventure text game with choices, can't seem to branch off and continue on that branch the player choses...
How to let the program know it should continue with the choice then output text after the option has been displayed specific for that branch?
NOTE: I use 2 classes because i was learning about using multiple classes and it seemed like a nice way to get the hang of it like this. Might not be needed but if i could i'd prefer to keep the multiple classes.

I hope the code explains it better than me!


Code AdventureGame mainclass:


Code AdventureGameChoices:


EDIT: something just popped in, i still don't know the answer but i'm pretty sure i'll have to use a different thing than 'void' in my gameadventurechoises class right? otherwise it can't send any info back? what should i use?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
Welcome to the Ranch

I see Mohammed has added code tags, and you can see how much better your post now looks.
You are correct to use two classes; in fact you will require many classes. Use of choices, however is a very high-level subject, using “linear logic”; there are a whole officeful of people just up the corridor from me who are researching such choices. So you need to start small. if yo0u write more than about 5 lines of code, you must compile and run that code and see what happens. If you try writing any more than that, you can accumulate errors and find it difficult to understand the errors.
You need to think object-oriented. I think your Adventure class needs Choice fields, or Decision fields. There are various tools you can use, for example an enum like this one:You are right that you can use a switch block; you can in fact use enum elements for the cases (or in Java7, Strings). But I think it would be more object-oriented to have Location objects, which you get to by following directions. Each Location object can have a number of possible Choices.
Now you see, we have already got about a half-dozen classes. You need to design the classes, and what the options, hazards and benefits of different choices in those locations are. And build them little bit by little bit. This is by no means an easy task for a beginner.
It is also worth finding about Tree and Graph structures; Graphs have nodes and edges, which can be modelled with Locations and Moves. Also find out about Maps; you can apply an option to a Map and get a Result back. The concept of a Graph, which your paths through the game cross, is the general design. The combinations of choices and results in each location represent data which can be added. The actual chape of the Graph is also a sort of data.
niels corn
Greenhorn

Joined: Jan 11, 2012
Posts: 4

Campbell Ritchie wrote:Welcome to the Ranch

I see Mohammed has added code tags, and you can see how much better your post now looks.
You are correct to use two classes; in fact you will require many classes. Use of choices, however is a very high-level subject, using “linear logic”; there are a whole officeful of people just up the corridor from me who are researching such choices. So you need to start small. if yo0u write more than about 5 lines of code, you must compile and run that code and see what happens. If you try writing any more than that, you can accumulate errors and find it difficult to understand the errors.
You need to think object-oriented. I think your Adventure class needs Choice fields, or Decision fields. There are various tools you can use, for example an enum like this one:You are right that you can use a switch block; you can in fact use enum elements for the cases (or in Java7, Strings). But I think it would be more object-oriented to have Location objects, which you get to by following directions. Each Location object can have a number of possible Choices.
Now you see, we have already got about a half-dozen classes. You need to design the classes, and what the options, hazards and benefits of different choices in those locations are. And build them little bit by little bit. This is by no means an easy task for a beginner.
It is also worth finding about Tree and Graph structures; Graphs have nodes and edges, which can be modelled with Locations and Moves. Also find out about Maps; you can apply an option to a Map and get a Result back. The concept of a Graph, which your paths through the game cross, is the general design. The combinations of choices and results in each location represent data which can be added. The actual chape of the Graph is also a sort of data.


i thought i putted the code tags?
I clicked the code button above the post and entered my code, dunno if that's what you meant, don't see what got changed really, but doesnt matter i suppose!

Hmm that's unfortunate, 70% of what you just said is a very difficult to understand for me. i will start at researching choice fields and decision fields or what do you suggest is the best thing to look into first?
As you can see i'm very new to java but eager to learn. I'm a little annoyed that such a 'simple' program involves difficult programming...
Thank you for replying!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
niels corn wrote: . . . i thought i putted the code tags? . . .
I was obviously mistaken about Mohammed’s edit. Very sorry.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7635
    
  19

niels corn wrote:As you can see i'm very new to java but eager to learn. I'm a little annoyed that such a 'simple' program involves difficult programming...

Ah, but that's the problem. Just because something looks simple doesn't mean to say that it is, and decision trees aren't; in fact, they're one of the reasons programming languages came about in the first place.

Maze problems (which is what Adventure basically is) are not the easiest. My suggestion would be to tackle something slightly less ambitious (Mastermind?) and then come back to this game when you're a bit further along - specifically, when you understand all the stuff that Campbell suggested you read up on (to which I would add recursion).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
niels corn wrote: . . . I'm a little annoyed that such a 'simple' program involves difficult programming...
It’s not such a simple task. Actually it is not the programming which is difficult, but the logic behind it.

Try writing it on a sheet of paper. Use a pencil and get a large eraser in case you need to change anything. You will end up with lots of interconnected blobs representing your locations. You can permit choices depending on the lines into and out of your locations. You can have occurrences or results. Example:-The heavy equipment and wet and dry attributes may turn out to be benefits or handicaps later in the game. You can see how much information you can use. Obviously if you are beginning, yo uwill start with something simpler.
Thank you for replying!
You’re welcome
niels corn
Greenhorn

Joined: Jan 11, 2012
Posts: 4

Thank you all!

I will let this sit a bit on the side then, it seems i'm not ready for encursion and such AT ALL!
I will bookmark this and your answers and hopefully i'll return and show my progress/ask for more help if i get stuck ^^!

Thank you and cheers!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7635
    
  19

niels corn wrote:I will let this sit a bit on the side then, it seems i'm not ready for encursion and such AT ALL!

Don't worry, you'll get there...just take small steps, like Campbell suggested. And BTW, it's recursion, not encursion.

Thank you and cheers!

You're welcome.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
Are you sure he means recursion? I didn’t think that sort of game required a recursive solution.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7635
    
  19

Campbell Ritchie wrote:Are you sure he means recursion? I didn’t think that sort of game required a recursive solution.

Perhaps not, but decision trees often lend themselves to recursion (although, as we both know, almost any recursive solution can be unrolled). But you may be right: In the case of Adventure, it could be overkill.

@niels: A nice bon-mot to remember: "To understand recursion, you must first understand recursion."

Winston
niels corn
Greenhorn

Joined: Jan 11, 2012
Posts: 4

And i'm back ^^!

i fixed it with making it return int values and then catching that with a if and else if!
Now i have a problem, basicly the biggest annoyances are gone, this is how i wish to continue(i just have to repeat all the code basicly :p)
But the problem is that i suddenly start again at my decision2(); ...
i know the problem is at line 29 but i don't know 'what' i have to do there (and the rest of my code there)...

Main:



choices:


So i fixed the way to branch of, it's certainly not the most epic way but doesn't matter, i just want to tell a story, not make it able that they pick up stuff :p.
They get guided through the program by me and their decisions can do different thingsb but ultimately nothing random happens.

I hope any of you can help! Would be awesome!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7635
    
  19

niels corn wrote:Now i have a problem, basicly the biggest annoyances are gone, this is how i wish to continue(i just have to repeat all the code basicly :p)...

No you don't. In fact it's usually bad practise. What about storing all that stuff in an array, viz:and now you only have one decision method.
You could even store your 'narrative' and 'choice' text in a file and read it in when you start the game.

I really think you need to sit down and work out on paper exactly what you need to do before you write another line of code.

Oh, and please remove the bad language from your code before you post it.
Most of us are old enough that it doesn't impress; but it may offend some people.
I suggest you edit what you've already posted.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
I have got rid of the ruder words, now that somebody has taken notice of them, and broken the long lines, and added a comment .
But I think the whole thing still looks like procedural programming to me. You don’t have decision objects leading to results requiring other decisions. You have lots of text, all about these girls and how the writer thinks he is going to pull
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7635
    
  19

Campbell Ritchie wrote:But I think the whole thing still looks like procedural programming to me. You don’t have decision objects leading to results requiring other decisions...

Totally agree.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
And think how much less code one would have to write to implement a proper object-oriented solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [HELP]Trying to make a decision cmd adventure! How to branch decisions? NEED HELP AGAIN :S! NOT done