diff --git a/README.md b/README.md index 580dbb7..5480e1f 100644 --- a/README.md +++ b/README.md @@ -34,4 +34,6 @@ This will build from the file in ./build ```shell $ cmake --build build ``` +## Includeing Into Your Project +If you want to include this into your own c/c++ just include the header file. diff --git a/source/diceroller.h b/source/diceroller.h new file mode 100644 index 0000000..bc899cf --- /dev/null +++ b/source/diceroller.h @@ -0,0 +1,42 @@ +#ifndef DICEROLLER_H +#define DICEROLLER_H +#include +#include +#include +#include + +/* 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 diff --git a/source/main.c b/source/main.c index 2d65e2b..5df0e7a 100644 --- a/source/main.c +++ b/source/main.c @@ -8,46 +8,11 @@ * ############################################################################# */ +#include "diceroller.h" -#include #include -#include -#include +#include -/* 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; -} int main (int argc, char *argv[]) { int roled;