Game Development – How to Create a Rules and Constraints Engine

finite-state machinegame developmentrules-and-constraints

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.

Best Answer

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.

Related Topic