Added a basic dice roller.

I built a basic dice roller because I needed one.
I want to add a few more features later on, though.
This commit is contained in:
Barry Kane 2022-12-07 11:07:19 +00:00
parent 821cba1136
commit 7e1d8cd961
1 changed files with 52 additions and 0 deletions

52
roll.c Normal file
View File

@ -0,0 +1,52 @@
// roll.c: A command line dice roller.
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct roll
{
unsigned diceSides;
unsigned numberOfDice;
} roll;
roll parseRollString(char * rollString)
{
char * numberString, * sidesString, * currentPointer;
currentPointer = rollString;
numberString = strtok_r(rollString, "dD", &currentPointer);
sidesString = strtok_r(NULL, "dD", &currentPointer);
roll roll;
roll.numberOfDice = (unsigned)atoi(numberString);
roll.diceSides = (unsigned)atoi(sidesString);
return roll;
}
int rollDice(roll roll)
{
int rolledValue = 0;
for(unsigned rolls = 0; rolls < roll.numberOfDice; rolls++)
{
rolledValue += (rand() % roll.diceSides) + 1;
}
return rolledValue;
}
int main(int argc, char ** argv)
{
srand((unsigned)time(NULL) * getpid());
roll roll;
if(argc < 1)
{
roll = parseRollString(argv[1]);
}
else
{
roll.diceSides = 20;
roll.numberOfDice = 1;
}
int result = rollDice(roll);
printf("You rolled a: %d\n", result);
return result;
}