43 lines
890 B
C
43 lines
890 B
C
#ifndef DICEROLLER_H
|
|
#define DICEROLLER_H
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
/* GetDice(char* format)
|
|
*
|
|
* This will return the value of the rolled dice.
|
|
*/
|
|
int GetDice(const char* format) {
|
|
int size = strlen(format);
|
|
int seperator = 0;
|
|
int resolt = 0;
|
|
unsigned int seed = time(0);
|
|
char c_amount[30] = { '\0' };
|
|
char c_dice[30] = { '\0' };
|
|
|
|
for(int i = 0; i < size; i++) {
|
|
if(format[i] != 'd')
|
|
continue;
|
|
seperator = i;
|
|
break;
|
|
}
|
|
if(seperator == 0)
|
|
return -1;
|
|
|
|
strncpy(c_amount, format,seperator);
|
|
strcpy(c_dice, &format[seperator + 1]);
|
|
|
|
int amount = atoi(c_amount);
|
|
int dice = atoi(c_dice);
|
|
int r = 0;
|
|
for (int i = 0; i < amount; i++) {
|
|
r = rand_r(&seed) % (dice - 1 + 1) + 1;
|
|
resolt += r;
|
|
}
|
|
|
|
return resolt;
|
|
}
|
|
#endif
|