Added diceroller.h so that this can be included into other projects
This commit is contained in:
@@ -34,4 +34,6 @@ This will build from the file in ./build
|
|||||||
```shell
|
```shell
|
||||||
$ cmake --build build
|
$ cmake --build build
|
||||||
```
|
```
|
||||||
|
## Includeing Into Your Project
|
||||||
|
If you want to include this into your own c/c++ just include the header file.
|
||||||
|
|
||||||
|
|||||||
42
source/diceroller.h
Normal file
42
source/diceroller.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#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
|
||||||
@@ -8,46 +8,11 @@
|
|||||||
* #############################################################################
|
* #############################################################################
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "diceroller.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <stdio.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main (int argc, char *argv[]) {
|
int main (int argc, char *argv[]) {
|
||||||
int roled;
|
int roled;
|
||||||
|
|||||||
Reference in New Issue
Block a user