16

I want to make a simple game similar to the choose-your-own-adventure books. The player is presented with a narrative text and gets to choose his action from a list of possibilities. This, in turn, leads to a new narrative text, ad infinitum. The only catch is that depending on some previous decisions, the list of possibilities might differ.

At first glance, this sounds like a load of if-else statements, thus implying a rule engine would be in place. But, it also sounds like a finite state machine to me.

I am about to write this in Java or maybe Groovy. I'm currently more interested in the conceptual issues, i.e. how should this be done on a broad level (how do people implement chess or card games, anyway?), but some advice on a specific library in also welcome.

Obviously, the "game engine" from the title does not refer to collision detection or other physics/graphics mechanics, but the logic deciding what options does a player have given the situation and his current state.

kaqqao
  • 363

4 Answers4

7

Based on what you've said in comments, this is how I would handle it:

Implement the story as a finite state machine, with a twist. Each State is a page of the story, and each Transition is a link from one page to another. But each Transition also has Conditions. The Conditions could be null, in which case the Transition always shows up as an available option, but if not, then they have to be evaluated when the page displays, and if the evaluation returns False, the Transition does not show up.

There are two basic ways you could implement the Conditions. The first is to set up a full-blown script engine inside the game, and then the Condition looks like return player.inventory.contains(GUN). This is initially more complicated to set up, but allows for more advanced scripting.

The second is to hard-code the possible conditions into some sort of object. It could have a RequiredItem field, and if that field has a value, you check to see if the condition is met. This system is simpler to set up. It limits what you can do a lot more than scripting would, but if you don't need the flexibility that a script engine would provide, it's probably a lot easier to use.

Mason Wheeler
  • 83,213
5

I think the answer is in the title: you need a rules engine. If you are planning to write your application with Java, you of course can write your own as Gilbert Le Blanc suggested, OR you might want to take a look at Drools, a rules engine.

what options does a player have given the situation and his current state

With Drools, or any other rules engine in fact, you can define a parameterized situation which outputs a list of possible actions. You can encode simple rules suc has:

  • player is at pageX:

    • choice 1: title: "go left", action: "page45"
    • choice 2: title: "go right", action: "page56"
    • IF player has the Staff of Fireballs THEN choice 3: title "launch fireball", action: "page32"
    • IF player has a Perception Skill of 10 THEN choice 4: title "check writings on the wall", action: "page67"

What is interesting with Drools is that you can encode all your rules in an Excel file, and then at the beginning of the game, make Drools read that file. After that everything is in memory, you only need to bother about your User Interface.

Here are some ressources to help you get started with Drools:

Jalayn
  • 9,827
2

Conceptually, your game is straightforward. In psudeocode, it would look something like this:

while not at end of adventure story
    display text
    get response

Now, chaining all of the text together so it flows from one action to the next is the hard part. You could use a relational database. You could use a tree.

It's a little hard to be more specific without knowing what computer language you want to use. Since you mentioned Java, I'd lean more towards a tree structure.

Create a response class that holds one response and a link to a text class.

Create a text class that holds the adventure text, and a List of responses as instances of the response class.

Edited to answer the comment:

You don't calculate anything based on this model. Using your example, the tree would look something like this, where T is the text and A is an action choice:

T You stumble on a dead police officer
    A Take the gun
    T You hear footsteps
      A Run away
      A Hide and see who comes
    A Don't touch anything
    T You hear footsteps
      A Run away

Yes, there is some duplication of text, but by following the chain, future actions can take into account past decisions. It's a huge decision tree.

Gilbert Le Blanc
  • 2,839
  • 21
  • 18
2

The state machine sounds like a safe approach to model your game. There are lots of libraries for interactive fictions:

http://en.wikipedia.org/wiki/Category:Text_adventure_game_engines

not mentioned in wikipedia, twine is very popular at the moment.

Simon Bergot
  • 8,020