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
  • 2
  • 9
  • 1
    Is this question better suited for [gamedev](http://gamedev.stackexchange.com/)? – Uwe Plonus Jul 16 '13 at 12:27
  • 2
    @Uwe Plonus Considered that, but I don't believe so. My question is purely conceptual and has nothing to with graphic libraries, 3D and other topics that dominate gamedev. Come to think of it, this question has little to do with games per se... but not sure how to better title it. – kaqqao Jul 16 '13 at 12:34
  • Chess and card games are **very** different from adventures. – Deer Hunter Jul 16 '13 at 13:09
  • 1
    Your question seems to cover expert system development as well. Where the range of follow up questions and possible diagnoses gets limited with every answer selected. Maybe that's what to look for to find some more "input"? – Marjan Venema Jul 16 '13 at 13:55

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
  • 82,151
  • 24
  • 234
  • 309
  • 1
    We actually use something like this in our completely non-game-related web-app. The user has a number of States, and any number of Events can be fired in code, many of which are pre-set to transition users from one State to another. I think for the described cases of the question, having some sort of rudimentary scripting language (or a full-on one like Python/Lua) to define conditions/triggers would be helpful too. – Katana314 Jul 16 '13 at 13:52
  • I like this approach quite a bit. Thanks! Will investigate it further. Any chance you know a useful library? – kaqqao Jul 16 '13 at 15:56
  • @veggen: Nope, sorry. Not a Java developer. – Mason Wheeler Jul 16 '13 at 16:03
  • @MasonWheeler: What do you think of [Lazarus](http://www.lazarus.freepascal.org/)? – Robert Harvey Jul 17 '13 at 17:02
  • @RobertHarvey: They've made a lot of progress in the last few years, but I think they're still missing some very important features that need to be present if they're going to present themselves as a viable alternative to Delphi. Most notably, after all these years, they still don't have a real Packages (BPL) system. Packages are quite useful for many situations beyond simply IDE plugins, and it's a bit silly that they don't seem to consider them an important feature. – Mason Wheeler Jul 17 '13 at 17:46
  • I'm going with this solution. I found a lot of Java FSM libs, plus decided to go with Groovy all the way as that makes in-game scripting ridiculously easy. And I have it all mostly well worked out in my head. Thanks Mason! – kaqqao Jul 17 '13 at 20:55
  • 1
    Since this question hit the popular questions list today, I'll report that I implemented this exactly as I said I would in the previous comment. I made a nice DSL and implemented the FSM logic myself as it's really, really simple. Couldn't be more satisfied with the solution. @MasonWheeler Thanks again for a great advice! – kaqqao Apr 26 '15 at 07:14
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,789
  • 4
  • 39
  • 58
  • In general - look for the Rete algorithm implemented in **any** rule engine of your liking. – Deer Hunter Jul 16 '13 at 13:09
  • Yup, Drools was one of the things I started with. I have to investigate a little more to see if I can evaluate just one package of rules at a time, as that is rather important in my case. Thanks! – kaqqao Jul 16 '13 at 16:01
  • @veggen glad to help ! – Jalayn Jul 16 '13 at 17:12
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,819
  • 19
  • 18
  • My issue is calculating the possible responses. Past decisions influence current options. So, if a player presented with "you stumble upon a dead police officer" chooses "steal his pistol" instead of "don't touch anything", he later gets an option to "shoot the pursuer" in addition to "run away like mad", which would have been the only option had they not previously acquired a gun. – kaqqao Jul 16 '13 at 12:49
  • @veggen: See the updated answer. – Gilbert Le Blanc Jul 16 '13 at 12:58
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
  • 7,930
  • 3
  • 35
  • 54