-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
132 lines (113 loc) · 3 KB
/
functions.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "functions.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include "stdbool.h"
void init_random_seed(void)
{
srand((unsigned)time(NULL));
}
int generate_random_number(int min, int max)
{
return rand() % (max - min + 1) + min;
}
int get_player_guess(void)
{
int guess, numScanned;
bool validInput = false;
while (!validInput)
{
printf("\nPlease enter your guess: ");
numScanned = scanf("%d", &guess);
if (numScanned == 1)
{
validInput = true;
}
else
{
printf("That's not an integer. Please try again.\n");
// Cleaning the input buffer to remove unwanted characters
int c;
while ((c = getchar()) != '\n' && c != EOF)
{
}
}
}
return guess;
}
int check_guess(int guess, int actual_number)
{
if (guess > actual_number)
{
return 1;
}
else if (guess < actual_number)
{
return 2;
}
else
{
return 3;
}
}
void display_result(int result, int attemps)
{
printf("\n");
printf("******************************\n");
printf("Answer: %d\n", result);
printf("Number of attempts: %d\n", attemps);
printf("******************************\n");
}
void play_game(void)
{
printf("==================================================================================\n");
printf("\t\tWelcome to Guess the Number Game\n\nIn this game, the computer picks a random number within a chosen range, and you\ntry to guess it. The goal is to guess the number in the fewest attempts possible.\n");
printf("==================================================================================\n");
printf("\n\tPlease enter the range!\n\t-----------------------\nMin: ");
int min, max;
int inputStatus = scanf("%d", &min);
while (getchar() != '\n');
printf("Max: ");
int inputStatus_1 = scanf("%d", &max);
if (inputStatus != 1 || inputStatus_1 != 1)
{
printf("\nThat's not an integer. Closing the game.\n");
return;
}
if (max <= min)
{
max = min;
}
printf("\nThe range you entered is between %d and %d\n", min, max);
init_random_seed();
int rand_number = generate_random_number(min, max);
bool exit = false;
int attemps = 0, result = 0;
while (!exit)
{
int userGuess = get_player_guess();
if (userGuess > max || userGuess < min)
{
printf("Please enter a number between %d and %d!\n", min, max);
continue;
}
result = check_guess(userGuess, rand_number);
if (result == 1)
{
printf("Too high!\n");
}
else if (result == 2)
{
printf("Too low!\n");
}
else
{
printf("\nCorrect guess. You win!\n");
result = rand_number;
exit = true;
}
attemps++;
}
display_result(result, attemps);
}