State Space

A state space algorithm represents a quest as a graph (see Generative Grammars for explanation of graphs) where nodes are states of the game, and edges are actions between states. It starts with the initial game state, then repeatedly applies actions like "walk" or "fight" to create new game states. The algorithm continues doing this until a desired ending game state is found, then returns the path from start to end.

State Space Algorithm
Because this algorithm is so general, it's better to present it in pseudocode. A Python implementation is shown in the code examples. An operator here is simply something that causes a change in state: traveling to a new area, defeating a boss, finding an item, etc. You can also define a maximum number of nodes permitted in the graph. This prevents quests from being too long, and also stops the algorithm for running too long. 1: Construct the node representing the initial state 2: If every node is a terminal node, mark this node as a failure and try on another node. If this node is the goal state, return the path form the initial state to this one 3: Select an applicable operator 4: Create child nodes by applying the operator If the number of nodes created exceeds the amount of nodes allowed, mark this node as a failure and try another one. Otherwise go to step 2 and repeat

State Space Algorithm Explained
The algorithm begins with the initial state. This could mean something like the player being in the starting town of the game, with no items. The desired end state of the game involves having a particular weapon and using it to defeat the final boss. Taking examples from the typical RPG, there are many steps that can happen in between those two points: moving to other locations, fighting enemies, retrieving items, and so on.

The state space algorithm simply goes through the possible actions the player could do from the start. It builds a path, one step at a time, seeing what happens if a certain action is performed. If a state is reached that is not the desired ending, but no more actions can be applied, then that course of actions is rejected. Similarly if a list of actions becomes too long, it's rejected. The algorithm continues doing this until a successful path from start to end is found.