This is a variation on twenty questions -- the program flows like this:
- The user thinks of an animal
- The computer asks a yes/no question about that animal
- The user answers the question
- Repeat step 2 until the computer runs out of questions and guesses an animal
- If the computer got it right, it wins!
- If the computer got it wrong, then the player wins, and...
- The player writes a new question that would distinguish the (wrong) guess from the (correct) animal, and the computer adds that to its list of questions for next time
- data structures
This game demonstrates a classic use of a binary tree. At each node in the tree, there are two choices, each of which leads to a separate tree; eventually you reach an answer node, and that's the guess.
Here is a diagram:
[Does it fly?] YES NO / \ [Does it tweet?] [Does it bark?] YES NO YES NO | | | | [Bird] [Bat] [Dog] [Cat]