Tap Into Your Nerdy Side

Creating an algorithm that helps calculate Dungeons & Dragons currency

Cassie Nutter
4 min readJun 25, 2021

During the COVID-19 pandemic, many people found a chance to start new hobbies and explore new interests.

Mine came in the form of a fantasy tabletop role-playing game named Dungeons & Dragons.

A friend mentioned his interest in starting up a game. He had been watching shows on YouTube for quite some time about Dungeons & Dragons (D&D) and felt he could adapt it to be played virtually. With that simple suggestion, seven of us eagerly joined to be players and he would be our Dungeon Master.

Once a week, we met on Zoom to raid Goblin hoards or rescue a Gnome town from Mimics that were disguised as furniture and wedding cakes.

As a team, we got into a good rhythm. Knowing what each other’s strengths and weaknesses were and who had a Ray of Frost cantrip that just never worked. It wasn’t always the same person that said it, but it was always said — “LOOT THE BODIES!” After each battle, we took our time to scour the area and look for any treasures. Our group amassed a huge fortune in coins and magical items.

We could spend our acquired treasure on shopping excursions we reached a new town. There were many hours of gameplay spent checking out all the shops and people within them.

My personal favorite was the “Eye of Newt” shop where the Hobgoblin alchemist named “nEwT” somehow manages to piece together the ingredients in the correct order to make potions. (The “SeEkrit 6 pak” is a set of six random and unmarked potions and/or poisons at a steal at a price of only 500 gold pieces!)

Photo by Jan Ranft on Unsplash

In a recent game, we found ourselves again wanting to shop and unload some of our coins. The site we have been using — D&D Beyond — has been instrumental for those of us that were clueless and without dice when we began. It can keep track of many things, including your currency.

Screenshot from D&D Beyond of the Currency counter

Though this character only has 2,973 gold pieces, the total of all the coins translated into gold equals 6,094 gold pieces. (If you are familiar with American money, you can imagine gold = dollars, copper = pennies, silver = dimes, etc.)

Let’s say you wanted to buy an Alchemy Jug (which I have, no big deal) for 5,000 gold pieces. With the currency above, the character can definitely afford that magical item. However, when attempting to remove the 5,000 gold from the gold section, only the gold pieces are affected. The 2,973 gold pieces will be emptied from the inventory, but other coins will remain untouched.

In an attempt to avoid doing the math by hand and to let a computer keep doing all the hard work for me, I used my Python programming skills to create an algorithm to do it for us.

The algorithm will take in the user’s coins, tell the user what that equates to in gold, and ask the user how much gold they are wanting to spend. Then, using Dynamic Programming, the algorithm tells them the optimal amount of coins needed to reach their spend goal.

if / else statement when user does not / does have enough currency

And, if you were curious on how the algorithm suggested using coins to buy the Alchemy Jug, check out the solution below.

# solution to getting that all the mayo you could possibly dream of from that Alchemy Jug[{'Amount': 14, 'Coin Name': 'Electrum'},
{'Amount': 202, 'Coin Name': 'Platinum'},
{'Amount': 2973, 'Coin Name': 'Gold'}]

Stay tuned for the next post where I will make a webpage so everyone can use this!

If you can’t wait until then, click here for the notebook on GitHub.

Let’s be friends! Connect with me on LinkedIn or on D&D Beyond as cheese_cassadilla

Photo by rupixen.com on Unsplash

--

--

Cassie Nutter

Aspiring Data Scientist, dog lover and running enthusiast