aspose file tools*
The moose likes Game Development and the fly likes Creating A 'Create Your Own Adventure' Game In Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Creating A Watch "Creating A New topic
Author

Creating A 'Create Your Own Adventure' Game In Java

Jason Newman
Ranch Hand

Joined: Oct 20, 2010
Posts: 53
I am currently writing a 'Create Your Own Adventure' game in Java. I know the basics of java, and I know how to write a story. There will be story, choices, then more story. Here is a chunk of the story so far

The year is 1917, and America has just entered the Great War. It is a cold morning a few miles off of the coast of New York,

and you can see the fog rolling across the water. You shiver and embrace yourself, only to fend off some of the cold. As you stare

off of the bow the fog begins to clear, and Lady Liberty comes into view. Your hear races in excitement, and you shake more.


///////////////////NEXT///////////////////////////


Your nose feels a sharp breeze, and starts to run. You wipe it off on the sleeve of your worn out jacket, the one your father

gave to you. You look around to see if anyone noticed, and you see a little girl around seven holding the hand of her mother. She is

staring right at you. What do you do?

///////////////Choice #1//////////////

1. Give her a look to frighten her.

2. Ignore her.

3. Give her a friendly smile and a wave.


/////////////NEXT/////////////////

The little girl looks embarrassed and turns away, a shy child obviously. You remember that age don't you? Clinging to your

fathers hand wherever you go, shy and afraid of the world. She isn't too different from you, now is she? Let's talk about your father

now, shall we?

//////////////NEXT////////////////////

Unlike most fathers, yours wasn't a drunk. He was all you had, you loved him. But you know that, don't you? He raised you

from birth, and made you into the man you are today. He loved your mother, and even though you killed her in child birth he never

blamed you. Never did he guilt you or look down on you for it, he viewed you as the last living thing of her. He gave you the best life

he could, which wasn't very much.

///////////////CHOICE #2////////////////

Growing up in poverty wasn't always easy, or ever easy. Your father loved you, and kept you out of the factories as long as he

could, but you two could only go so long without two sources of income. The three factories closest to your hometown where the

Steel Mill, the Lumber Mill and the Textile Mill. The working conditions where all terrible and kids died everyday working in these

factories. But you needed the money badly, so you got a job. Where did you work at?

1. Steel Mill

2. Textile Mill

3. Lumber Mill

Now, the problems is (I feel dumb) I have NO clue how to do this, or even how to start. If you can help me with how to write the code, or give me a tutorial (I couldn't find the right kind) it would really help. You've helped me before guys, lets see if you can help me again!
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
How do you want to do this? Text based? From a console (like Zork, games like that)? With a GUI?

Which part are you having trouble with?
Jason Newman
Ranch Hand

Joined: Oct 20, 2010
Posts: 53
Yes, it is going to be text based, and it is going to run as an exe JAR that will be downloaded.

I need help with... Heh... Everything I suppose. Well.... I need help getting it started and piecing it all together.
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Start small. Very small. Break your problem up into separate pieces, and get those pieces working independently before you even think about putting them together.

Do you know how to output text to the console? Do you know how to get input? Those would be two solid places to start. Google is your friend.

If you get stuck, feel free to post an SSCCE demonstrating what you've tried and ask a specific question about that one piece.
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
PS- I'm not quite sure how you're going to do console IO with an executable JAR. But one thing at a time.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

what you basically need is a state table or a tree. At each point in the story, the user has 2+ choices (do you want to define an upper limit or design it to be expandable to any number of choices?) So each node would have the current text of the story, and what the choices are. Then, you would go to the next node based on the choice the user makes, and print that stuff to the screen.




There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Yeah, a 'dialogue tree' would be a good way to do this. What you could do is make 'story nodes' that reference to a piece of the story, and that reference to a series of 'choice nodes'.

Make the 'choice nodes' reference to one other story node.

You can improve the choice nodes with conditions that determine when they appear, for instance, the main character has taken a particular path in the past, or is in possession of a certain item. You can also enhance both the choice nodes and the story nodes with effects, that will occur when a particular path is chosen, for instance, the main character is given a certain item when he takes a specific path.

http://en.wikipedia.org/wiki/Dialog_tree
Jason Newman
Ranch Hand

Joined: Oct 20, 2010
Posts: 53
YES. How can I make it like Zork in Java?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

umm...what do you mean by "like Zork"?

You'd do the exact same thing, except you need a language parser to figure out what the user types and what they mean. Then you have to check and see if it's valid. I.e. if they type 'put statue in backpack', you have to check and see if they have a statue, if they have a backpack, and if the statue will fit (there may be some items that aren't allowed in the backpack)

Your nodes will now have to be able to handle not only where they can go (if they are in the foyer, they can get to the bathroom (e), kitchen(s) and hall (n), but not the bedroom which is n of the hall), but weather the action makes sense there, and what the results would be. Swinging the sword in the kitchen might break a pot, but in the foyer might break the coat rack.
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
    
    1
Hi - It seems to me there are a number of ways you could organize this. I'm relatively new to Java myself, so some of the suggestions might not be the most efficient, or they might be at too basic a level.

First off, I'd avoid using the console. It could be done, but a lot of folks don't know how to deal with the consoles of their computers. There is no reason not to display the text via Swing, e.g., JFrame & either a single JPanel that gets updated, or a series of JPanel-based classes, e.g., class StoryStart extends JPanel, class Branch1 extends JPanel, class Branch1a extends JPanel, etc., but you end up with maybe a lot of duplication and 50 or 100 classes depending upon the size of the story!)

Since you are offering choices, rather than requiring inputs, you could consider using buttons for the choices. Alternatively, a JTextField could be made where one types in the choice, but you'd have to deal with parsing the typing. With JButtons, you would have to learn how to program a Listener for the Button. But this is a standard part of Swing, and thus covered in most any halfway decent beginning Java book. Also, adding and deleting text, images and buttons with labels for the buttons to and from a JPanel is very straightforward.

The code that executes when the JButton is pressed can either call the "next" JPanel (if you produce this as a stack of JPanels) or it can call stored text or stored JButtons as needed and add them to the JPanel (after deleting the prior objects). JPanels, JTextAreas, JButtons can all be created and added or deleted from a JFrame or whatever container as part of what gets executed by the code called from the Button choice. Last step of any button code would be a repaint(), I'm assuming.

Also, you'll probably want to make some private class variables to hold "state" information that might need to be referenced by the code the button triggers.

If you are updating a single JPanel, an option is to keep the same buttons but update what they do. If you check out the HF Design Patterns book, famous on this site, take a look at the "Strategy" pattern (chapter 1) as a possible way to update the code that the buttons execute.

Sounds like a fun project, and I hope I get a chance to try it once it is complete!

--Phil Freihofner
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
    
    1
Just remembered this layout maybe worth looking at: CardLayout. Might be a way to organize a lot of JPanels (one per story page) onto a single JPanel. I haven't tried this yet. Maybe have two panels on a frame, one with buttons and a button listener as a driver panel, the other with a display panel with a CardLayout and lots of story pages and data to update the buttons' "labels" and "strategies". Just a thought.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

I think simplest would probably be a JTextArea that is uneditable, and simply clear it and fill it with new text with each new part of the story.
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Not to take away from the whole gui idea here, but there's just that whole element of gameplay that you take away when setting everything in stone using a gui to present a limited set of choices. There's a fun factor in guessing what you have to type, like figuring out that you have to swing a sword in a room to break a pot, or use a certain key in a certain lock, as opposed to clicking one of three options (without having to think about it much) that will quickly progress you through the game. I'm all for approaching this from the command line, and even with having to code the parsing mechanism, it can be just as manageable of a solution as doing it through swing (in my opinion), and possibly even more fun. Or at least let the user type commands in through the gui, versus having a few button choices. That way you solve the problem of opening the command prompt from the executable jar.


Studying for SCJP 6
Mike Peters
Ranch Hand

Joined: Oct 10, 2009
Posts: 67

Maybe a bit off topic, but you may want to take a look at inform (http://www.inform-fiction.org/I7/Welcome.html). I think it is pretty easy and has a lot of features to create this kind of adventure.


Mike Peters
Sarah Becker
Greenhorn

Joined: Nov 14, 2010
Posts: 5
Hi everyone,

I've used directed graphs for making dialogue trees. I'm not sure if this is the best way to do this but it has worked for me.
Node (or Vertice) itself is an instance of a class that contains attributes such as pointers (or edges) to other nodes,
the dialogue text and the id of the node. I've included the edges in the Node class as a private subclass and then just created
instances from it to other nodes by their id.
http://en.wikipedia.org/wiki/Directed_graph
A simple way to search a graph is the breadth-first search (works for undirected graphs as well).
http://en.wikipedia.org/wiki/Depth-first_search

Cheers,
Sarah

Edit: Oh, and you can add as much scripted text (or any kind of attributes) into the nodes as you like.
Scott Foulk
Greenhorn

Joined: Dec 02, 2010
Posts: 1
[size=9]In a Zork type of game, there are lots of predefined, numbered rooms. The rooms have doors in and out, in all the directions, if you want. The numbers pertain to where in an array you place the room object.

You must first define your world by making a diagram, with your rooms (diagram as little square boxes) and pathways leading N,NE,E,SE,S,SW,W,NW, UP, and DOWN.
thus, if a player is in room x and types up, he/she will be in the connected room no.

Here's an easy way to make the rooms; it's busy but you wanted to write games, so...:
[/size]

So you can see where I'm going with this. make your diagram first, then encode it. I always loved these types of games, and probably still do but haven't the time to play. Others may do this differently, so look around.

Hope this helps though.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating A 'Create Your Own Adventure' Game In Java