13
Practice
Create a game in which the server tries to guess the animal chosen by
user by repeatedly asking clarifying yes-no questions.
If the server suggests a wrong answer, it requests the user to provide
the animal name and then asks a different question. 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 required functions.
3. Check your implementation.
A possible dialog (between people):
— Is it a mammal? — Yes.
— Is it an elephant? — No.
— I give up. Who is it? — It’s a whale.
— How can we tell a whale from an elephant? — It lives in the ocean.
Task 1. It is convenient to present this information as a binary tree. Inner
nodes store questions, while leaf nodes store animal names. One of the
child nodes corresponds to “yes,” the other one corresponds to “no.”
Task 2. Between the function calls, it is required to pass the information
about the last node of the tree we have got to (the context of the dialog). For
example, we 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, issue 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