Jump to content
  • Advertisement
Sign in to follow this  

Noob Here, need help with "Guess My Number"

This topic is 1304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have just recently purchased Michael Dawson's book, "Beginning C++ Game Programming" but have come to a road block upon completing chapter two. (I have zero previous knowledge of any programming) In the end of chapter 2, one of Dawson's exercises is as follows: "3. Write a new version of the Guess My Number program in which the player and the computer switch roles. That is, the player picks a number and the computer must guess what it is." On his CD, Dawson supplies the code for the OPPOSITE program (where the user guesses the computer's number). I'm trying to rearrange steps and place the user in the computer's shoes, though I'm stumped...Here's what I _THINK_ I know... -------------------- 1. I need to have the user input an integer number to be guessed... "cin >> theNumber;" 2. I need to have the computer choose random numbers using the "rand()" 3. I need to use a 'do' accompanied by a series of 'if's...the loop must wait for the user to hit enter (or some key), then the computer will choose a random number from say...1-100. If the number is too low, it gives the computer a message saying so, same with if its too High. 4. With each new number guessed, i need to increase the number of tries by 1. ++tries; (where 'tries' is the integer value of the number of tries performed) 5. Need to end loop when computer finds number....also display a "Congrats!" or something -------------------- My biggest problem is understanding how to have the computer "guess" or choose a random number...though I'd like to view the entire program's code Any basic examples of how to accomplish this program are appreciated. This being my first post, I hope not to barge in on this new community if I'm asking too much so soon.... (this post probably could have been alot shorter too lol)

Share this post


Link to post
Share on other sites
Advertisement
Well, if you want to have the computer guess a random number between 1-100, you would use the rand function like so:

int com_guess = rand() % 100;

To clarify, by default, rand() chooses a random number between 0 and RAND_MAX. RAND_MAX is around 30,000 I think. Anyway, there is a way to make the random number generator pick between 0 and a number you define. If you put a % symbol, followed by the max number you want the random number generator to go up to, it will work.

I hope that answers your question.

Share this post


Link to post
Share on other sites
Well...first you have to write a function that picks a random number between to numbers.

int Random(int high, int low);
there are a few ways to do this, but the easiest is as follows

int Random(int high, int low)
{
//this selects a radnom number between high and low
return rand() % (high - low + 1) + low;
}

this will allow you to choose random numbers

to allow for a more random number you should seed rand() which will create unique random numbers every time the program is run as oppose to the same set.

you can do this by writing this at the top of main(as a seperate functino call or not)

srand((unsigned int)time(0));


after you do this you can follow the following general idea.

have the cpu chose a number...if it is too high or too low have it select a new number accordingly.

for example
user types 30

cpu selects(1-100) 50
too high
cpu slects(1-50) 20
too low
cpu selects(20-50) 30
Bells and whistles


Hope this helps.

Regards, Me

Share this post


Link to post
Share on other sites
OK...thanks for all the help so far...this is where I am at the moment.
Lines marked by "*****" is where I know a problem exists...

// Guess My Number 2.0
// Have computer guess your number

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int theNumber;
int comGuess = rand() % 100 + 1;
int tries;

cout << "\tWelcome to Guess My Number 2.0!\n\n";

cout << "Please enter a number to be guessed: ";
cin >> theNumber;

do
{
cout << "\n\nPress Enter to see the Computer's guess...";
*****How do I get the program to wait for enter (or a key) to be pressed?*****

return comGuess;
cout << "The Computer's Guess: " << comGuess << ".\n";
++tries;

if (comGuess > theNumber)
cout << "Too high!\n\n";

if (comGuess < theNumber)
cout << "Too low!\n\n";

} while (comGuess == theNumber);

cout << "\nThat's it! The Computer got it in " << tries << " guesses!\n";

}

-----------------------------------

I doubt that's everything as I haven't had a successful run yet.
I'm pretty sure I will still have problem's getting the computer to choose a random number. Wasn't clear how to seed rand() successfully so I could be attempting some noob way to generate and return random numbers.

Any additions to making my learning program more complete are welcome :D

Share this post


Link to post
Share on other sites
Quote:
Original post by Stompy9999
Well, if you want to have the computer guess a random number between 1-100, you would use the rand function like so:

int com_guess = rand() % 100;

To clarify, by default, rand() chooses a random number between 0 and RAND_MAX. RAND_MAX is around 30,000 I think. Anyway, there is a way to make the random number generator pick between 0 and a number you define. If you put a % symbol, followed by the max number you want the random number generator to go up to, it will work.

I hope that answers your question.


That would give you a number from 0 to 99, not 1 to 100. As chris_bell has done, you would need:

int com_guess = rand() % 100 + 1;

Share this post


Link to post
Share on other sites
Can anyone figure out what's wrong with this program? Closes immediately after user enters "theNumber".
I tried using system("pause") where i have "cout << "\n\nPress Enter to see the Computer's guess...";" but the program would just get to that new pause point and close afterward (not much further than the previous closing @ entering 'theNumber').
-----------------------------------------

// Guess My Number 2.0
// Have computer guess your number

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int theNumber;
int comGuess = rand() % 100 + 1;
int tries;

cout << "\tWelcome to Guess My Number 2.0!\n\n";

cout << "Please enter a number to be guessed: ";
cin >> theNumber;

do
{
cout << "\n\nPress Enter to see the Computer's guess...";
cin.get();
return comGuess;
cout << "The Computer's Guess: " << comGuess << ".\n";
++tries;

if (comGuess > theNumber)
cout << "Too high!\n\n";

if (comGuess < theNumber)
cout << "Too low!\n\n";

} while (comGuess == theNumber);

cout << "\nThat's it! The Computer got it in " << tries << " guesses!\n";

}

Share this post


Link to post
Share on other sites
It closes because you put return ComGuess after the wait for enter command. This will exit your program immedietly. You shouldn't be returning ComGuess anyways.

Share this post


Link to post
Share on other sites
Your problem is the line "return comGuess" that statement causes the program to exit with comGuess as the programs return value. This is likely not what you want.

Most likely what you want is to move the line "int comGuess = ..." into the do loop in place of your "return comGuess" line.

Share this post


Link to post
Share on other sites
thanks for that, i wasn't really sure what to do about obtaining a value each time for comGuess.
now for some reason, though, i'm getting an error on the 'while' line...though i see nothing wrong with it.

} while (comGuess == theNumber);

seems fine to me...

Share this post


Link to post
Share on other sites

This topic is 1304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!