Back to DFS's Workshop Page
Back to Agenda Page


Animals Problem

This problem is a reworking of the Animals program in Integer Basic from the Apple ][+ distribution. It is a quasi-artificial-intelligence problem in that the more the program is run, the more it seems to "know".

The Game

The program asks a sequence of yes/no questions, eventually asking if the user is thinking of a specific animal. The accompanying image shows the structure of a starter data tree.

The program "knows" only two animals, shark and elephant. The question (1) is asked of every user. If the user answers "Yes", the program proceeds to ask if the animal is a shark (2); if the user answers "No", it asks if the animal is an elephant (3). In either case, if the user then answers "No" to the animal named, the program will ask for

If the animal, question, and answer are "cod", "Does it attack people?", and "No", respectively, the tree would be transformed to look as the one on the right.

The Data File

The starter data file could be as follows:

3
Q|Does it live exclusively in water?|2|3
G|shark
G|elephant

The "3" in the first line specifies the total number of lines to follow. The first field in each of the remaining lines indicates whether the record contains a non-terminating question, "Q", or a guess, "G", in an "Is it a(n) ...?" question. The next field is either the question or the terminating animal guess, respectively. Each question record has two additional fields which specify which records to go to for the next question or terminating guess. The third field indicates the next record when the answer is "yes" and the fourth when the answer is "no".

When the animal guessed is incorrect, the following changes are made to the array containing the data from the file.

  1. Copy the record of the incorrect animal to the end of the array.
  2. Create a new record at the new end of the array which contains the new animal as an answer.
  3. Replace the copied record with a new question record using the new question and the numbers of the records for the animal guesses just placed at the end of the array.

These actions would result in the following data file when the information in the array is written to disk.

5
Q|Does it live exclusively in water?|2|3
Q|Does it attack people?|4|5
G|elephant
G|shark
G|cod

The Programming Task

Almost any programming problem lends itself to multiple solutions. This game is not an exception. Here, however, the programming options can be dictated by the medium used.

Command Line Version

As a program run from the Command Line, the solution can be an event-loop design. Here is a list of functions which could prove useful and a pseudocode for the program.

loaddata()
printlist()
savedata()
getcharinlist($prompt, $charlist)
getnnstring($prompt)

Pseudocode

  1. Load data from disk file
  2. Introduce program to user, providing list of animals currently known
  3. Event loop
    1. Give user the option of viewing the animal list, playing, quitting
    2. If "Play"
      1. Starting with the first Q, ask questions until an animal guess is encountered
      2. Guess the animal
        • If correct, give option to play again
        • If not, obtain and process new-animal info, saving it to file
    3. If "Quit", exit
    4. If "View", list currently known animals

Web Page Version

Using a web page for interaction with the user causes this to be a problem in recursion, because the program needs to call itself each time the user answers one of the data file questions.

Information about previous questions and answers must be made available to subsequent executions of the script. This can be done by writing the information to a disk file or by including it in an HTML form as hidden inputs.

The following list of functions that I used may be of some help.

loaddata()
printlist()
savedata()
function getynchar($prompt) // print prompt and call printynform()
function printynform() // print yes/no form
function printprevious() // print initial instructions, animals known (printlist()), and previous Qs and As
function processnewanimal()

To see how this could work, view this possible solution.


© 2006 DFStermole
Created 24 Jan 06
Last Modified 18 Feb 06