() The ANIMAL program

advertisement
Page 1 of 5
ANIMAL
Early Line-Numbered BASIC Game: Computer Guesses the Animal
In this game you think of an animal and the computer asks you questions and tries to guess the type of animal.
If the computer guesses incorrectly, it will ask you for a question that differentiates the animal it guessed from
the one you were thinking of. In this way the computer "learns" new animals. Questions to differentiate new
animals should be input without a question mark. They should be of the type "Does it do so-and-so" or "Does
it have so-and-so" which can be answered yes or no. The program starts initially knowing only FISH and
BIRD.
0010
0020
0030
0040
0100
0102
0105
0110
0120
0200
0205
0210
0211
0212
0215
0220
0230
0240
0500
0510
0520
0530
0600
0610
0620
0630
0640
0650
0660
0670
0690
0700
0710
0720
0800
1000
1010
1020
1030
1040
2000
9000
DIM A$(20), Q$(20), Y(20), N(20)
A$(1) = "FISH": A$(2) = "BIRD"
Q$(1) = "DOES IT SWIM"
Y(1)=-1: N(1)=-2: Q=1
PRINT
PRINT "ARE YOU THINKING OF AN ANIMAL";: INPUT R$
GOSUB 1000
IF X<0 GOTO 2000
IF X=0 GOTO 100
I=1
IF I<0 THEN G=-I: GOTO 500
PRINT Q$(I);
INPUT R$
GOSUB 1000
J=I: W=X
IF X=1 THEN I=Y(I)
IF X=-1 THEN I=N(I)
GOTO 205
PRINT "MY GUESS IS THAT IT IS A "; A$(G)
PRINT "AM I CORRECT";: INPUT R$: GOSUB 1000
IF X=0 GOTO 510
IF X=1 THEN PRINT "BOY, AM I SMART.": PRINT: GOTO 100
PRINT "I GOOFED. WHAT ANIMAL ARE YOU THINKING OF";: INPUT T$
Q=Q+1: A$(Q+1) = T$
IF W=1 THEN Y(J)=Q
IF W=-1 THEN N(J)=Q
PRINT
PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH"
PRINT "A "; T$; " FROM A "; A$(G)
INPUT Q$(Q)
PRINT "FOR A "; T$; " THE ANSWER WOULD BE ";
INPUT R$: GOSUB 1000: IF X=2 GOTO 690
IF X=1 THEN Y(Q)=-(Q+1): N(Q)=I
IF X=-1 THEN N(Q)=-(Q+1): Y(Q)=I
PRINT: GOTO 100
X=0: L$=LEFT$(R$,1)
IF L$="Y" THEN X=1
IF L$="N" THEN X=-1
IF X=0 THEN PRINT "PLEASE ANSWER YES OR NO"
RETURN
PRINT "OK, GOODBYE"
END
Page 2 of 5
Sample Dialog
Computer:
Player:
ARE YOU THINKING OF AN ANIMAL ?
YES
Computer:
Player:
DOES IT SWIM ?
NO
Computer:
Player:
MY GUESS IS THAT IT IS A BIRD
AM I CORRECT ?
NO
Computer:
Player:
I GOOFED. WHAT ANIMAL ARE YOU THINKING OF ?
KANGAROO
Computer:
Player:
PLEASE TYPE IN QUESTION THAT WOULD DISTINGUISH
A KANGAROO FROM A BIRD.
Here the human operator
DOES IT FLY
teaches the computer a new
Computer:
Player:
FOR A KANGAROO THE ANSWER WOULD BE ?
NO
Computer:
Player:
ARE YOU THINKING OF AN ANIMAL ?
YES
Computer:
Player:
DOES IT SWIM ?
NO
Computer:
Player:
DOES IT FLY ?
NO
Computer:
MY GUESS IS THAT IT IS A KANGAROO
AM I CORRECT ?
YES
Initially, the computer "knows"
only two animals: fish and bird. If
the animal does not swim, it must
be a bird.
animal -- the kangaroo.
Player:
Computer:
BOY, AM I SMART.
ARE YOU THINKING OF AN ANIMAL ?
etc.
This time, the computer is smart
enough to distinguish a kangaroo from
a bird, based on the question "Does it
fly" that it learned in the preceding
dialog.
Page 3 of 5
Explanation of Some BASIC Syntax
In this very early version of BASIC:
 every statement line must have a distinct line number.
 one line can include multiple statements, separated by colons (:). [example: line 20]
 the names of string (text) variables end with the dollar sign ($). [example: R$ in line 700]
 the names of variables that do not end with a dollar sign are integers. [example: X in line
110]
 elements of an array are indicated by subscripts in parentheses [example line 210: Q$(I)
denotes element number I of the string array Q$], but in a dimension line [example: line
10] the number in parentheses denotes the size of the array.
 all variables are global. They can be read from or written to anywhere in the program.
 each input statement [example: line 211] causes the computer to wait for a response from
the program user (game player), and then store the player's response into the specified
string variable [R$ in line 211].
 each subroutine logically ends with a RETURN statement. [example: line 1040]
 the LEFT$ function produces the leftmost n characters of a string variable. [example in line
1000: LEFT$(R$,1) produces the leftmost character of the text stored in variable R$.]
Problems/Questions
1. Draw a graph showing the structure of this program. Label each branch of the graph with a
distinct letter, and show the BASIC line numbers corresponding to each letter. Determine the
cyclomatic complexity and other key metrics of your graph.
2. Program execution is sequential from one statement line to the next, except for GOTO or GOSUB
statements. In later versions of BASIC, statement lines do not need line numbers unless they are
targets of a GOTO or GOSUB statement. From which statements can line numbers can be
eliminated? In modern BASICs, line numbers can be replaced by alphanumeric line labels, adding to
program readability. Suggest meaningful alphanumeric labels for the statement lines that need labels.
3. Except for arrays [line 10], variables need not be predeclared. Make a list of all variables
(including arrays) used by this program, showing variable name, and type (string, or integer).
4. For each variable name in problem #3, suggest a longer (more descriptive) name. Also give a
brief (about one line) description of the variable. These names and descriptions could be useful to
maintenance programmers who might be assigned the task of modifying the program.
5. Rewrite this program in structured (S5) form using a modern version of BASIC, or using a
pseudo-BASIC.
6. BASIC allows comments (remarks) by prefacing them with the keyword REM or with an
apostrophe ('). Augment your answer to problem #5 by adding comments where appropriate.
7. Can you spot potential errors or bad behavior (bugs) in this program? Can you spot provisions in
the program which are intended to cope with possible errors by the program user (game player)?
8. For each of the variables identified in problem #3, identify where in the program the variable's
value is (a) initialized, (b) changed, and (c) read (used).
Page 4 of 5
Some Answers to Questions
2) Only line numbers 100, 205, 500, 510, 690, 1000, and 2000 need be retained.
3, 4)
A$( )
Q$( )
Y( )
N( )
Q
animal names
string array
distinguishing questions
string array
next questn # if this one is answered YES
next questn # if this one is answered NO
number of stored questions
integer
R$
X
I
G
J
W
T$
L$
user's response
response code
index of question to ask
index of guessed animal
new animal name
first character of user response
string
integer
integer
integer
integer
integer
string
string
This program works by building a list of questions to ask. Q$( ) and two sets of links Y( ) and N( ) depending on what
answer is received to each question. In line 205, a positive link number gives the next question number, and a negative
link number gives, when negated, the animal to guess (line 500). If the guess is incorrect (line 600) the lists are updated
(lines 710-720), and the guessing/learning process begins again at line 100.
7) Array sizes are only 20. Lower-case user input is not recognized. Line 700: how can X ever take
on the value 2? If user responds with a null (line 600, 670) it is undetected. Line 1030 re-prompts
user if answer does not begin with "Y" or "N".
Page 5 of 5
Download