Back to DFS's Workshop Page
Back to Agenda Page
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 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 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.
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
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.
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
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.