Tools for nerds

Anything that doesn't fit in the other forums, feel free to throw this way. Silly fun things encouraged!

Moderator: Hektor

Forum rules
- Use common sense and be respectful towards each other at all times, even when disagreeing.
- Do not reveal sensitive game information. Guild secrets, player seconds are examples of things not allowed.
Post Reply
User avatar
Rhaegar
Legend
Posts: 960
Joined: 13 May 2010 06:22

Tools for nerds

Post by Rhaegar » 30 May 2011 14:20

Ok now, this is extremely nerdy, I know but I've set myself on a quest to learn some programming by creating random tools for pen-and-paper RPGs.
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;
	}
}
It's a simple program written in C that allows you to generate some dice throws.

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
You'll have to compile it yourself though, as I haven't figured out how to make it properly to work on various platforms.

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.
I fear no evil for I am fear incarnate.

Sharn
Expert
Posts: 289
Joined: 04 Mar 2010 12:34

Re: Tools for nerds

Post by Sharn » 30 May 2011 17:23

http://www.wizards.com/dnd/dice/dice.htm

...but I think rolling dice is way more funnier then using a computer simulator.

User avatar
Rhaegar
Legend
Posts: 960
Joined: 13 May 2010 06:22

Re: Tools for nerds

Post by Rhaegar » 31 May 2011 15:08

sharn wrote:http://www.wizards.com/dnd/dice/dice.htm

...but I think rolling dice is way more funnier then using a computer simulator.
It is :)

I came up with this thing when we were drinking beer at the lake with no dice and no Internet access. ^_^
I fear no evil for I am fear incarnate.

Post Reply
http://tworzymyatmosfere.pl/przescieradla-jedwabne-z-gumka/