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.
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?
1. Give her a look to frighten her.
2. Ignore her.
3. Give her a friendly smile and a wave.
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?
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.
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!
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
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.
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.
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!
Joined: Sep 01, 2010
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
Joined: Sep 20, 2010
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.
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.
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).
Edit: Oh, and you can add as much scripted text (or any kind of attributes) into the nodes as you like.
[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...:
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.