troll.c - rpgtools - various tools for role-playing games | |
git clone git://src.adamsgaard.dk/rpgtools | |
Log | |
Files | |
Refs | |
--- | |
troll.c (782B) | |
--- | |
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 #include <err.h> | |
4 #include <math.h> | |
5 | |
6 char *argv0; | |
7 | |
8 void | |
9 usage(void) | |
10 { | |
11 errx(1, "usage: %s DICE\nwhere DICE is N dice with M sides, in f… | |
12 } | |
13 | |
14 int | |
15 random_number(int min, int max) | |
16 { | |
17 return min + round((double)rand() / RAND_MAX * (double)(max - mi… | |
18 } | |
19 | |
20 int | |
21 main(int argc, char *argv[]) | |
22 { | |
23 int i, n, sides, roll, sum = 0; | |
24 | |
25 argv0 = *argv; | |
26 if (argc != 2) | |
27 usage(); | |
28 | |
29 sscanf(argv[1], "%dd%d", &n, &sides); | |
30 | |
31 if (n < 1) | |
32 errx(1, "number of dice must be a positive value (%d)", … | |
33 | |
34 if (sides < 2) | |
35 errx(1, "dice must be at least two sided (%d)", sides); | |
36 | |
37 for (i = 0; i < n; i++) { | |
38 roll = random_number(1, sides); | |
39 printf("1d%d\t%d\n", sides, roll); | |
40 sum += roll; | |
41 } | |
42 | |
43 if (n > 1) | |
44 printf("SUM:\t%d\n", sum); | |
45 | |
46 return 0; | |
47 } |