Skip to main content
GameDev.net gamedev.net
🔒 Locked

help with a program

Started by crowleyhammer Oct 22, 2006 at 1:23 PM 4 replies 1.4k views
Original Post
crowleyhammer
crowleyhammer
ok im using Dev-C++ and ive just created a guess the number game,i now need to find a way to allow the player to pick a number and the computer to guess it, if someone could post the code w/o telling me why it works that would be more helpfull because working it out will be more educational for me,thanks for your time.
http://crowleyhammer.blogspot.comMy journey into programming games
Samsonite
Samsonite
Hi there!

A solution to your problem would be to inverse it. You already know how to generate random numbers, and therefore you can use it to make the computer generate a random number and then check to see if the generated number is equal to the number the user has set. Try it.

Hope this helps!
Hope I was helpful. And thank you if you were!
crowleyhammer
crowleyhammer
i was thinking that,but i thought it will generate numbers too high,but of course you can set the largest number it can generate a random number from.
thanks for your help.
http://crowleyhammer.blogspot.comMy journey into programming games
Samsonite
Samsonite
Yes, but remember that the computer already "knows" the answer(the user input the number to the computer), your job as a programmer is to hide that. So if the user inputs 78. Then make the computer generate numbers between 1-100. If the user inputs 999, make the computer generate numbers between 900-1000 etc.

Good luck!
Hope I was helpful. And thank you if you were!
crowleyhammer
crowleyhammer
well so far i have the computer guessing my number and and me being able to tell it if its right or not,all i need to do is track its guesses and also tell it whether to be higher or lower,the tracking should not be a problem,but how do i go about telling it higher or lower?
http://crowleyhammer.blogspot.comMy journey into programming games
crowleyhammer
crowleyhammer
ok i need help working out how to tell the computer its too high or too low,

thanks in advance

#include
#include
using namespace std;

int main()
{
int number;
int tries = 0, guess;

cout << "\tWelcom to Guess My Number VS the computer!\n\n";
cout << "Please select a number ";
cin >> number;

char again = 'n';
while (again == 'n')
{
srand(time(0));
int guess = rand() % 100 + 1;

cout << guess;
cout << "\nIs this your number? (y/n)";
cin >> again;
++tries;
}

cout << "\nYes i won!" << tries << " guesses!\n";
system("PAUSE");
return 0;
}
http://crowleyhammer.blogspot.comMy journey into programming games

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.