Public Group

# Computer ai

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

## Recommended Posts

Hey,
i am currently learning c++ and i'm learning from a book. at the end of every chapter there are a few questions that will help you understand what you learned that chapter. At the end of chapter 2 there was a question to make a program in which you type in a number and the computer guesses it. Using , variables, if statements, else clauses, do, while, switch, and game looping, how would i proced in doing this i really cant think of a way. is there anyone out there that could help me with this, and aside from giving me code or something, just kinda poke me in the right direction.

thanks

##### Share on other sites
At all times your program should have some lower bound and some upper bound for your number. It should then ask you if the number is larger than something around the middle of the range, and depending on the answer, either update the lower bound or the upper bound. When the lower bound and the upper bound meet, the number is known.

##### Share on other sites

At all times your program should have some lower bound and some upper bound for your number. It should then ask you if the number is larger than something around the middle of the range, and depending on the answer, either update the lower bound or the upper bound. When the lower bound and the upper bound meet, the number is known.

Thanks for your help however im not sure what the bout is

##### Share on other sites
By bout do you mean bounds? The bounds is the range of numbers that can be picked from. For instance if someone asked you, "Pick a number between 1 and 5", we could pick 1, 2, 3, 4 , or 5 within these given *bounds* or to a computer something like "rand (1, 5)".

So just imagine the computer as the person asking the question, it needs to know what the lowest possible number is (usually 1 in a guessing game) and which is the highest (commonly 100). Then the computer would want to guess, or randomize a number that you would give a "too high" or "too low" feedback to. Like alvaro was saying you would then alter the upper or lower bounds accordinly on the randomize function. If for instance the machine guessed 10 and you input that was too high, the random function would go something like "rand (1,100)" to "rand (1, 9)".

You could take the program a step further and let it fulfill both roles. The person guessing the number and the one who has it. In this instance using a for loop would allow you to have a function check back and forth until the correct number is chosen.

##### Share on other sites
I would imagine the interface to the user would be something like this (character is bold italics is the input from the user):

Enter a Number between 1 and 100> 32
Thinking...
I guess 50; Is this (0) correct, (1) too high, or (2) too low> 1
Thinking...
I guess 25; Is this (0) correct, (1) too high, or (2) too low> 2
Thinking...

And it continues until the computer guesses your number. Thinking about how you would program that.

##### Share on other sites
This would be fun to write recursively.

Edit:

import random low = 1 high = 100 def set_number(): n = int(raw_input("Please type in a number between 1 and 100.")) if n < 1 or n > 100: print "That is not a valid number." return set_number() return n def guess_number(): global number global low global high guess = random.randint(low, high) a = raw_input("Is %d your number? Higher:(h), Lower:(l), Correct:(c)" % guess) if a == 'c': print "Awesome!!! Quitting program." return elif a == 'h': low = guess elif a == 'l': high = guess return guess_number() number = set_number() guess_number()

##### Share on other sites
A person who just wants to make a guessing game, and you decide you want to show him a recursive solution?

OP, please dis-regard this solution. You should even be worrying about recursion. Just solve the problem using loops. in the future, when you have a good understanding, i'd take a look at recursion; although, in my experience, there are few good solutions that use recursion.

This would be fun to write recursively.

Edit:

import random low = 1 high = 100 def set_number(): n = int(raw_input("Please type in a number between 1 and 100.")) if n < 1 or n > 100: print "That is not a valid number." return set_number() return n def guess_number(): global number global low global high guess = random.randint(low, high) a = raw_input("Is %d your number? Higher:(h), Lower:(l), Correct:(c)" % guess) if a == 'c': print "Awesome!!! Quitting program." return elif a == 'h': low = guess elif a == 'l': high = guess return guess_number() number = set_number() guess_number()

##### Share on other sites
If you need to see a simple solution in C++, I can provide one.

This is a fun C++ implementation, but not for beginners:#include <iostream> #include <algorithm> #include <boost/iterator/counting_iterator.hpp> bool compare(int i, int j) { while (1) { if (i==-1) std::cout << "Is your number less than " << j << "?\n"; else std::cout << "Is your number more than " << i << "?\n"; char answer; std::cin >> answer; std::cin.ignore(100000,'\n'); if (answer=='y' || answer=='Y') return true; if (answer=='n' || answer=='N') return false; } } int main() { typedef boost::counting_iterator<int> CI; CI number = std::lower_bound(CI(1), CI(100), -1, compare); std::cout << "Your number is " << *number << '\n'; } 

##### Share on other sites
Let's keep a lid on the 'cute' solutions requiring other languages/knowledge levels in For Beginners, shall we? The OP asked for advice rather than code - code dumping Python/boost isn't very helpful (and we already know you guys can solve the problem).

##### Share on other sites

Let's keep a lid on the 'cute' solutions requiring other languages/knowledge levels in For Beginners, shall we? The OP asked for advice rather than code - code dumping Python/boost isn't very helpful (and we already know you guys can solve the problem).

Oh, you are no fun.

But yeah, you are probably right. In my defense, I did provide what I thought was the kind of advice he needed in my first post.

It would be good to hear back from the OP, see what his best attempt so far looks like and what problems he has encountered.

1. 1
2. 2
3. 3
Rutin
12
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013378
×