forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguessingGame3.c
More file actions
52 lines (41 loc) · 1.61 KB
/
Copy pathguessingGame3.c
File metadata and controls
52 lines (41 loc) · 1.61 KB
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
/* This is the number guessing game program */
/* This program was written in the C programing language
By (randerson112358)
*/
# include <stdio.h>
# include <stdlib.h>
# include <math.h> // Library used for math in this case log() and ciel()
int main(void)
{
//Declaring Variables to be used
int lowRange = 1;
int highRange = 100;
int possibleGuesses = highRange + lowRange - 1;
int maxTurns = (int)(log(possibleGuesses)/log(2)) + 1; // = log base 2 of possible guesses by rules of logarithms
int yourGuess;
int playerAnswer;
int countNumTurns = 1;
printf("Choose a number from %d to %d.\n", lowRange, highRange);
printf("I will guess your number in %d turns or less.\n\n", maxTurns );
//This loops through the algorithm
do{
possibleGuesses = highRange + lowRange - 1;
yourGuess = (int) ceil(possibleGuesses / 2.0);
printf("Is your number %d ?\n",yourGuess );
printf("Press (1) Yes (2) Guess a lower number (3) Guess a higher number\n");
scanf("%d", &playerAnswer);
if(playerAnswer == 3)//Guess was to low
lowRange = yourGuess + 1;
if(playerAnswer == 2) // Guess was too high
highRange = yourGuess - 1;
if (playerAnswer == 1)
break;
countNumTurns++;
}while(playerAnswer != 1 && countNumTurns <= maxTurns);
//Print the end results
if(countNumTurns > maxTurns)
printf("You made a mistake somewhere, you've exceeded the maximum turns. \n");
else
printf("I guessed your number in %d turns !\n", countNumTurns );
system("pause"); //This is only for windows operating system otherwise comment it out.
}