summaryrefslogtreecommitdiff
path: root/Dice/src/dice.c
blob: 5aee7b95ace5a3368b425c256e3dcf7b3ca2f960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <tice.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#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;
}