Keep It Together: Crafting the Algorithm

Back to the portfolio

When building our space comedy game Keep It Together, we wanted to ensure that it had replayability, despite being narrative-based. Therefore, we organized it around a scene randomization algorithm. Despite the game’s small size, this algorithm turned out to be surprisingly complex. In flowchart form, here’s how the game selects a scene:

And here’s a fuller explanation of how it works.

The simplest way to randomize the scenes would be to put them all in a list, choose one randomly, play it, and then remove it from the list. But this would create a game with no overarching plot. Therefore, the game is instead organized into plotlines, each composed of a series of scenes. The plotlines are stored in a list, and a dictionary matches each plotline with its starting scene. At the beginning of the game (after the tutorial material, which is static), the game chooses a random plotline, looks up its first scene in the dictionary, and plays that scene.

The endpoint of each scene contains the name of the next scene in that plotline (and there may be multiple endpoints, allowing plotlines to branch). When the game reaches the end of a scene, it records the next scene to the dictionary and repeats the process. If the next scene is listed as “none,” the plotline is finished and it is removed from the list. When there are no plotlines left on the list, that marks the end of the act.

Ah yes–acts! The game is broken into four acts to give it structure. Act 0 is simply the opening tutorial material, Acts 1 and 2 are the “meat” of the game, and Act 3 is the climax and resolution. The main plotline (broken into four stages) runs through all four acts; the remaining plotlines belong to either Act 1 or Act 2. At the end of Act 0, the game populates the list of active plotlines with a few randomly chosen plotlines from the list of Act 1 plotlines, as well as the Act 1 stage of the main plotline. (A separate variable keeps track of the next scene of the main plotline between acts; this allows the main plot to branch.)

Act 2 plotlines are generated the same way at the end of Act 1, with one difference: Player choices in Act 1 can unlock additional Act 2 plotlines. These plotlines are not guaranteed to appear in Act 2–they are simply added to the master list from which the Act 2 plotlines are selected. (Plotlines can be locked in the same way.)

To further increase replayability, there are also random scenes that can appear at any point in Act 1 or 2. When the game rolls a random scene (10% chance), it picks a scene off the list of available random scenes, plays it, and erases it from the list. Just like plotlines, player choices can lock and unlock random scenes–for instance, bringing the cat onto the station unlocks random scenes involving the cat. However, some random “scenes” are longer than a single scene–in effect, they’re short plotlines that can begin at irregular times. Therefore, when they trigger, they get added to the list of active plotlines and are treated identically to any other plotline from then on.

Act 3 is more streamlined: It has only one plotline–the main plot–and it has no random events.

And voilà. Simple!