Yesterday I have solved the Bracketing Search exercise level 2 stars, now I'm doing this level 4 stars with some modified I've made for challenging myself(ignore it), this is not homework, there's no programming education in my place..
Here's the Bracketing Search level 4 stars
Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.
If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.
The program must let the user continue to guess until the user correctly guesses the number.
★★ Modify the program so that instead of the user guessing a number the computer came up with, the computer guesses the number that the user has secretely decided. The user must tell the computer whether it guesed too high or too low.
★★★★ Modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.
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
|
// Exercise 5 - Bracketing Search Level 4 Star other MODIFIED
// Difficulty : Lunatic
// * Modify the program so if the user tell lies after 7 or more tries, the computer will telling him/her that you're lying.
// ***** Modify the program so the user will be asked to try again or not..
#include <cstdlib>
#include <limits>
#include <iostream>
#include <time.h>
#include <conio.h> // this conio.h is a dumb
#include <string>
using namespace std;
void process();
int main()
{
char ready;
cout << "Think a number between 1-100 and the computer will try to guess it in within 7 tries or less.\n"; getch();
cout << "Give your inspection for every time the computer guess, \n";
cout << "If your number is bigger enter (1), if lower enter (2), if correct enter (3)\n";
cout << "\nAre you ready ?(y)/(n)";
do {
cin >> ready;
switch (ready)
{
case 'y' : cout << "Ok let's get started."; getch(); break;
case 'n' : cout << "So you are not ready yet ? Ok I'll close the application now. Thank you."; getch(); return 0;
default : cout << "You've entered invalid command, please enter (y)/(n): ";
}
} while (ready != 'y' && ready != 'n');
process();
getch();
return 0;
}
void process()
{
retrying :
char retry;
int Max = 100, Min = 1, inspection;
int temporary;
cout << "\n\nThink your number now. Press Enter if you have your secret number in your head... \n\n"; getch ();
srand(time(NULL));
int ComputerGuess = rand() % 100+1; int ComputerTries;
cout << "Is your number: " << ComputerGuess << " ?\n";
ComputerTries = 0;
do { cout << "Enter your inspection here: ";
while (!(cin >> inspection) || inspection > 3 || inspection < 1)
{
cin.clear(); // Clear any error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear input stream // (requires #include <limits>
cout << "\nYou input invalid command, try again: ";
}
if (inspection == 3)
{
cout << "\nSo the computer guessed correct as expected not more than 7 tries. Thank you for trying. " << endl;
cout << "Try Again ? (y)/(n): ";
do { cin >> retry;
switch (retry)
{
case 'y' : cout << "Ok let's get started."; getch(); break;
case 'n' : cout << "Ok then, I'll close the application now. Thank you."; getch(); return;
default : cout << "You've entered invalid command, please enter (y)/(n): ";
}
} while (retry != 'y' && retry != 'n');
goto retrying;
}
ComputerTries++;
if (ComputerTries == 8)
{
cout << "\nYou're lying too much, so I'll close the program now. Bye then. "; return;
}
else if (inspection == 1) // I believe this is the main problem, please check it for me
{
Min = ComputerGuess+1;
temporary = ((Max - Min)/2);
ComputerGuess = Min + temporary;
cout << "\nIs your number: " << ComputerGuess << " ?" << endl;
}
else if (inspection == 2) // This one too
{
Max = ComputerGuess - 1;
temporary = ((Max - Min)/2);
ComputerGuess = Min + temporary;
cout << "\nIs your number: " << ComputerGuess << " ?" << endl;
}
} while ( "nothing");
}
|
Okay, first I'm sorry if my code is to long or not efficient, but the main problem is when the computer supposed to guess the user secret number not more then 7 tries,
sometimes it's worked sometimes it's not, I mean the computer will guess correct at 8th tries sometimes(not more than 8 though).
Aside from this main problem, this code work nice and fine. I have did this from I wake up till lunch time and I have not take bath and breakfast just to solve this, please help..
Big Thanks.