Click to reopen instructions.

You probably know the game 20 Questions: one player thinks of something and the other tries to guess it by asking up to twenty yes/no questions. In this assignment you’re going to write a program to play the guesser in a much simpler game: 3 Questions.

Your program should ask three yes/no questions, and then announce its guess with a line that starts with I guess — for example, I guess a dog!. That prefix is how the tests pick your guess out from anything else your program prints, so you’re free to print a welcome message before the questions or a “play again?” line after the guess. After each question, read the player’s answer with IO.readln(); the tests always answer with exactly a capital Y or N. (Whether your program also accepts y, yes, etc. from a human player is up to you.)

You can play your own game any time: click ▶ Run and type your answers in the terminal on the right. That’s the best way to build it — make the first question work, play it, then grow the tree.

The key to playing well: each question should depend on the answers so far. If “Is it an animal?” gets a Y, the natural follow-up is something like “Is it a mammal?” — but after an N you’d want a completely different question. There’s no information in a question whose answer you can already predict. So a full game is a tree: one first question, two possible second questions (one for each answer to the first), and four possible third questions — seven different questions in all, leading to 2 Ă— 2 Ă— 2 = 8 different final guesses.

That’s also the shape of the code: an if/else on the first answer, each side asking its own second question and containing its own if/else, and so on. The code will get repetitive — that’s okay for now.

When you want to check your work, switch to the Tests tab on the right and click ▶ Run again. The tests play all eight games against your program, answering Y Y Y, then Y Y N, and so on through N N N. When a test fails, the “got” column shows the whole conversation, with the answers your program was given marked with «.

Put definitions here.

Revisions:

Errors