commit 0571f250b07981d96226350aa3e19150f7ec1a40 Author: Tait Hoyem Date: Sun Aug 28 18:22:42 2022 -0600 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..02eb5e2 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +CC_FLAGS := -O2 -Wall -std=c99 +PLATFORM := linux # supports windows, macos + +default: + $(CC) $(CC_FLAGS) -DPLATFORM=$(PLATFORM) -o roll roll.c diff --git a/README.md b/README.md new file mode 100644 index 0000000..aadfe77 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# `roll` + +Basic di(c)e roller utility. + +## Usage + +Supports basic roll functionality for standard any set of dice and their sides. +You *must* always include both a number of dice and the sides of the dice. + +```bash +$ roll 5d2 +6 +``` + +By default, the sum of the rolls will be printed out on the terminal. + +## Options + +THE OPTIONS DO NOT WORK AT THIS TIME. + +* `-a` -- `--all` + * Show a list of all roles instead of the sum (overrides default behaviour). +* `-s` -- `--sum` + * Show the sum of all roles. This may be combined with other options. + diff --git a/roll b/roll new file mode 100755 index 0000000..9bd87c8 Binary files /dev/null and b/roll differ diff --git a/roll.c b/roll.c new file mode 100644 index 0000000..37a49a7 --- /dev/null +++ b/roll.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +enum Flags { + LIST_ALL = 0x1, + SHOW_SUM = 0x2, +}; + +unsigned int roll_one(unsigned int sides) { + return (rand() % sides) + 1; +} + +void roll(unsigned int sides, unsigned int num_rolls, unsigned int* buffer) { + for (unsigned int i = 0; i < num_rolls; i++) { + buffer[i] = roll_one(sides); + } +} + +int read_roll(const char* arg, unsigned int* sides, unsigned int* num_rolls) { + return sscanf(arg, "%ud%u", num_rolls, sides); +} + +int main(int argc, char** argv) { + if (argc != 2) { + printf("You must provide exactly one argument.\n"); + exit(1); + } + unsigned int sides, num_rolls; + time_t t; + if (!read_roll(argv[1], &sides, &num_rolls)) { + printf("Invalid arguments! Must be of format d, where N is the number of dice to roll and is the sides on the die.\n"); + exit(2); + } + enum Flags flags = 0x0; + for (int argi = 0; argi < argc; argi++) { + if (strcmp(argv[argi], "-s") || + strcmp(argv[argi], "--sum")) { + flags |= SHOW_SUM; + } else if (strcmp(argv[argi], "-a") || + strcmp(argv[argi], "--all")) { + flags |= LIST_ALL; + } + } + // default if no arguments supplied: show only the sum + if (flags == 0x0) { + flags |= SHOW_SUM; + } + unsigned int rolls[256] = {0}; + // seed random number generator + srand((unsigned) time(&t)); + + roll(sides, num_rolls, rolls); + unsigned int sum = 0; + for (unsigned int i = 0; i < num_rolls; i++) { + sum += rolls[i]; + if ((flags & LIST_ALL) == LIST_ALL) { + printf("%u", rolls[i]); + if (i == num_rolls-1) { + printf("\n"); + } + } + } + if ((flags & SHOW_SUM) == SHOW_SUM) { + printf("%u\n", sum); + } +}