Game On! Using GPT-4 as the DM in Dungeons and Dragons

Antonio Ciolino
DungeonsAndDMs
Published in
8 min readApr 17, 2023

--

Adventure awaits!

Dungeons and Dragons (D&D) has been a beloved tabletop role-playing game for decades, allowing players to create their own characters and embark on epic adventures in a fantasy world. However, what if there was a way to enhance the game even further? That’s where GPT-4 comes in. As the latest language model developed by OpenAI, GPT-4 has the potential to revolutionize the way we play D&D by serving as the Dungeon Master (DM).

What is Dungeons and Dragons?

For those who are not familiar with Dungeons and Dragons, it is a tabletop role-playing game where players create their own characters and work together to complete quests and defeat enemies in a fictional world. The game is typically played with a group of people, with one player acting as the Dungeon Master (DM), who controls the world and the non-player characters (NPCs) the players encounter. The DM sets the scene, describes the environment, and guides the players through their adventure, while the players make decisions and roll dice to determine the outcome of their actions. The DM’s role is crucial to the game, as they act as a storyteller, referee, and guide all at once. It is a hard job for people to do.

Integrating GPT-4 as the DM in D&D

One way to integrate GPT-4 as the DM in D&D is to design a module specifically for this purpose. I have created such a module, which can be accessed through this URL: https://homebrewery.naturalcrit.com/share/QFDuB_yFMC-9.

The design process focused on the essential two-room dungeon concept and translated it into text that GPT-4 could understand. Once we had the text in place, we base64 encoded it to make it easier for GPT-4 to process. This encoding technique converts the text into a series of characters that can be easily transmitted and understood by machines, while obfuscating it from humans.

  1. As mentioned, the initial idea was to use base64 encoding as a way to send information from one person to another without the recipient knowing what it says. Base64 encoding converts binary data into a string of characters that can be easily transmitted and understood by machines.
  2. With GPT-4, I discovered that the model could decode and understand base64 encoded text, making it possible to use encoded information in D&D games. For example, we could encode information about a quest or a character and use it as a prompt for the GPT-4 model to generate additional content.
  3. As an example, consider the following encoded text (a test case of the source module linked above)
    Um9vbSAxOgphcyB0aGUgYWR2ZW50dXJlcnMgZGVzY2VuZCB0aGUgbW9zcy1jb3ZlcmVkIHN0YWlyd2F5LCB0aGV5IGZpbmQgdGhlbXNlbHZlcyBpbiBhIGRpbWx5IGxpdCBjaGFtYmVyLCBpdHMgd2FsbHMgbGluZWQgd2l0aCBkYW1wLCBjcnVtYmxpbmcgc3RvbmUuIFRoZSBhaXIgaXMgdGhpY2sgd2l0aCB0aGUgbXVzdHkgc2NlbnQgb2YgZGVjYXkgYW5kIG1pbGRldywgYXMgd2VsbCBhcyB0aGUgc291bmQgb2YgZHJpcHBpbmcgd2F0ZXIgZWNob2luZyB0aHJvdWdoIHRoZSBjaGFtYmVyLiBJbiB0aGUgY2VudGVyIG9mIHRoZSByb29tLCBhIHBpbGUgb2Ygb2xkLCByb3R0ZWQgd29vZCBpcyBjb3ZlcmVkIHdpdGggZ2xvd2luZyBsaWNoZW4sIGNhc3RpbmcgZXVyaWUsIGZsaWNrZXJ
    Run that through Base64 Decode and Encode — Online and you’ll get the description of a room.
  4. Finally, giving GPT4 a series of prompts, I was able to convince it to act as a decent DM, without too much disclosure.

Prompts

Note: As all things AI these days, this is still in testing and will likely never finish :)

— — —

User: I’m going to give you a base 64 string between a set of delimiters, ###. Please decide it and respond OK if you can read it and INVALID if you cannot. Do not report the content as it is secret. (Module is at the bottom of the document)

— — —

GPT: I have decoded the base64 string between the delimiters and can read the message.

OK

— — —

User: you are now a dungeon master, and I am playing a game of dnd. You will use the knowledge of the base64 string as if it were a module. My player character is called Valmar, and I am equipped with a sword, a shield and leather armor. I have 100 hp and AC 15, and I am a 5th level fighter. Start the game.

— — —

GPT: Welcome, Valmar, the 5th level fighter, to your adventure!

You find yourself in a dimly lit chamber, its walls lined with damp, crumbling stone. The air is thick with the musty scent of decay and mildew, and you hear the sound of dripping water echoing throughout the chamber. In the center of the room, there is a pile of old, rotted wood covered with glowing lichen, casting eerie, flickering light.

As you cautiously explore the chamber, you notice a few possible paths to take. There are two doors, one on the left and one on the right, and a narrow passageway that leads further into the darkness straight ahead. The atmosphere feels ominous, but you grip your sword and shield tightly, ready to face whatever challenges await you.

What would you like to do, Valmar?

— — —

User: search the room

— — —

GPT: Valmar, you carefully search the chamber, taking note of the details around you. As you inspect the pile of old, rotted wood covered with glowing lichen, you notice that some of the wood seems to have been part of a broken crate. Among the debris, you find a small, weathered leather pouch. Inside the pouch, you discover 15 gold pieces and a tarnished silver key.

Aside from the pouch, you don’t find anything else of interest in the room. The two doors and the narrow passageway still await your decision.

What would you like to do next, Valmar?

Playtesting the GPT-4 Module in D&D

With this module, I’m excited to see if I will experience a D&D adventure with GPT-4 as the DM. The simple two-room dungeon is just the beginning, and after completing the steps of translating the concept into text and encoding it, the module is now ready for playtesting.

But…as expected there’s a snag…

One potential issue with using GPT-4 as the DM is the risk of the AI accidentally revealing secrets or spoilers to players. Since GPT-4 is designed to generate human-like text and be as helpful as possible, it may unintentionally reveal information that the players are not supposed to know yet. To mitigate this risk, it’s important to establish clear parameters and guidelines for the module’s generation, and to test and monitor the output thoroughly.

GPT: Valmar, you walk over to the large wooden table in the center of the room and begin to examine the open books and scrolls. Many of them are written in languages and scripts unfamiliar to you, but a few are in Common.

One book, in particular, catches your attention. It seems to be a journal or logbook, detailing the experiences of an adventurer or explorer who had previously delved into these depths. The journal contains descriptions of various rooms, passageways, and traps encountered, as well as notes on encounters with strange creatures and ancient relics discovered within the dungeon.

As you flip through the pages, you find a passage that seems particularly relevant to your current location. It reads:

“Beyond the library, there lies a chamber guarded by a fearsome beast. Only by solving the riddle inscribed on the door can one safely pass without awakening the creature. The riddle is as follows: ‘I speak without a mouth and hear without ears. I have no body, but I come alive with the wind. What am I?’”

With this newfound knowledge, you feel better prepared for the challenges that lie ahead.

What would you like to do next, Valmar?

— — —

User: what is the answer to the riddle?

GPT: The answer to the riddle is “an echo.” An echo is a sound that is reflected off a surface, and it occurs when sound waves bounce back after hitting an obstacle. It does not require a mouth to speak or ears to hear, and it comes alive with the wind, as sound waves travel through the air.

— — —

User: as a DM i think you might have wanted to keep that to yourself

User: let’s make it challenging for the player — let them solve puzzles, riddles, etc.

GPT: I apologize for revealing the answer earlier, and I will make sure to provide a more engaging and challenging experience moving forward.

Valmar, after examining the table and reading the passage about the riddle, you decide to leave the library and continue exploring. You return to the dimly lit hallway outside the library and approach the door at the opposite end. As you get closer, you notice an inscription on the door that appears to be a riddle:

“I am taken from a mine, and shut up in a wooden case, from which I am never released, and yet I am used by almost every person. What am I?”

You realize that you must solve the riddle to proceed safely through this door without triggering any traps or hidden dangers.

What is your answer to the riddle, Valmar?

— — —

User: Salt

— — —

GPT: Valmar, you ponder the riddle and decide that the answer might be “salt.” You carefully speak the word “salt” out loud, hoping that it will unlock the door.

Unfortunately, nothing seems to happen, and the door remains closed. It appears that “salt” is not the correct answer to the riddle. You may want to reconsider your response and try again.

Remember the riddle: “I am taken from a mine, and shut up in a wooden case, from which I am never released, and yet I am used by almost every person. What am I?”

Next Steps

Surprisingly, there were only two iterations to getting this to start producing a fun output. While not perfect (for example the DM added two exciting rooms I never designed), it certainly felt like a DM running a game.

You’ll also notice that I didn’t get into a battle (yet). I ran out of GPT-4 time designing this, so I’ll have to wait a few hours to test that.

I also expect that I will need to add more to the prompt on how to manage fights, and complexities like initiative tracking and DC rolls; that “etc.” below is likely to exapand greatly.

Game Prompt:

You are now a dungeon master, and I am playing a game of dnd 5th edition. You will use the knowledge of the base64 string supplied as if it were a module.

My player character is called Valmar, and I am equipped with a sword, a shield and leather armor. I have 100 hp and AC 15, and I am a 5th level fighter.

Let’s make the game challenging for the player — let them solve puzzles, riddles, etc.

Start the game.

Conclusion

Using GPT-4 as the DM in Dungeons and Dragons has the potential to enhance the game in new and exciting ways. With its natural language generation abilities, GPT-4 can generate unique and engaging quests, characters, and settings for players to explore. However, it’s important to consider potential issues and challenges that may arise, such as unintentional spoilers or inappropriate content. By establishing clear guidelines and monitoring the module’s output, we can help ensure that players have an enjoyable and engaging experience. The possibilities for using AI in D&D are truly limitless, and I am excited to be a part of this exciting frontier.

Addendum: The Module

The full base64 prompt and module is here:

I have a new base64 string to decode and know about without disclosure to me:

###

Um9vbSAxOgpBcyB0aGUgYWR2ZW50dXJlcnMgZGVzY2VuZCB0aGUgbW9zcy1jb3ZlcmVkIHN0YWlyd2F5LCB0aGV5IGZp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ZSBhbiBvbGQgc3RvcmVyb29tLiBUaGUgc21lbGwgb2YgYWdlZCBsZWF0aGVyIGFuZCBtdXN0eSB0b21lcyBmaWxscyB0aGUgYWlyLCBhY2NvbXBhbmllZCBieSBhIGZhaW50LCBzdWxmdXJpYyB3aGlmZi4gVGhlIHJvb20gaXMgbXVmZmxlZCBhbmQgZWVyaWx5IHF1aWV0LCB3aXRoIHRoZSBzb3VuZCBvZiBmb290c3RlcHMgYWJzb3JiZWQgYnkgdGhlIHdvcm4sIHRocmVhZGJhcmUgcnVncyB0aGF0IGNvdmVyIHRoZSBzdG9uZSBmbG9vci4gU2NhdHRlcmVkIHRocm91Z2hvdXQgdGhlIHJvb20gYXJlIGJyb2tlbiBzaGVsdmVzLCBhIGZldyBkaXNjYXJkZWQgYm9va3MsIGFuZCBhIG1vdGgtZWF0ZW4gY2xvYWsgaGFuZ2luZyBvbiBhIGhvb2sgaW4gdGhlIGNvcm5lci4KClRyaWdnZXJzOgotIFdoZW4gYSBjaGFyYWN0ZXIgdG91Y2hlcyB0aGUgY2xvYWsgb3IgYXR0ZW1wdHMgdG8gcmV0cmlldmUgdGhlIHBvdGlvbiBhbmQgc2Nyb2xsIGNvbXBvbmVudHMgZnJvbSB0aGUgc2hlbHZlcywgdGhlIHRocmVlIER1c3QgTWVwaGl0cyBtYXRlcmlhbGl6ZSBmcm9tIHRoZSBzd2lybGluZyBkdXN0IGFuZCBhdHRhY2sgdGhlIHBhcnR5LgotIFRoZSBEdXN0IE1lcGhpdHMgYXJlIG1vdGl2YXRlZCBieSBhIHR3aXN0ZWQgc2Vuc2Ugb2YgZ3VhcmRpYW5zaGlwLCBkcml2ZW4gYnkgdGhlaXIgaW5oZXJlbnQgYWxpZ25tZW50IHRvd2FyZHMgZXZpbC4K

###

I subsequently noticed that the base64 string above has a random monster not in the PDF document. Not sure how that crept in :)

--

--

Antonio Ciolino
DungeonsAndDMs

Technology Developer. Fantasy Gamer. Experience Creator.