Computing.Net > Forums > Programming > not so noob, but still noob

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

not so noob, but still noob

Reply to Message Icon

Name: Deimos
Date: November 5, 2007 at 16:05:39 Pacific
OS: Windows Xp professional S
CPU/Ram: CPU:2.6x2Ghz Ram:2048mbs
Product: Deimos's nr1
Comment:

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;



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: November 5, 2007 at 18:41:29 Pacific
Reply:

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.


0

Response Number 2
Name: Deimos
Date: November 5, 2007 at 23:47:10 Pacific
Reply:

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;


0

Response Number 3
Name: Razor2.3
Date: November 6, 2007 at 03:07:21 Pacific
Reply:

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.)


0

Response Number 4
Name: Deimos
Date: November 6, 2007 at 06:10:48 Pacific
Reply:

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;


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: not so noob, but still noob

NOT or ALL BUT www.computing.net/answers/programming/not-or-all-but-/13041.html

Javascript in ASP? www.computing.net/answers/programming/javascript-in-asp/5436.html

re: edit text control /r/n Win32 www.computing.net/answers/programming/re-edit-text-control-rn-win32/7108.html