Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello
I made this program in my book that creates a random number between 1 and 100 and the user, trough "too hight" or "too low" comments must guess it.
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>using namespace std;
int main(){
srand(time(0));
int theNumber = rand() % 100 + 1;
int tries = 0, guess;
cout << "\tWellcome to Guess my Number\n\n";
do{
cout << "Enter a guess:";
cin >> guess;
cout << endl;
++tries;
if(guess > theNumber){
cout << "Too High\n\n";
}
if(guess < theNumber){
cout << "Too Low\n\n";
}
}while(guess != theNumber);
if(tries < 20){
cout << "\tYou got it in just " << tries << " tries!!";
cout << " You lucky b---tard\n";
}
if(tries > 20){
cout << "You finnaly got it...pweff" << endl;
cout << "You took" << tries << " to do it" << endl;
}
system("Pause");
}When the user figures the number out it informs the user of the number of tries he used.
And then the book asked for me to do a program were the user picks a number and the computer must guess it, I made it myself ^^ lol
code:
#include <iostream>
#include <cstdlib>
#include <ctime>using namespace std;
int main(){
srand(time(0));
int computerNumber;
int theNumber;
int tries = 0;
cout << "\tWellcome to Guess my Number\n\n";
cout << "In this program you enter a number and see how many attempts the computer uses to figure out what number it is.\n\n";
cout << "Enter the number(1-100): ";
cin >> theNumber;
do{
computerNumber = rand() % 100 + 1;
cout << "The computer's guess is: " << computerNumber << endl;
++tries;
}while(computerNumber != theNumber);
cout << "The computer took " << tries << " tries to figure out what was the the number" << endl;
system("Pause");
}
its pretty simple actualy but its also pretty stupid beacause the computer sometimes takes more than 100 tries, ergo, it "tried" the same number various times. Now, what i wanted to do was some sort of AI in witch the pc didn't repeat the same number, can anyone give me any sugestions and,if you may, explain them?Best Regards
PS: Sorry for the long post...AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

Pretty simple, actually. Just make your 'min' and 'max' variables instead of constants. Then, adjust those variables based on a too high/too low test.

Sorry, but, as usual, can you show me?
I know i should make like
if(computerNumber > the Number){
but i dont know wha to put in here...
}
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

I could, but what fun would that be?
unsigned int min = 1, max = 100;
do{
if (max > min)
computerNumber = rand() + min % (max - min);
else
computerNumber = min;
cout << "The computer's guess is: " << computerNumber << endl;
++tries;
max = (computerNumber > theNumber) ? computerNumber - 1 : max;
min = (computerNumber < theNumber) ? computerNumber + 1 : min;
}while(computerNumber != theNumber);It took longer to compile that than write it.
Actually, you could write it so the computer would always get the number in 5 moves or less. But that's for me to know, and you to actually spend 10 minutes thinking about.
(EDIT: And even longer to get the colors right.)

I think I'l just follow my book for some more time before I make another post here, I still dont know enough to discuss with actual programers.
Thanks for the help^^
EDIT: I've been thinking by my self, as you said, insted of using all of your your code and i came up with this,
if(computerNumber > theNumber){
max = computerNumber;
}
if(computerNumber < theNumber){
min = computerNumber;
}
i think it makes sense, if a human typed a number and he got the comment "too high" the user would pick a smaller number therefore, if I make the max equal to the computer's "too high" guess it would not guess higher numbers than that one right?
I made the same with the min.but when I run the program sometimes...ok many times it even guesses numbers higher than 100, why does that happen?
PS: the first part is:
if(max>min){
computerNumber = rand() % max + min;
}else{
computerNumber = min;
}
cout << "The computer's guess is: " << computerNumber << endl;
++tries;
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |