• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating A 'Create Your Own Adventure' Game In Java

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PS- I'm not quite sure how you're going to do console IO with an executable JAR. But one thing at a time.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YES. How can I make it like Zork in Java?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 67
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic