Hi all, i started to practice true those excersises that i found on this forum. I camed to Bracketing Search, and i did manage to solve first two modifications, but i just can't figure out how to make a computer guess the number in 7 attempts. My head is gonna burst lol, for two days i've been trying to figure it out, writing on paper, trying empiricly, nothing. My code will find the number for sure, but not in maximum of 7 attempts in any scenario.
Any help and guidance is very much appriciated. Btw i also feel my code is complete rubish, seams as too many code lines for the task, any suggestion?
Bracketing Search
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
psudo random numbers
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 to output how many guesses it took the user to correctly guess the right 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.
my code
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
|
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int main() {
int mode, times = 1;// sets mode for switch case
int userNumber; //takes user input
int divider; //divider
string directions; //
srand(time(0));
int computerNumber = 1+(rand()%100); // generates random number between 1-100
cout << "Enter a number between 1-100 for computer to try and guess it\nNumber: ";
cin >> userNumber;
if(computerNumber<userNumber){
mode = 0;//if computer number is smaller than user number do case 0
}
else if (computerNumber>userNumber){
mode = 1;//if computer number is bigger than user number do case 1
}
divider = computerNumber;
switch(mode){
case 0:
while(computerNumber != userNumber){
times++;
cout << computerNumber;
cout << ":";
cin >> directions;
if(directions == "L"){ // too low
computerNumber += divider;
if (computerNumber > 100) goto here;
}else if(directions == "H"){ // too high
here:
here2:
if(divider != 1){ //limits divider variable to hold minimum of 1
divider /= 2;
}
computerNumber -= divider;
if(computerNumber > 100) goto here2;//prevents computer from displaying number greater than 100 because 1-100
}
}
break;
case 1:
while(computerNumber != userNumber){
times++;
cout << computerNumber;
cout << ":";
cin >> directions;
if(directions == "L"){ // too low
divider /= 2;
computerNumber += divider;
}else if(directions == "H"){ //Too high
if(divider != 1){ //limits divider variable to hold minimum of 1
divider /= 2;
}
computerNumber -= divider;
if(computerNumber == 0){//prevents computer from "saying" 0 because the task was to find a number between 1-100
computerNumber++;
}
}
}
break;
}
cout << computerNumber << ": Win!" << endl << endl;
cout << "Computer found the number, the number was " << computerNumber << "!" << endl;
cout << "It took " << times << " attempts for computer to guess it!" << endl;
}
|
Enter a number between 1-100 for computer to try and guess it
Number: 1
54:H
27:H
14:H
8:H
5:H
4:H
3:H
2:H
1: Win!
Computer found the number!
It took computer 9 attempts to guess the right number!
|