summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/dialogl/animals.tex
blob: dcce0c32029494de2060326fc3e93238425421ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
%   Animals   (in TeX, no less!!!)
%
%   This is the program that uses a binary tree of questions to
%   guess the type of animal of which the user is thinking.
%
%   Andrew Marc Greene, March-April 1988
%
%%% modified May 1991, Michael Downes:
%%%   ---removed path names to leave bare file names
%%%   ---added animals2.tex so that you can repeat without
%%%      restarting TeX
%%%   ---added code so that lowercase and uppercase user
%%%      responses will both be recognized.
%
%   Moral support (i.e., ``You can't do that!  Show us!'')
%   provided by the Student Information Processing Board
%   of MIT.
%
% Instructions on running this program:
%
%     tex animals
%
% Think of an animal.  The program will try to guess your animal.
%
% You will be asked a whole bunch of yes/no questions.  This is a
% spartan implementation, so answer with a capital Y or N.  When
% the program finishes going through its tree, it will either have
% guessed your animal or it will ask you to enter a question that
% it can ask to differentiate between your animal and its guess.
% It will then ask you which one is `yes.'

% Here's where I declare all my variables, etc.
%
% ``curcode'' is the current index into the data file.
% ``temp'' is a temporary holding variable.
% ``lc'' is a loop counter
% ``ifamg'' is a general-purpose flag.  amg are my initials.
% ``ifreploop'' controls loop repetitions.
% ``ifmainlooprep'' controls repetitions of the main loop.
% ``inp'' is the input file.
% ``outp'' is the output file.
% ``amgY'' and ``amgN'' are character constants.  Why I did it this way I
%                        don't remember.
%
\newcount\curcode\curcode=1\newcount\temp\temp=0\newcount\lc
\newif\ifamg\newif\ifyn\newif\ifreploop\newif\ifmainlrep
\newread\inp\newwrite\outp\def\foo{}
\def\amgY{Y}\def\amgN{N}

% The data file consists of records stored in the following format:
%
% Record Number <newline>
% Question <newline>
% If-Yes-Goto-Record Number <newline>
% If-No-Goto-Record Number <newline>
%
% The following routine scans the data file until it reaches the
% record requested in \curcode
%
\def\Scan{%
{\loop
 \global\read\inp to \foo
 \ifnum\foo=\curcode\amgfalse\else\amgtrue\fi
 \ifamg\read\inp to \foo\read\inp to \foo\read\inp to \foo
\repeat}}

% The following routine displays the question and waits for a Y or N
% answer
%
\def\Query{%
{\read\inp to \foo
\immediate\write16{}%
\message{\foo}%
\GetYN
\ifyn
  \read\inp to \foo\global\curcode=\foo\read\inp to \foo
\else
  \read\inp to \foo\read\inp to \foo\global\curcode=\foo
\fi
}}

% The following routines deal with the user's input.
% \vread (verbatim read) ignores <newline>s and makes <space>s normal
% \GetYN gets input and repeats until it gets a Y or N response.
%
\def\vread#1{{\catcode`\^^M=9\catcode`\ =12\global\read-1 to #1}}
\def\first#1#2\end{#1}
%
\def\GetYN{%
{\loop
\vread{\bar}%
\edef\bar{\uppercase{\def\noexpand\bar{%
  \expandafter\first\bar\empty\end}}}\bar
\reploopfalse
\if Y\bar\global\yntrue\else
 \if N\bar\global\ynfalse\else\replooptrue\fi\fi
\ifreploop
\immediate\write16{Hey, you!  Answer Y or N, please.}%
\message{Please enter Y or N --> }%
\repeat
}}

% The following routine is called if the ``Goto-Record'' is -1,
% meaning that the program didn't guess correctly and is clueless.
% It gets the new animal and the differentiating question, and
% modifies the data file.  Actually, it makes a modified copy of
% the file, then copies the temporary new one over the old outdated
% one.
%
\def\NewAnimal{
\immediate\write16{Well, I'm stumped.  What animal did you have in mind?}
\vread{\usersanimal}
\immediate\write16{OK.  What question would let me tell the difference?}
\vread{\userquery}
\immediate\write16{Is the answer to that question Yes or No if I ask about}
\message{\usersanimal? }
\GetYN
\curcode=-1
\Scan
\read\inp to \lastcode\lc=\lastcode
\closein\inp
%
% Open up the files.  These names are system-dependent.   *FLAG*
%
\openin\inp=animal.dat
\ifeof\inp \errmessage{Unable to open animal.dat!}\fi
\immediate\openout\outp=newaniml.dat
%
% Read through the inp file, copying all records that don't need to
% be changed, outputting modified versions of the changed ones (and
% discarding the old), and appending the new records.
%
{\loop
 \read\inp to \foo
 \amgtrue
 \ifnum\foo=\temp\amgfalse\fi
 \ifnum\foo=-1 \amgfalse\fi
 \ifamg\immediate\write\outp{\foo}%
  \read\inp to \foo\immediate\write\outp{\foo}%
  \read\inp to \foo\immediate\write\outp{\foo}%
  \read\inp to \foo\immediate\write\outp{\foo}%
  \amgtrue
 \else\ifnum\foo=\temp
  \immediate\write\outp{\foo}%
  \immediate\write\outp{\userquery}%
  \immediate\write\outp{\number\lc}%
  \global\advance\lc by 1
  \immediate\write\outp{\number\lc}%
  \read\inp to \animal\read\inp to \foo\read\inp to \foo
  \amgtrue
 \else
  \lc=\lastcode
  \ifyn\WriteUsers\WriteAnimal
  \else\WriteAnimal\WriteUsers
  \amgfalse\fi
 \fi\fi
 \ifamg
\repeat}
\closein\inp
\immediate\write\outp{-1}
\immediate\write\outp{\number\lc}
\immediate\closeout\outp
%
% Now copy the temporary file over the original one
%
% These filenames are also system-dependent.           *FLAG*
%
\openin\inp=newaniml.dat
\ifeof\inp \errmessage{Unable to open newaniml.dat!}\fi
\immediate\openout\outp=animal.dat
{\endlinechar=-1
  \loop
  \read\inp to \foo
  \immediate\write\outp{\foo}
  \amgtrue
  \ifeof\inp\amgfalse\fi
 \ifamg\repeat}
\immediate\closeout\outp
}
%
% This routine is called by NewAnimal and writes the record for
% the user's new animal
%
\def\WriteUsers{
\immediate\write\outp{\number\lc}
\immediate\write\outp{Is it \usersanimal?}
\immediate\write\outp{0}
\immediate\write\outp{-1}
\global\advance\lc by 1 }

% This one writes the modified old animal
%
\def\WriteAnimal{
\immediate\write\outp{\number\lc}
\immediate\write\outp{\animal}
\immediate\write\outp{0}
\immediate\write\outp{-1}
\global\advance\lc by 1 }

% Now we get into the main routine.
% It simply repeats the scan-query loop until it gets a 0 (right answer)
% or a -1 (wrong answer, I'm stumped), and calls the appropriate routine.
%
\def\maybecontinue{\input animals2.tex
  \maybecontinue}

\maybecontinue

% Ah, the joys of a job well-done.  We can now exit to the system, knowing
% that the world is a slightly better place for our efforts.
%
% The following line of code, probably the most profound in the entire
% program, sums up this philosophy of life in four characters.  The
% Puritan work ethic is embodied in this amazingly meaning-laden
% command designed by Donald Knuth.

\end