Sign in to follow this  
m3rcyful ag3nt

switch roles

Recommended Posts

m3rcyful ag3nt    158
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 this post


Link to post
Share on other sites
wasted_druid    692
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

User answer = Higher

computer guess = 750

User answer = Higher

Computer guess = 875

User answer = Lower

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

Computer guess = 760

User answer = higher

Computer guess = 770

User Answer = Lower

Computer guess = 761

User Answer = Correct

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

Share this post


Link to post
Share on other sites
Sneftel    1788
The algorithm you should use is known as "bisection search". Google for it.

Share this post


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

Share this post


Link to post
Share on other sites
Zahlman    1682
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this