dungeons & python

FINAL PROJECT FOR INFO-664 BY SHIVANI ISHWAR

PROJECT GOALS

Dungeons & Dragons is a tabletop roleplaying game in which each player creates a character with a combination of traits, skills, and equipment to generate a unique game-playing experience. The customization options are nearly endless — and at times, that can be overwhelming. While creating a character is fun and exciting, it also takes a lot of time and can intimidate new players.

This project's goal is to make that process a little easier. I found an API that contains most of the necessary information to create a D&D character in the 5th edition of the game, at https://www.dnd5eapi.co/. Using Python scripts to request information from this API, I created a process that randomizes all of the pieces of a character creation process. This results in a completely random playable character with much less effort on the part of the player.

This project consists of 7 Python scripts, each of which takes steps to build different aspects of the character. In order, here are the steps taken to create a playable character:

  • Select a race at random (and a sub-race, if applicable).

  • Select a class at random (and a sub-class, if applicable).

  • Based on the race and class, choose skills for the character to be proficient in.

  • Randomly generate ability scores, then plug those numbers in along with the proficiencies to create all of the numerical scores for the character.

  • Based on the character's class, determine the number of spells they can learn, and choose which spells to learn.

  • Based on the character's race, choose the languages they know.

  • Based on the character's race, determine the extra traits and abilities they have.

  • Based on the character's class, choose the weapons and equipment they carry.

  • Compile all of the character's information into one file.

WORKFLOW

PROCESS

This project uses the Python modules "requests," "json," and "random." The "requests" module allows Python to parse information from the API, the "json" module allows it to read and write JSON format, and the "random" module is perhaps the most important to the process. The "random" module includes functions and methods for randomly selecting items from lists and dictionaries in Python, which is what allows this random character generator to work.

Each step of the process consists of one Python script, which exports its generated data to a JSON file. Then the next Python script reads that JSON file, creates new information, and exports more JSON data. At the end, the last Python script combines all of the generated data into one JSON file.

I also created a shell script, which works for Mac OS to be run in the Terminal. This script automatically runs all of the scripts in order and deletes the extra JSON files at the end of the process. If that shell script doesn't work on a different OS, the Python scripts can still be run manually in order.

While this project works as a pretty simple character generator, I was unfortunately limited by the information contained in the API. One major component of character creation was completely missing from the API: the "background" selection, which gives the characters additional skills and equipment, and most importantly, determines the amount of money the character starts the game with. So sadly, that information isn't included in the generated characters from this project.

In addition, I discovered a couple of typos in the API, which led to the scripts not executing correctly if those options were randomly chosen. This required a bit of troubleshooting, but ultimately I was able to overcome this challenge.

Overall, even with a few flaws and shortcomings, this project still makes the process of creating a character much easier, so I still consider it a success.

ROADBLOCKS

RESULTS

At the end of the character generation process, the script asks for one piece of user input: the character's name. Using that along with all of the other pieces that were randomly generated, the project creates a JSON file that contains all of the character's information. Here are a few characters that I generated to test my scripts:

The JSON file resulting from the character creation process will have a lot more information in it, including ability scores, armor class, and spells. You can discover all of that information by running this project yourself.

All of the Python scripts, plus the shell script, can be downloaded from GitHub at https://github.com/shivaniishwar/DNDproject. You can run the scripts yourself, or modify them to suit your needs.

In addition, if the shell script doesn't work on your OS, the readme file contains detailed instructions on how to run the Python scripts successfully.

ORIGINAL FILES

FUTURE WORK

This project was an interesting experiment in random generation, and is a great starting point for streamlining the D&D character creation process.

With more time, and in future iterations, this project could be expanded to allow characters to level up. While the current iteration creates characters at Level 1, with a bit more work (and more information than the API currently contains), the characters could be given a higher level, and all the modifications and bonuses that come with it.

This code could also be modified so that the player has more control over certain variables. For example, the scripts could ask for user input on which race and class they would prefer, and then generate the rest of the character data based on that. This would be a useful feature for players who already have an idea of what they would like their characters to be like.

Finally, this project could be expanded to include a weighting mechanism. That could take into account existing D&D characters, submitted by existing players, to note which combinations of skills and traits are the most popular. For more on the popularity of certain D&D archetypes, check out this article.