-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
74 lines (64 loc) · 1.83 KB
/
main.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Q2.c
// The program is a roulette game that random numbers from 0-36 and the user tries to guess the random number
// Eilon Ashkenazi 23/05/2017
// -----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// Function Decleration
bool exitClicked(char * input);
int checkInput(char * input);
void main()
{
// Variable decleration
char input[5]; // The user input
int credits = 100; // The amount of credits
int randomNumber; // The random number
srand(time(NULL));
printf("Total Credits : %d \n", credits);
printf("Enter a number between 0-36: \n");
scanf("%s", &input);
// the loop runs as long as not exit/quit clicked or the user has enough credits
while (!(exitClicked(input)) && credits > 0)
{
randomNumber = (rand() % 37); // Random the numbers
printf("Spinning the wheel...\n");
printf("The random number is: %d\n", randomNumber);
if (randomNumber == checkInput(input)) // Checks the user input with the random number
{
printf("YOU WON!\n");
credits += 36;
}
else
credits -= 1;
printf("Total Credits : %d \n", credits);
printf("Enter a number between 0-36: \n");
scanf("%s", &input);
}
if (credits == 0)
printf("Game over, you are out of credits...");
else // If credits are not 0, then exit or quit clicked
printf("Exiting...");
}
// The function converts input from string to integer
int checkInput(char * input)
{
int result = 0;
sscanf(input, "%d", &result);
return result;
}
// The function checks if exit or quit clicked
bool exitClicked(char * input)
{
int resultExit;
int resultQuit;
bool finalResult = false;
resultExit = strcmp(input, "exit");
if (resultExit == 0)
finalResult = true;
resultQuit = strcmp(input, "quit");
if (resultQuit == 0)
finalResult = true;
return finalResult;
}