Public Group

switch roles

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

Recommended Posts

Hey there I want to change the roles in the guess my number game. That is, the player picks the number and the computer must guess what it is. The other way is very clear (when the user has to guess a random number), but in the other way around is not very clear... Any ideas? Cheers

Share on other sites
I'd program the computer to pick the middle number in the range, find out if it's higher or lower. Then pick the middle number in the reduced range. Just for fun, when he gets to within about 100 of the number, I'd have him start below it and guess numbers going up in 10 unit increments, then when he's within 10 he'd go below and start in 1 unit increments and then he's there.

Example:

User number between 1 and 1000 = 761

Computer guess = 500

computer guess = 750

Computer guess = 875

*** Computer stops doing midpoint since his range is only 125 ***

Computer guess = 760

Computer guess = 770

Computer guess = 761

Computer response = "Woot! Look at me and my bad self!"

Share on other sites
The algorithm you should use is known as "bisection search". Google for it.

Share on other sites
Thanks for that Sneftel. I´ll make a search on that.
But my prob at the the moment is how do I programm the computer to pick a number in the range??

Share on other sites
Dead center of range: num = (MIN_RANGE+MAX_RANGE)/2
random number in range: num = MIN_RANGE+(rand()%(MAX_RANGE-MIN_RANGE))

Share on other sites
But do I have to generate a random number?? I mean, as I now the number can´t I just declare an int with the correct answer?

Cheers

Share on other sites
You don't want the program to know the correct answer right off - that defeats the purpose.

Instead, you just tell the user to think of the number, and then present guesses. No randomness is required in calculating the guess, although you could put some in if you want (it will hurt the computer's number-of-guesses in the long run though). For the first guess, you can just say "is it {fill in n/2 here}?" and prompt the user to indicate higher/lower/correct. Based on that feedback, you adjust the guess upwards or downwards for the next loop.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• Forum Statistics

• Total Topics
633654
• Total Posts
3013170
×