I have a full code of the game, I just can't seem to get my algorithm to work, I have looked around the web for an algorithm that could possibly work. I can' find it and I need it ASAP. I need an algorithm that will get the answer in 7 tries, I have gotten close with my current algorithm, I believe 12 is one of the numbers that breaks it, so try 12. Or just show me some manipulations of my code that will work with the 7 tries parameter! Thank You
//User thinks of a number.
//Computer has to guess within 7 tries.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
usingnamespace std;
int main(){
string hint;
srand(time(0));
int max = 101;
int min = 0;
int tries = 0;
cout << "Think of a number between 1 and 100." << endl << endl;
do{
int Number = 30 % (max - (min + 1))+ (min + 1); // Algorithm
cout << "The computer's guess is " << Number << endl << endl;
cout << "Is the guess high, low, or correct?: "; // get's hint
cin >> hint;
if (hint == "high"){ // the user input "high"
cout << "The guess was too high." << endl << endl;
max = Number;
tries++;
}
if (hint == "low"){ // the user input "low"
cout << "The guess was too low." << endl << endl;
min = Number;
tries++;
}
}
//computer guessed correctly, end game loop.
while (hint != "correct");
cout << "The computer's guess was correct after " << tries << " tries! Woohoo!!!" << endl << endl;
cout << "Thanks for playing." << endl << endl;
return(0);
} // end main
Also you use do{} whithout while. It should be do{}while(some_bool)
EDIT:
EDIT 2: made some changes
EDIT 3 : more changes, now works with 100
Try this one: