0
Computer Guessing Program
Hey guys, I am working through a book called Learn c++ through game programming. The program has asked me to write a program that will allow a user to input a number and have the computer guess their number through repetitive random attempts. I have written the program here on sololearn to see If you guys could give me advice. Does this program look like it should work? The sololearn app times out every time I attempt it. Would it work if I tried it outside of sololearn? Anyway here it is. https://code.sololearn.com/cgWOfOwjvduT/#cpp also any ideas on how to make the computer guess more efficiently like to start by guessing random numbers in intervals of 10 until it gets down to it?
5 Respostas
+ 1
I'll look at it and add comments on the code.
+ 1
Never mind, you'd have to save the code first for me to comment.
First thing is that your next guesses look suspicious. You really need to do more to make sure you guess in the correct range.
Think about a human guessing. At the beginning, your upper bound is the max value and your lower bound is the min value. Every guess that is more than the value changes the upper bound. For example, once you know 85 is too high, you know that the number must be strictly less than 85 (if it was the first guess, number is between 1 and 84). Every guess that is less than the value changes the lower bound. For example, once you know 20 is too low, you know that the number must be strictly greater than 20 (if it was the first guess, number is between 21 and 100).
Right now, your code does not keep track of the bounds determined by previous guesses. I'd start there. Then update the bounds based on your current guess. Finally, write one place where you set the next guess based on the current bounds. It should look kinda like this:
guess = lowerBound + rand() * (upperBound - lowerBound);
For the record, there's another strategy for a computer to guess a value in a range that is faster than this random strategy. It is called Binary search and you might find it interesting.
+ 1
thanks. I am just learning and this is from chapter two of my book so my knowledge base to work with is low. I have not learned anything about upper bounds but your right. I've actually been trying to figure out how to make it guess more effectively.
+ 1
That's good. Keep working and keep learning. You don't get better without practice.
0
thanks