From 9a6959b2700ec54392fb2ffb145bacae66fc2269 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Wed, 18 Aug 2021 17:31:19 -0400 Subject: initial commit --- Dice/src/dice.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Dice/src/dice.c (limited to 'Dice/src') diff --git a/Dice/src/dice.c b/Dice/src/dice.c new file mode 100644 index 0000000..5aee7b9 --- /dev/null +++ b/Dice/src/dice.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +#define EnterKey 5 +#define maxline 22 +#define ExitKey 9 +#define ExitCode -1 + +int diceRoll(int sides); +int getInput(void); + +int main(void) +{ + while (true) { + os_ClrHome(); + + printf("Dice Roll\nPress Clear to exit\nHow many sides?\nInput: "); + + int ans = diceRoll(getInput()); + + if (ans == ExitCode) + break; + + printf("\nOutput: %d", ans); + + while (!os_GetCSC()); + } + return 0; +} + +int diceRoll(int sides) +{ + if (sides == ExitCode) + return ExitCode; + time_t t; + srand((unsigned) time(&t)); + return (rand() % sides) + 1; +} + +int getInput(void) +{ + char number[maxline]; + char *ptr; + int16_t key, i = 0, output; + while((key = os_GetKey()) != EnterKey) { + if(key == ExitKey) + return ExitCode; + number[i] = (key - 142) + '0'; + printf("%c", number[i++]); + } + output = strtol(number, &ptr, 10); + return output; +} -- cgit v1.2.3