13
Practice+
Create a game in which the server tries to guess the animal chosen by
the user by asking a series of clarifying questions that can be
answered with a “yes” or a “no”.
If the server suggests a wrong answer, it requests the user to provide
the animal name, and then asks how is it different from its
suggestion. This new information is registered for use in the next
games.
1. Create a table for data representation.
2. Design an interface and implement all the necessary functions.
3. Check the implementation.
A possible dialog (between humans):
— Is it a mammal? — Yes.
— Is it an elephant? — No.
— I give up. What is it? — It’s a whale.
— How is it different from an elephant? — It lives in water.
1. The data falls nicely into a binary tree structure. Inner nodes store
questions, while leaf nodes store animal names. One of the child nodes
corresponds to the answer “yes”, the other to the answer “no”.
2. Between the function calls, you need to pass the information about the
last node of the tree the user gets to (the context of the dialog). For
example, you could have the following functions:
●
start the game (no input context):
FUNCTION start_game(OUT context integer, OUT question text)
●
continue the game (get the answer, ask the next question):
FUNCTION continue_game(
INOUT context integer, IN answer boolean,
OUT you_win boolean, OUT question text)
●
end the game (add information about another animal):
FUNCTION end_game(
IN context integer, IN name text, IN question text)
RETURNS void