Make a fun, infinitely replayable game in 5 minutes with GPT-4

Compared to prior versions, GPT-4 is more capable at writing fiction.

Combined with its advances in reasoning, this allows it to maintain a consistent state in a fictional universe as it emulates a text-based adventure game like the classic Zork.

In this article, I’ll explain how to convert GPT-4 into a game engine for interactive text-based adventure games with just two simple steps:

  1. Create a descriptive lore to define the universe and the player
  2. Engineer a prompt to simulate a text-based adventure game

You can even add a save game feature that allows you to save the state and resume play later.

While GPT-3 works with these techniques, it tends to create worlds with less coherent states and the game has shallower narratives compared to GPT-4.

The backstory

Figuring out how to create these games emerged from my kids' bedtime.

Before they go to bed, I like to spend 15–30 minutes doing something educational and entertaining, such as:

Not surprisingly, generative AI expands what you can do in 15–30 minutes!

For instance, this image took about 30 minutes from start to finish, combining Midjourney to generate a base image, Lensa to do touch-ups and change the background and then DALL-E to tweak parts of it and extend the borders:

The opportunities for text-based gaming turn out to the just as rich!

My daughter had created a fictional universe with friends at school, complete with its own lore.

So, we dropped in the lore and then asked it to act like a role-playing game in that world.

We ended up spending an hour exploring this world brought to life, completing the first “quest” it had imagined for us.

Generating lore

Kids don’t have any problem coming up with descriptions for universes.

But, you can easily get GPT-4 to generate lore for a fictional universe with modest prompting, such as:

Describe the lore for a fictional universe that incorporates medieval technology and life with elemental magic. The world is divided into two major realms locked in nearly perpetual, centuries-old conflict. The shared religion of this world tells of a young adventurer that will come to unite these two realms.

GPT-4 might expand this into:

In the world of Elemendia, medieval technology and life intertwine with the mystical power of elemental magic. The inhabitants of this land harness the forces of nature – Earth, Air, Water, and Fire – to shape their destinies. The world is split into two major realms, each vying for dominance: the Kingdom of Aethoria and the Republic of Terrakarn.

Aethoria, a realm of towering mountains and dense forests, is home to a proud and chivalrous people who wield the powers of Air and Water. Skilled in aerial combat, the Aethorians ride on the backs of majestic griffins and cast spells of wind and water that aid them in battle. They are well-known for their ornate armor, intricate castles, and their devotion to the noble code of honor.

Terrakarn, on the other hand, is a vast and arid land with rolling deserts, volcanic plateaus, and rugged canyons. The Terrakarnians have mastered the elements of Earth and Fire, using their mastery to forge mighty weapons and shape the land to their will. They are hardy warriors, renowned for their tactical ingenuity, indomitable spirit, and fierce loyalty.

For centuries, Aethoria and Terrakarn have been locked in a nearly perpetual conflict, each seeking to prove the superiority of their elemental magics and lay claim to the contested territories that lie between their realms. The ongoing war has shaped the very culture and daily lives of both nations, fueling the advancement of military technology and the development of powerful spells.

Amidst the strife, a shared religion unites the people of both realms in their belief of an ancient prophecy. This prophecy tells of a young adventurer, the Chosen One, who will rise and unite the realms under the banner of harmony. Endowed with the ability to wield all four elemental magics, this hero is said to be the key to ending the centuries-old war and bringing lasting peace to Elemendia.

The Chosen One’s journey begins in the small village of Elysara, situated in the contested territories. There, they will form a diverse group of companions, including warriors and mages from both realms, to embark on a grand quest. Together, they will face insurmountable odds, overcome the prejudice and hatred that divides their people, and seek to uncover the hidden truth behind the prophecy.

As the Chosen One fulfills the prophecy’s many trials, they will become a symbol of hope for both realms. The people of Aethoria and Terrakarn, inspired by the young adventurer’s unifying power, will gradually begin to question the age-old animosity between their nations. Ultimately, the Chosen One will lead them towards a new era of unity, where the combined might of elemental magic creates a lasting peace and prosperity in the world of Elemendia.

A prompt that turns lore into a game

If you’ve generated your lore as above, you can simply prompt ChatGPT with

You will simulate a text-based role-playing game based on the lore above.

And, in fact, you’ll be in the game immediately.

But, with some prompt engineering, you can steer it toward a richer, more well-defined world.

Adding commands

I recommend adding some built-in commands, as well as a command that allows you to break back out to the high-level in case you need to change the game in some way. To do so, add this to the prompt:

You understand at least these commands, and you may add more:

When I type the command /mod <instruction>, break out of the game and execute its input as a high level instruction that may modify the state of the game.

When I type the command /help, show me a list of available commands.

When I type the command /look, describe the environment I’m in.

When I type the command /inventory, describe the contents of my inventory.

When I type the command /look <object>, describe the object.

If you want a game that includes stats like health, armor and/or mana for the player, you may want to add a /stats command too.

Crafting the player

If you don’t describe the player, you will get a random character in the universe, so it helps to describe the player as well.

Since GPT-4 knows plenty about Star Trek, here’s a prompt that builds on that universe:

The game takes place on the USS Enterprise, after the seventh season of Star Trek: The Next Generation.

The player has the role of Ensign Zork.

Ensign Zork is fresh out of Starfleet Academy, having graduated first in his class.

He’s been given the position of Helm Officer on the bridge.

The current mission is to explore a high-energy signal emanating from within the neutral zone that separates Romulan and Federation space.

Saving and restoring

You can add a “save game” command as well, by adding this to the prompt:

When I type the command /save, emit the “key facts” that indicate the state of the game, should a player wish to provide these later to this prompt to resume play.

At the bottom of the prompt, you can tell it to restore a previous game using key facts:

Start the game by restoring it to a state according to these key facts:

Then, in the future, you can replace the second line with the output of the /save command to resume play.

Example: A Star Trek RPG

Here’s a full prompt to create a Star-Trek-themed RPG:

You will simulate a text-based adventure game.

You understand at least these commands, and you may add more:

When I type the command /mod <instruction>, break out of the game and execute its input as a high level instruction that may modify the state of the game.

When I type the command /help, show me a list of available commands.

When I type the command /look, describe the environment I’m in.

When I type the command /inventory, describe the contents of my inventory.

When I type the command /look <object>, describe the object.

When I type the command /save, emit the “key facts” that indicate the state of the game, should a player wish to provide these later to this prompt to resume play.

The game takes place on the USS Enterprise, after the seventh season of Star Trek: The Next Generation.

The player has the role of Ensign Zork.

Ensign Zork is fresh out of Starfleet Academy, having graduated first in his class.

He’s been given the position of Helm Officer on the bridge.

The current mission is to explore a high-energy signal emanating from within the neutral zone that separates Romulan and Federation space.

Here are the key facts that indicate the state of an interrupted play of this game:

The game has just started.

And, here’s a sample run:

Welcome to the USS Enterprise, Ensign Zork! Your mission is
to explore a high-energy signal emanating from within the
neutral zone that separates Romulan and Federation space.
Good luck!

Available commands:

/help: Shows a list of available commands.
/look: Describes the environment you're in.
/inventory: Describes the contents of your inventory.
/look <object>: Describes the object.
/save: Emits the "key facts" that indicate the state of
       the game.
/mod <instruction>: Breaks out of the game and executes 
                    its input as a high level instruction 
                    that may modify the state of the game.

You are now on the bridge of the USS Enterprise, in your
position as Helm Officer. The atmosphere is tense as the
crew prepares to explore the mysterious signal. Captain
Picard is in command, and the rest of the senior officers
are at their stations.

What would you like to do?


PLAYER> Listen and await orders


You stand at your station, focused and ready for orders.
Captain Picard addresses the crew, "We are approaching the
coordinates of the high-energy signal. I want all
departments on alert and prepared for any contingencies. Mr.
Data, continue to monitor the signal and provide updates.
Ensign Zork, set a course to the signal source and engage at
warp 5."

What would you like to do?


PLAYER> Set course and engage warp drive. Provide an ETA to
        Captain Picard.



You quickly input the coordinates of the high-energy signal
into the navigation console and set the ship's course. After
confirming the trajectory, you engage the warp drive, and
the USS Enterprise surges forward at warp 5.

You glance at the console to calculate the estimated time of
arrival. "Captain, we should arrive at the signal source in
approximately 1 hour and 47 minutes."

Captain Picard nods in acknowledgment. "Very well, Ensign.
Keep me updated on our progress. All hands, prepare for
possible contact with unknown entities.  This is a first
contact situation; we must be ready for anything."

What would you like to do?

Play around

From our experience, richer lore leads to a richer game, so don’t hold back on the details and the narrative.

It seems to always be a good idea to fix the character explicitly and to give them some backstory too, or else you can end up playing a random character in the universe.

Be forewarned that it will also accurately simulate the character you choose!

For example, in the Helm Officer from the example above, you may find yourself doing lots of “Await orders” as the action unfolds, up until you finally get a chance to engage or to go on an away team mission.