forked from staceycampbell/animal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimal.shar
774 lines (774 loc) · 16 KB
/
animal.shar
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#! /bin/sh
#
#Path: uunet!tektronix!tekgen!tekred!games
#From: games@tekred.CNA.TEK.COM
#Newsgroups: comp.sources.games
#Subject: v06i031: animal - guess the object game for SysV or BSD
#Message-ID: <3748@tekred.CNA.TEK.COM>
#Date: 21 Mar 89 20:13:00 GMT
#Sender: billr@tekred.CNA.TEK.COM
#Lines: 764
#Approved: billr@saab.CNA.TEK.COM
#
#Submitted-by: stacey@hcrvax.UUCP
#Posting-number: Volume 6, Issue 31
#Archive-name: animal
#
# [[This is a C implementation of the ``guess the animal'' game.
# It uses curses(3X) for the user interface, it was developed
# on Unix SysV.3 then ported to BSD and SysV.2.]]
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of shell archive."
# Contents: Makefile README an.c scr.c animal.6 example
# Wrapped by billr@saab on Tue Mar 21 11:10:44 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(548 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X# Animal - makefile
X#
X# System Dependencies - edit as appropriate for your machine
X#
X
X# Unix System 5.3
X
XCFLAGS= -O
XLIBS= -lcurses
X
X# BSD
X#
X#CFLAGS= -O -DBOGUS_GET -DBOGUS_BOX
X#LIBS= -lcurses -ltermcap
X#
X
X# Unix System 5.2 (possibly other less advanced SysV ports)
X#
X#CFLAGS= -O -DBOGUS_GET -DBOGUS_BOX
X#LIBS= -lcurses
X#
X
XLDFLAGS= -O
XOBJS= an.o scr.o
XSRC= an.c scr.c
XEXE= animal
X
X$(EXE): $(OBJS)
X cc $(LDFLAGS) $(OBJS) -o $(EXE) $(LIBS)
X
Xshar:
X xshar -v README animal.6 Makefile $(SRC) example > $(EXE).shar
X
Xman:
X nroff -man animal.6 > $(EXE).man
END_OF_FILE
if test 548 -ne `wc -c <'Makefile'`; then
echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'README' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(1105 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
XThis is a C implementation of the ``guess the animal'' game.
XIt uses curses(3X) for the user interface and standard buffered
XI/O routines for the file interface.
X
XThis version of the program has been ported to a Sun running BSD,
XSystem 5.3 on a VAX, Interactive's 386/ix port of System 5.3 and
XVX/VE System 5.2 running on a CDC Cyber.
X
XAn example file is included with the source and can be used by
Xthe command:
X
X$ animal example
X
XThe program will look the best on System 5.3 Unix, and the
Xworst on BSD. I'll fix any bugs people have, I don't
Xconsider the BSD curses library routines to be bugs per se
X(so don't complain to me about them).
X
XThe first time I ever saw this game was on an ancient DECUS
Xtape, it was a FORTRAN implementation. Since then I have
Xwritten a VAX Pascal version (1983), a C64 Basic version (1982),
Xand a VMS DCL version (1985) (recently posted to alt.sources).
X
XPLEASE: take time to edit the Makefile for your machine.
XBefore you go rushing off to post on the net complaining
Xthat something doesn't work make some attempt to fix the
Xproblem yourself, it may even be your fault!
END_OF_FILE
if test 1105 -ne `wc -c <'README'`; then
echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'an.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'an.c'\"
else
echo shar: Extracting \"'an.c'\" \(5668 characters\)
sed "s/^X//" >'an.c' <<'END_OF_FILE'
X/*
X * Animal - copyright HCR Corporation, Toronto, Canada, 1989
X *
X * author: Stacey Campbell
X */
X
X#include <stdio.h>
X#include <string.h>
X
X#define YES_ANS 1
X#define NO_ANS 0
X#define BAD_FILE 0
X#define BAD_OPTS 1
X#define BOTTOM "$$"
X
Xtypedef struct node_t {
X char *question;
X struct node_t *yes_p;
X struct node_t *no_p;
X } node_t;
X
Xmain(argc, argv)
X
Xint argc;
Xchar *argv[];
X
X {
X node_t *top;
X char buf[256];
X node_t *InitTop();
X node_t *LoadFile();
X void Query();
X int KeepGoing();
X void Usage();
X void SaveAnimals();
X void InitCurses();
X void EndCurses();
X void PutFileMsg();
X void PutMsg();
X
X InitCurses();
X switch (argc)
X {
X case 1 :
X top = InitTop();
X break;
X case 2 :
X top = LoadFile(argv[1]);
X if (top == 0)
X Usage(BAD_FILE, argv[1]);
X sprintf(buf, "using datafile: %s", argv[1]);
X PutFileMsg(buf);
X break;
X default :
X Usage(BAD_OPTS, argv[0]);
X break;
X }
X
X do {
X PutMsg("Think of something...");
X Query(top);
X } while (KeepGoing() == YES_ANS);
X
X SaveAnimals(top);
X EndCurses();
X return 0;
X }
X
Xstatic void SaveAnimals(top)
X
Xnode_t *top;
X
X {
X char fn[256];
X char buf[256];
X int GetAnswer();
X void DumpAnimals();
X char *FileQuestion();
X FILE *fp;
X
X
X if (YesNo(FileQuestion("Do want to save the data?")) == NO_ANS)
X return;
X do {
X strcpy(fn, FileQuestion("input a datafile name:"));
X if ((fp = fopen(fn, "w")) == NULL)
X {
X sprintf(buf, "Cannot open %s, try again?", fn);
X if (YesNo(FileQuestion(buf)) == NO_ANS)
X return;
X }
X } while (fp == NULL);
X DumpAnimals(fp, top);
X fclose(fp);
X }
X
Xstatic void DumpAnimals(fp, node_p)
X
XFILE *fp;
Xnode_t *node_p;
X
X {
X if (node_p == 0)
X fprintf(fp, "%s\n", BOTTOM);
X else
X {
X fprintf(fp, "%s\n", node_p->question);
X DumpAnimals(fp, node_p->yes_p);
X DumpAnimals(fp, node_p->no_p);
X }
X }
X
Xstatic node_t *LoadFile(fn)
X
Xchar *fn;
X
X {
X FILE *fp;
X node_t *top;
X void CollectAnimals();
X
X if ((fp = fopen(fn, "r")) == NULL)
X return 0;
X CollectAnimals(fp, &top);
X fclose(fp);
X return top;
X }
X
Xstatic void CollectAnimals(fp, node_pp)
X
XFILE *fp;
Xnode_t **node_pp;
X
X {
X static char read_buf[256];
X node_t *GetNode();
X
X if (fgets(read_buf, 256, fp) == NULL)
X return;
X read_buf[strlen(read_buf) - 1] = '\0';
X if (strcmp(read_buf, BOTTOM) == 0)
X return;
X else
X {
X *node_pp = GetNode(read_buf);
X CollectAnimals(fp, &(*node_pp)->yes_p);
X CollectAnimals(fp, &(*node_pp)->no_p);
X }
X }
X
Xstatic void Usage(status, str)
X
Xint status;
Xchar *str;
X
X {
X char buf[256];
X void PutFileMsg();
X void EndCurses();
X
X switch (status)
X {
X case BAD_FILE :
X sprintf(buf, "Cannot open datafile: %s", str);
X PutFileMsg(buf);
X break;
X case BAD_OPTS :
X default :
X sprintf(buf, "Usage: %s {data-file}", str);
X PutFileMsg(buf);
X break;
X }
X EndCurses();
X exit(1);
X }
X
Xstatic node_t *InitTop()
X
X {
X node_t *top;
X char fn[256];
X node_t *GetNode();
X char *FileQuestion();
X node_t *LoadFile();
X void Usage();
X int YesNo();
X
X if (YesNo(FileQuestion("Do you wish to read a datafile?")) == YES_ANS)
X {
X strcpy(fn, FileQuestion("Name of datafile?"));
X top = LoadFile(fn);
X if (top == 0)
X Usage(BAD_FILE, fn);
X }
X else
X {
X top = GetNode("was or is it a living organism");
X top->yes_p = GetNode("an ant");
X top->no_p = GetNode("a concrete brick");
X }
X return top;
X }
X
Xstatic void Query(node_p)
X
Xnode_t *node_p;
X
X {
X int GetAnswer();
X void NewAnimal();
X void PutQuestion();
X void PutFinalQuestion();
X void PutMsg();
X
X if (node_p->yes_p)
X {
X PutQuestion(node_p->question);
X if (GetAnswer() == YES_ANS)
X Query(node_p->yes_p);
X else
X Query(node_p->no_p);
X }
X else
X {
X PutFinalQuestion(node_p->question);
X if (GetAnswer() == YES_ANS)
X PutMsg("Yay!");
X else
X NewAnimal(node_p);
X }
X }
X
Xstatic void NewAnimal(node_p)
X
Xnode_t *node_p;
X
X {
X char animal[256];
X char question[256];
X char buf[256];
X node_t *old_object, *new_query, *new_object;
X char *malloc();
X node_t *GetNode();
X int GetAnswer();
X void GetQuestLine();
X void PutMsg();
X void PutQuestion();
X
X old_object = (node_t *) malloc(sizeof(node_t));
X *old_object = *node_p;
X GetQuestLine("What are you thinking of?", animal);
X new_object = GetNode(animal);
X PutMsg("Type a question such that:");
X PutMsg(" (a) a yes or no answer would distinguish");
X sprintf(buf, " between %s and %s", animal, node_p->question);
X PutMsg(buf);
X PutMsg("and");
X PutMsg(" (b) it does not explicitly mention either of these things.");
X GetQuestLine("question:", question);
X new_query = GetNode(question);
X *node_p = *new_query;
X
X sprintf(buf, "For %s the answer would be", animal);
X PutQuestion(buf);
X if (GetAnswer() == YES_ANS)
X {
X node_p->yes_p = new_object;
X node_p->no_p = old_object;
X }
X else
X {
X node_p->yes_p = old_object;
X node_p->no_p = new_object;
X }
X }
X
Xstatic int GetAnswer()
X
X {
X char input[256];
X void GetQuestLine();
X int YesNo();
X
X GetQuestLine("", input);
X return YesNo(input);
X }
X
Xstatic int YesNo(str)
X
Xchar *str;
X
X {
X int ret_val;
X void ToLower();
X
X ToLower(str);
X if (strcmp(str, "yes") == 0 || strcmp(str, "y") == 0)
X ret_val = YES_ANS;
X else
X ret_val = NO_ANS;
X
X return ret_val;
X }
X
Xstatic int KeepGoing()
X
X {
X void PutQuestion();
X int GetAnswer();
X void StartBold();
X void EndBold();
X
X StartBold();
X PutQuestion("Another query");
X EndBold();
X return GetAnswer();
X }
X
Xvoid ToLower(str)
X
Xchar *str;
X
X {
X while (*str)
X {
X if (*str >= 'A' && *str <= 'Z')
X *str += 'a' - 'A';
X str++;
X }
X }
X
Xnode_t *GetNode(question)
X
Xchar *question;
X
X {
X node_t *node_p;
X char *malloc();
X
X if ((node_p = (node_t *) malloc(sizeof(node_t))) == NULL)
X return NULL;
X node_p->question = malloc(strlen(question) + 2);
X strcpy(node_p->question, question);
X node_p->yes_p = 0;
X node_p->no_p = 0;
X return node_p;
X }
END_OF_FILE
if test 5668 -ne `wc -c <'an.c'`; then
echo shar: \"'an.c'\" unpacked with wrong size!
fi
# end of 'an.c'
fi
if test -f 'scr.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'scr.c'\"
else
echo shar: Extracting \"'scr.c'\" \(2285 characters\)
sed "s/^X//" >'scr.c' <<'END_OF_FILE'
X/*
X * Animal - copyright HCR Corporation, Toronto, Canada, 1989
X *
X * author: Stacey Campbell
X */
X
X#include <curses.h>
X
X#define HEAD "Animal"
X
Xstatic WINDOW *Header;
Xstatic WINDOW *FileMsg;
Xstatic WINDOW *QuestAns;
Xstatic WINDOW *QuestAnsBorder;
X
Xvoid InitCurses()
X
X {
X initscr();
X#ifdef BOGUS_ECHO
X echo();
X#endif
X werase(stdscr);
X#ifdef BOGUS_BOX
X box(stdscr, '|', '-');
X#else
X box(stdscr, 0, 0);
X#endif
X wrefresh(stdscr);
X Header = newwin(1, COLS - 2, 1, 1);
X wstandout(Header);
X mvwaddstr(Header, 0, (COLS - 2) / 2 - sizeof(HEAD) / 2 - 1, HEAD);
X wstandend(Header);
X wrefresh(Header);
X FileMsg = newwin(3, COLS - 2, 2, 1);
X#ifdef BOGUS_BOX
X box(FileMsg, '|', '-');
X#else
X box(FileMsg, 0, 0);
X#endif
X QuestAnsBorder = newwin(LINES - 6, COLS - 2, 5, 1);
X#ifdef BOGUS_BOX
X box(QuestAnsBorder, '|', '-');
X#else
X box(QuestAnsBorder, 0, 0);
X#endif
X QuestAns = newwin(LINES - 8, COLS - 4, 6, 2);
X wmove(QuestAns, 0, 0);
X scrollok(QuestAns, TRUE);
X idlok(QuestAns, TRUE);
X wrefresh(FileMsg);
X wrefresh(QuestAnsBorder);
X wrefresh(QuestAns);
X return;
X }
X
Xchar *FileQuestion(prompt)
X
Xchar *prompt;
X
X {
X static char answer[256];
X
X werase(FileMsg);
X#ifdef BOGUS_BOX
X box(FileMsg, '|', '-');
X#else
X box(FileMsg, 0, 0);
X#endif
X mvwaddstr(FileMsg, 1, 1, prompt);
X waddch(FileMsg, ' ');
X#ifdef BOGUS_GET
X wrefresh(FileMsg);
X#endif
X wgetstr(FileMsg, answer);
X return answer;
X }
X
Xvoid PutFileMsg(str)
X
Xchar *str;
X
X {
X werase(FileMsg);
X#ifdef BOGUS_BOX
X box(FileMsg, '|', '-');
X#else
X box(FileMsg, 0, 0);
X#endif
X mvwaddstr(FileMsg, 1, 1, str);
X wrefresh(FileMsg);
X }
X
Xvoid EndCurses()
X
X {
X delwin(Header);
X delwin(FileMsg);
X delwin(QuestAns);
X delwin(QuestAnsBorder);
X endwin();
X }
X
Xvoid PutQuestion(question)
X
Xchar *question;
X
X {
X wprintw(QuestAns, "%s?", question);
X }
X
Xvoid PutFinalQuestion(question)
X
Xchar *question;
X
X {
X char buf[256];
X void PutQuestion();
X
X strcpy(buf, "final guess: is it ");
X strcat(buf, question);
X PutQuestion(buf);
X }
X
Xvoid PutMsg(msg)
X
Xchar *msg;
X
X {
X waddstr(QuestAns, msg);
X waddch(QuestAns, '\n');
X }
X
Xvoid GetQuestLine(prompt, reply)
X
Xchar *prompt;
Xchar *reply;
X
X {
X wprintw(QuestAns, "%s ", prompt);
X#ifdef BOGUS_GET
X wrefresh(QuestAns);
X#endif
X wgetstr(QuestAns, reply);
X }
X
Xvoid StartBold()
X
X {
X wstandout(QuestAns);
X }
X
Xvoid EndBold()
X
X {
X wstandend(QuestAns);
X }
END_OF_FILE
if test 2285 -ne `wc -c <'scr.c'`; then
echo shar: \"'scr.c'\" unpacked with wrong size!
fi
# end of 'scr.c'
fi
if test -f 'animal.6' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'animal.6'\"
else
echo shar: Extracting \"'animal.6'\" \(2040 characters\)
sed "s/^X//" >'animal.6' <<'END_OF_FILE'
X.TH Animal 6l
X.SH NAME
XAnimal \- the classic game of animal
X.SH SYNOPSIS
X.B animal
X.B [database]
X.SH DESCRIPTION
X.I Animal
Xis an implementation of the ``guess the animal'' game,
Xan assignment given to most first year computer science students.
XThis version uses curses(3X) to give a reasonable interface,
Xand can read and save a database of questions and objects.
X.PP
XThe basic idea of the game is to think of an object
Xthen answer ``yes or no'' questions
Xuntil the program has a final guess at what you are thinking
Xof. If it is correct then you will be convinced that there
Xmay be something to AI after all. If it is incorrect you are
Xexpected to provide a question that, when asked, will yeild
Xa yes or no answer that will allow the computer to distinguish
Xbetween its last guess and the correct object. For example if
Xits last guess is Ronald Reagan but you were thinking of a gnat,
Xthen a suitable question would be 'Is it an insect?'. For RR
Xthe answer would be no, for a gnat the answer would be yes.
X.PP
XHints on choosing good questions:
X(a) keep the questions as general as possible,
X(b) never explicitly refer to either of the objects in your question, and
X(c) all questions can only have a yes or no answer, so avoid questions
Xthat could produce a ``maybe'' answer.
X.PP
XAt the end of the game you will be asked if you want to save
Xthe information you have added. A text file with a name
Xof your choosing will be created, and can be used as an argument
Xfor later invocations of
X.I animal.
X.SH BUGS
XUnknown, but please send bug problems to {utzoo,utcsri,lsuc}!hcr!stacey.
X.SH AUTHOR
XStacey Campbell \- HCR Corporation, Toronto, Canada, 1989.
X.SH DISCLAIMER
XAnimal is copyright 1989 by HCR Corporation, Toronto, Ontario, Canada.
XPermission to use, copy, modify, and distribute this software and
Xits documentation for any purpose and without fee is hereby
Xgranted, provided that the above copyright notice appear in all
Xcopies.
X.PP
XHCR Corporation disclaims all warranties with regard to
Xthis software. Use at your own risk.
END_OF_FILE
if test 2040 -ne `wc -c <'animal.6'`; then
echo shar: \"'animal.6'\" unpacked with wrong size!
fi
# end of 'animal.6'
fi
if test -f 'example' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'example'\"
else
echo shar: Extracting \"'example'\" \(861 characters\)
sed "s/^X//" >'example' <<'END_OF_FILE'
Xwas or is it a living organism
Xis it a human
Xis it female (for sex-less object answer no)
Xis she a well known politician
XIndira Ghandi
X$$
X$$
XElizabeth Taylor
X$$
X$$
Xwas it born in the USA
XRonald Reagan
X$$
X$$
XPierre Burton
X$$
X$$
Xis it an insect
Xdoes it construct underground dwellings
Xan ant
X$$
X$$
Xa bush fly
X$$
X$$
XRoger the Magic Llama
X$$
X$$
Xis it generally used for consuming beverages
Xis it constructed of ceramic
XStacey's coffee cup
X$$
X$$
Xa beer glass
X$$
X$$
Xcan a normal human touch it
Xdoes it consist mainly of paper
Xis a different version produced at least 6 days a week
Xthe New York Times
X$$
X$$
Xa Sears catalog
X$$
X$$
Xis it mainly used for medicinal reasons
Xa box of Sucrets
X$$
X$$
Xdoes it require electricity to operate usefully
Xis it a micro computer of some description
Xan IBM PC
X$$
X$$
Xa VAX 750 computer
X$$
X$$
Xa concrete brick
X$$
X$$
XAlpha Centauri
X$$
X$$
END_OF_FILE
if test 861 -ne `wc -c <'example'`; then
echo shar: \"'example'\" unpacked with wrong size!
fi
# end of 'example'
fi
echo shar: End of shell archive.
exit 0