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

Started by
11 comments, last by Charles Jarret Grassi 11 years, 7 months ago
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.
Advertisement
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)
{
exit(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)
}
fclose(pFile);
printf("\n\n\n !END!");
system("PAUSE");
return(0);
}


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

-Edit-

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.
Oh and thank you, RulerofNothing, for the Roll code. You're code is the meat and potatoes of my program. Haha.

This topic is closed to new replies.

Advertisement