• Advertisement
Sign in to follow this  

Trying to write a basic D&D Character sheet program.

This topic is 1966 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, guys. This is my first post so I hope that this goes well, otherwise I will find myself in the corner of my faded-blue room sobbing profusely.

So I'm trying to write, with my limited knowledge of c and c++ (the latter I taught myself the basics in a few weeks over the summer), a somewhat-basic D&D Character sheet program.

The concept is somewhat skewed since my mind can't seem to wrap around what I want to achieve when rolling for ability scores and such. So far, I'm just striving for a simple program that can execute(Of course, without failing), take the input of the character's name, race, and possibly pick some storyline options, and then output them onto a .txt file on their desktop (which I'm trying to find a way to do through precise direction of the fopen deal), and such and such.

Sorry if this seems a bit mushy, I haven't slept much. Ha...

Anyway, what I'm trying to ask is;

How would one randomize four integers, being equal to that of a six sided dice, and then take the four numbers I get and remove the lowest integer, then add them together? I can't seem to wrap my brain around how I could pick out the lowest integer if something happens to come up the same, such as two fours, and two fives, how would i code it to remove just one of the fours, not both of them, so someone doesn't get the ability score of Ten?

If none of what I have just asked makes sense, I'm willing to answer any questions you might have.

And I know of the rule about no homework assignments on here, so to let you guys know this is strictly for pleasure. I'm trying to impress my RP Crew with this simple executable program that everyone can use to make five character sheets in five minutes instead of taking about 4 hours.

If someone can help me piece this together, I should be able to make an efficent program. :D Hopefully!

Share this post

Link to post
Share on other sites
You're basically wanting to just do a quick sort on the array and then either remove or ignore the initial element. C++11 has random number generation in the library with distribution, etc... but you could also use the C function rand() Edited by Saruman

Share this post

Link to post
Share on other sites
How would one do that quick sort? I was doodling on my white board trying to figure out the best possible way to sort out the random integers that would remove the lowest integer out of the four numbers without removing more than the one integer if it were to roll multiples of the same integer. I was messing around with trying to divide down the average of the rolls, but I always seemed to take out more or less than I needed out of the average. Such as if it were to randomize the integers 4, 3, 6, 2, I'd add them up into a variable, then divide it by the number of rolls and then remove that from the sum of the four. But that never worked. Could you explain what you mean by that quick sort?

Share this post

Link to post
Share on other sites
And to clarify, when you roll an ability score for a d20 based dungeons and dragons character sheet, you just roll four six-sided dice and remove the lowest number, then add up the total of the three remaining dice. if you happen to roll all sixes or a multiple of the lowest number, you would just take out one of the dice, or one of the multiples, and then add them up. That's what i'm trying to achieve in a simple function that I can print out onto the text file without having the players have to manually do it, which is time consuming. I said all this assuming you've never played D&D or any Tabletop rpg, but if you have then this is redundant. Haha.

Share this post

Link to post
Share on other sites
You can use the standard library to have your program four to a text file that it creates, look it up, it's really easy.

Share this post

Link to post
Share on other sites
One way to implement Saruman's suggestion:

std::array<int, 4> rolls;
std::generate(rolls.begin(), rolls.end(), []() { return (rand() % 6) + 1; }); // fill the array with rolls
std::sort(rolls.begin(), rolls.end(), std::greater<int>()); // sort the array so biggest numbers are first
int total = std::accumulate(rolls.begin(), rolls.begin() + 3, 0); // add up the first three numbers

Share this post

Link to post
Share on other sites
Could you explain what those std::array and std::generate things are? I'm guessing those are class things? I'm not that far into c++ or c to quite understand what those mean, but I know about randomizing numbers and seeding and such. If it's not too much trouble, could you elaborate what's going on? :D

Share this post

Link to post
Share on other sites
std::array<> is a class that acts like a normal array except it has some functions to make life more convenient. In this case I'm using the begin() and end() member functions to get iterators that I pass to the generate(), sort() and accumulate() algorithms. std::generate() is an algorithm that takes a range and a function object and for every element in that range assigns the value of a call to that function object. Since I use begin() and end() for the array, it assigns every element in the array a call to the function, which is your basic 1d6 roll. std::sort() is an algorithm that sorts the range it's given. By default it sorts from low to high. Because I use std::greater<> as an argument it sorts from high to low. std::accumulate() adds up all the elements in the range it's given to a starting value. It can also be used to multiply every element together or any other binary operation, but by default does addition. In this case I don't pass the entire range of the array, just a range for the first three elements of the array, which gives the sum of the three greatest.

Share this post

Link to post
Share on other sites
I actually just finished my basic character sheet program today. I wrote it in about an hour. I'm proud of myself since it does everything that I want it to do so far. I want to add a few more things to it as I tinker around with strings and arrays so I can add in some racial text (Such as if you're an elf, dwarf, etc etc) and then possibly add more to the application, like re-rolling certain stats and stuff. Here's my code, (Warning, it's kinda big)

Program : CharacterSheet.ccp
Author : Charles Jarret Grassi
Date : 9/30/2012
Version : 1.0
Desc : A Dungeons and Dragons Character Ability Score
automated roller, using a 4d6 stat engine to
get even, balanced stats for the six categories;
Strength, Constitution, Dexterity, Intelligcen, Wisdom, and Charisma
:D Use it wisely!

Soon to come : - Add "Host and Player" lines to the top
- Add Races, Background, and Equipment
- Add a secondary program; "Encounters"
- A name Randomizer
- Editable interface.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
int main()
FILE * pFile;
pFile = fopen("CharacterSheet.txt","w");
fprintf(pFile, " DUNGEONS & DRAGONS - Character Sheet ");
fprintf(pFile, "\n\n Strength: Modifier [ ] ");
fprintf(pFile, "\n\n Dexterity: Modifier [ ] ");
fprintf(pFile, "\n\n Constitution: Modifier [ ] ");
fprintf(pFile, "\n\n Intelligence: Modifier [ ] ");
fprintf(pFile, "\n\n Wisdom: Modifier [ ] ");
fprintf(pFile, "\n\n Charisma: Modifier [ ] \n");
int nInput;
printf("\n\n Welcome to the Dungeons and Dragons AP roller! ");
printf("\nThe program has been executed and is storing stats ");
printf("inside of a 'character.txt' file inside this folder.");
printf("\n\n You will recieve the raw ability scores with the ");
printf("modifiers beside the stat. You may allocate the stats");
printf("within any core stat you may like. If you have recieved");
printf("a bad score, exit the program and run it again. :3 ");
printf("\n\n If you wish to save this sheet without deleting the stats");
printf(", make sure you change the name of the text file before running");
printf(" the program again, otherwise it will delete all of the data stored");
printf(" inside the text file and write over it. :D ");
printf("\n\n======Developed by Dragon Master INC.======");
printf("\n\n Press 0 to exit the program. Press any other key to continue. ");
scanf("%d", &nInput);
if(nInput == 0)
srand( time(NULL) );
for(int nAccu = 0; nAccu < 6; ++nAccu)
int nMod;
int nStat = 0;
int nMin = 7;
for(int nAccumulator = 0; nAccumulator < 4; nAccumulator++)
printf("\n\n Rolling...");
// Make sure not to seed a rand inside of a loop
// the results will be the same every time.
int nRoll = rand() % 6 + 1;
cout << "\n nRoll = " << nRoll;
if(nRoll < nMin)
nMin = nRoll;
nStat += nRoll;
nStat -= nMin;
printf("\n\n Stat : %d \n\n", nStat);
nMod = (nStat - 10)/2;
printf(" Modifier : %d \n\n", nMod);
printf(" Outer Loop Accu : %d \n\n", nAccu);
fprintf(pFile, "\n Stat : %d \n Modifier : %d \n ", nStat, nMod);
// Listing the stats on the page via switching. (Neater. :D)
printf("\n\n\n !END!");

If you can't already tell, it just uses some basic loops to generate the rolls, stores them in the stat and then prints them all to a text file. My friends were extremely impressed since they don't know how to program. I felt like a badass. But yeah, I'm going to study these class functions that you were describing and hopefully tinker around with them so they make sense to me. xD


I apologize if my coding is sort of sloppy. I threw it together while in a crunch to start building character sheets for my friends, and instead of doing things the easy way and just rolling stats, I had to make things harder for myself and make a program to do it. xD. So any comments that don't seem to fit, just ignore them. I had differnet mind sets on how I wanted the stats to be printed onto the text file, such as using a switch inside the loop to print out each stat name with the rolled stat and modifier, but it ended up printing out each stat six times which didn't work, so I just threw the stat names down at the top of the program just to be neat. Edited by Prefantastic

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement