I'm kind enough to share my work with other nerds who also enjoy automating some of the pnp stuff, in case they have a laptop with them but no usual tools of the trade (dice, books etc.).
First comes the dice generator:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct DiceConfiguration
{
int DiceNumber;
int DiceType;
};
struct DiceThrowsResult
{
int DiceNumber;
int Sum;
int ThrowsResults[];
};
struct DiceConfiguration* GetConfiguration();
struct DiceThrowsResult* ThrowDices(struct DiceConfiguration *config);
void GenerateRandomSeed(void);
int ThrowDice(struct DiceConfiguration *config);
void PrintDiceThrowsResult(struct DiceThrowsResult *result);
void ReleaseMemory(struct DiceConfiguration *config, struct DiceThrowsResult *result);
int main(void)
{
struct DiceConfiguration *config;
struct DiceThrowsResult *result;
GenerateRandomSeed();
config = GetConfiguration();
while(config != NULL)
{
result = ThrowDices(config);
PrintDiceThrowsResult(result);
ReleaseMemory(config, result);
config = GetConfiguration();
}
return 0;
}
struct DiceConfiguration* GetConfiguration()
{
int diceNumber = 0;
int diceType = 0;
printf("Enter the number of dice to throw (0 to quit): ");
scanf("%d", &diceNumber);
if(diceNumber == 0)
{
return NULL;
}
else
{
printf("Enter dice type (sides) : ");
scanf("%d", &diceType);
if(diceType < 2)
{
printf("Dice can't have less than 2 sides!\n");
return NULL;
}
else
{
struct DiceConfiguration *config;
config = malloc(sizeof(struct DiceConfiguration));
config->DiceNumber = diceNumber;
config->DiceType = diceType;
return config;
}
}
}
struct DiceThrowsResult* ThrowDices(struct DiceConfiguration *config)
{
struct DiceThrowsResult *result;
result = malloc(sizeof(struct DiceThrowsResult) + config->DiceNumber * sizeof(int));
result->DiceNumber = config->DiceNumber;
for (int i = 0; i < config->DiceNumber; i++)
{
result->ThrowsResults[i] = ThrowDice(config);
result->Sum += result->ThrowsResults[i];
}
return result;
}
void GenerateRandomSeed(void )
{
srand((unsigned int) time(0));
}
int ThrowDice(struct DiceConfiguration *config)
{
return rand() % config->DiceType + 1;
}
void PrintDiceThrowsResult(struct DiceThrowsResult *result)
{
for (int i = 0; i < result->DiceNumber; i++)
{
printf("%d ", result->ThrowsResults[i]);
}
printf("Sum: %d\n", result->Sum);
}
void ReleaseMemory(struct DiceConfiguration *config, struct DiceThrowsResult *result)
{
if (config != NULL)
{
free(config);
config = NULL;
}
if (result != NULL)
{
free(result);
result = NULL;
}
}
Here's how it works:
Code: Select all
Enter the number of dice to throw (0 to quit): 4
Enter dice type (sides) : 6
2 5 6 6 Sum: 19
Enter the number of dice to throw (0 to quit): 3
Enter dice type (sides) : 8
1 7 5 Sum: 13
Enter the number of dice to throw (0 to quit): 0
Right now I'm working on a d20 character creation tool (you enter stats and it calculates everything for you and prints it out nicely, currently customized for Star Wars: Saga Edition).
Enjoy!
P. S.
We really need spoiler tags here, to hide large blocks of text.