From e619d7e271de7583abf8fdabb378ce9541b71641 Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 14 Sep 2025 23:14:07 +0200 Subject: [PATCH] feat: created new project --- .gitignore | 2 ++ src/dice.c | 8 ++++++++ src/dice.h | 22 ++++++++++++++++++++++ src/interactive.c | 6 ++++++ src/interactive.h | 6 ++++++ src/main.c | 23 +++++++++++++++++++++++ src/output.c | 27 +++++++++++++++++++++++++++ src/output.h | 10 ++++++++++ src/parse.c | 8 ++++++++ src/parse.h | 10 ++++++++++ 10 files changed, 122 insertions(+) create mode 100644 .gitignore create mode 100644 src/dice.c create mode 100644 src/dice.h create mode 100644 src/interactive.c create mode 100644 src/interactive.h create mode 100644 src/main.c create mode 100644 src/output.c create mode 100644 src/output.h create mode 100644 src/parse.c create mode 100644 src/parse.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3558c56 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +build/ diff --git a/src/dice.c b/src/dice.c new file mode 100644 index 0000000..5e54d6d --- /dev/null +++ b/src/dice.c @@ -0,0 +1,8 @@ +#include "dice.h" +#include + +int roll_die(struct roll_data data) { + int min = data.count; + int max = data.count * data.die - data.count; + return min + (rand() % max); +} diff --git a/src/dice.h b/src/dice.h new file mode 100644 index 0000000..ec21da4 --- /dev/null +++ b/src/dice.h @@ -0,0 +1,22 @@ +#ifndef DICE_H +#define DICE_H + +enum die { + D4 = 4, + D6 = 6, + D8 = 8, + D10 = 10, + D12 = 12, + D20 = 20, + D100 = 100, + COIN = 2, +}; + +struct roll_data { + int count; + enum die die; +}; + +int roll_die(struct roll_data data); + +#endif // !DICE_H diff --git a/src/interactive.c b/src/interactive.c new file mode 100644 index 0000000..9698bf6 --- /dev/null +++ b/src/interactive.c @@ -0,0 +1,6 @@ +#include "interactive.h" +#include + +void interactive() { + fprintf(stderr, "interactive mode not implemented\n"); +} diff --git a/src/interactive.h b/src/interactive.h new file mode 100644 index 0000000..241ae0a --- /dev/null +++ b/src/interactive.h @@ -0,0 +1,6 @@ +#ifndef DICE_INTERACTIVE_H +#define DICE_INTERACTIVE_H + +void interactive(); + +#endif // !DICE_INTERACTIVE_H diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..9581692 --- /dev/null +++ b/src/main.c @@ -0,0 +1,23 @@ +#include "dice.h" +#include "interactive.h" +#include "output.h" +#include "parse.h" +#include +#include +#include + +int main(int argc, char *argv[]) { + srand(time(NULL)); + int mod; + struct roll_data roll_data = { 1, D4 }; + if (argc == 1) { + interactive(); + return 0; + } else if (parse(argv[argc-1], &roll_data, &mod)) { + print_roll(roll_data, mod, roll_die(roll_data)); + return 0; + } else { + print_help(); + return 0; + } +} diff --git a/src/output.c b/src/output.c new file mode 100644 index 0000000..b7d635d --- /dev/null +++ b/src/output.c @@ -0,0 +1,27 @@ +#include "output.h" +#include +#include + +static void print_mod(int mod) { + if (mod > 0) { + printf("+%d", abs(mod)); + } else if (mod < 0) { + printf("%d", abs(mod)); + } +} + +void print_roll(struct roll_data data, int mod, int roll) { + printf("rolled: %dd%d", data.count, data.die); + print_mod(mod); + printf(" (%d", roll); + print_mod(mod); + printf(")"); +} + +void print_error(char const *error) { + fprintf(stderr, "format incorrect: %s\n", error); +} + +void print_help() { + fprintf(stderr, "help not implemented\n"); +} diff --git a/src/output.h b/src/output.h new file mode 100644 index 0000000..9418fd2 --- /dev/null +++ b/src/output.h @@ -0,0 +1,10 @@ +#ifndef DICE_OUTPUT_H +#define DICE_OUTPUT_H + +#include "dice.h" + +extern void print_roll(struct roll_data data, int mod, int roll); +extern void print_format_error(char const *error); +extern void print_help(); + +#endif // !DICE_OUTPUT_H diff --git a/src/parse.c b/src/parse.c new file mode 100644 index 0000000..92e507e --- /dev/null +++ b/src/parse.c @@ -0,0 +1,8 @@ +#include "parse.h" +#include +#include + +bool parse(char *die_arg, struct roll_data* o_data, int *o_mod) { + fprintf(stderr, "parse not implemented\n"); + return false; +} diff --git a/src/parse.h b/src/parse.h new file mode 100644 index 0000000..6571969 --- /dev/null +++ b/src/parse.h @@ -0,0 +1,10 @@ +#ifndef DICE_PARSE_H +#define DICE_PARSE_H + +#include +struct roll_data; + +extern bool parse(char *die_arg, struct roll_data* o_data, int *o_mod); + +#endif // !DICE_PARSE_H +