I think I'd enjoy writing something like
that for fun. It should be pretty easy (for me :-) ).
If you let me know, I can probably come up
with something (gimme a few days), and it's yours.
-Mike
#! /bin/sh
# 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)
Xcc $(LDFLAGS) $(OBJS) -o $(EXE) $(LIBS)
X
Xshar:
Xxshar -v README animal.6 Makefile $(SRC) example > $(EXE).shar
X
Xman:
Xnroff -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 {
Xchar *question;
Xstruct node_t *yes_p;
Xstruct node_t *no_p;
X} node_t;
X
Xmain(argc, argv)
X
Xint argc;
Xchar *argv[];
X
X{
Xnode_t *top;
Xchar buf[256];
Xnode_t *InitTop();
Xnode_t *LoadFile();
Xvoid Query();
Xint KeepGoing();
Xvoid Usage();
Xvoid SaveAnimals();
Xvoid InitCurses();
Xvoid EndCurses();
Xvoid PutFileMsg();
Xvoid PutMsg();
X
XInitCurses();
Xswitch (argc)
X {
X case 1 :
Xtop = InitTop();
Xbreak;
X case 2 :
Xtop = LoadFile(argv[1]);
Xif (top == 0)
XUsage(BAD_FILE, argv[1]);
Xsprintf(buf, "using datafile: %s", argv[1]);
XPutFileMsg(buf);
Xbreak;
X default :
XUsage(BAD_OPTS, argv[0]);
Xbreak;
X }
X
Xdo{
XPutMsg("Think of something...");
XQuery(top);
X} while (KeepGoing() == YES_ANS);
X
XSaveAnimals(top);
XEndCurses();
Xreturn 0;
X}
X
Xstatic void SaveAnimals(top)
X
Xnode_t *top;
X
X{
Xchar fn[256];
Xchar buf[256];
Xint GetAnswer();
Xvoid DumpAnimals();
Xchar *FileQuestion();
XFILE *fp;
X
X
Xif (YesNo(FileQuestion("Do want to save the data?")) == NO_ANS)
Xreturn;
Xdo{
Xstrcpy(fn, FileQuestion("input a datafile name:"));
Xif ((fp = fopen(fn, "w")) == NULL)
X{
Xsprintf(buf, "Cannot open %s, try again?", fn);
Xif (YesNo(FileQuestion(buf)) == NO_ANS)
Xreturn;
X}
X} while (fp == NULL);
XDumpAnimals(fp, top);
Xfclose(fp);
X}
X
Xstatic void DumpAnimals(fp, node_p)
X
XFILE *fp;
Xnode_t *node_p;
X
X{
Xif (node_p == 0)
Xfprintf(fp, "%s\n", BOTTOM);
Xelse
X{
Xfprintf(fp, "%s\n", node_p->question);
XDumpAnimals(fp, node_p->yes_p);
XDumpAnimals(fp, node_p->no_p);
X}
X}
X
Xstatic node_t *LoadFile(fn)
X
Xchar *fn;
X
X{
XFILE *fp;
Xnode_t *top;
Xvoid CollectAnimals();
X
Xif ((fp = fopen(fn, "r")) == NULL)
Xreturn 0;
XCollectAnimals(fp, &top);
Xfclose(fp);
Xreturn top;
X}
X
Xstatic void CollectAnimals(fp, node_pp)
X
XFILE *fp;
Xnode_t **node_pp;
X
X{
Xstatic char read_buf[256];
Xnode_t *GetNode();
X
Xif (fgets(read_buf, 256, fp) == NULL)
Xreturn;
Xread_buf[strlen(read_buf) - 1] = '\0';
Xif (strcmp(read_buf, BOTTOM) == 0)
Xreturn;
Xelse
X{
X*node_pp = GetNode(read_buf);
XCollectAnimals(fp, &(*node_pp)->yes_p);
XCollectAnimals(fp, &(*node_pp)->no_p);
X}
X}
X
Xstatic void Usage(status, str)
X
Xint status;
Xchar *str;
X
X{
Xchar buf[256];
Xvoid PutFileMsg();
Xvoid EndCurses();
X
Xswitch (status)
X {
X case BAD_FILE :
Xsprintf(buf, "Cannot open datafile: %s", str);
XPutFileMsg(buf);
Xbreak;
X case BAD_OPTS :
X default :
Xsprintf(buf, "Usage: %s {data-file}", str);
XPutFileMsg(buf);
Xbreak;
X }
XEndCurses();
Xexit(1);
X}
X
Xstatic node_t *InitTop()
X
X{
Xnode_t *top;
Xchar fn[256];
Xnode_t *GetNode();
Xchar *FileQuestion();
Xnode_t *LoadFile();
Xvoid Usage();
Xint YesNo();
X
Xif (YesNo(FileQuestion("Do you wish to read a datafile?")) == YES_ANS)
X{
Xstrcpy(fn, FileQuestion("Name of datafile?"));
Xtop = LoadFile(fn);
Xif (top == 0)
XUsage(BAD_FILE, fn);
X}
Xelse
X{
Xtop = GetNode("was or is it a living organism");
Xtop->yes_p = GetNode("an ant");
Xtop->no_p = GetNode("a concrete brick");
X}
Xreturn top;
X}
X
Xstatic void Query(node_p)
X
Xnode_t *node_p;
X
X{
Xint GetAnswer();
Xvoid NewAnimal();
Xvoid PutQuestion();
Xvoid PutFinalQuestion();
Xvoid PutMsg();
X
Xif (node_p->yes_p)
X{
XPutQuestion(node_p->question);
Xif (GetAnswer() == YES_ANS)
XQuery(node_p->yes_p);
Xelse
XQuery(node_p->no_p);
X}
Xelse
X{
XPutFinalQuestion(node_p->question);
Xif (GetAnswer() == YES_ANS)
XPutMsg("Yay!");
Xelse
XNewAnimal(node_p);
X}
X}
X
Xstatic void NewAnimal(node_p)
X
Xnode_t *node_p;
X
X{
Xchar animal[256];
Xchar question[256];
Xchar buf[256];
Xnode_t *old_object, *new_query, *new_object;
Xchar *malloc();
Xnode_t *GetNode();
Xint GetAnswer();
Xvoid GetQuestLine();
Xvoid PutMsg();
Xvoid PutQuestion();
X
Xold_object = (node_t *) malloc(sizeof(node_t));
X*old_object = *node_p;
XGetQuestLine("What are you thinking of?", animal);
Xnew_object = GetNode(animal);
XPutMsg("Type a question such that:");
XPutMsg(" (a) a yes or no answer would distinguish");
Xsprintf(buf, " between %s and %s", animal, node_p->question);
XPutMsg(buf);
XPutMsg("and");
XPutMsg(" (b) it does not explicitly mention either of these things.");
XGetQuestLine("question:", question);
Xnew_query = GetNode(question);
X*node_p = *new_query;
X
Xsprintf(buf, "For %s the answer would be", animal);
XPutQuestion(buf);
Xif (GetAnswer() == YES_ANS)
X{
Xnode_p->yes_p = new_object;
Xnode_p->no_p = old_object;
X}
Xelse
X{
Xnode_p->yes_p = old_object;
Xnode_p->no_p = new_object;
X}
X}
X
Xstatic int GetAnswer()
X
X{
Xchar input[256];
Xvoid GetQuestLine();
Xint YesNo();
X
XGetQuestLine("", input);
Xreturn YesNo(input);
X}
X
Xstatic int YesNo(str)
X
Xchar *str;
X
X{
Xint ret_val;
Xvoid ToLower();
X
XToLower(str);
Xif (strcmp(str, "yes") == 0 || strcmp(str, "y") == 0)
Xret_val = YES_ANS;
Xelse
Xret_val = NO_ANS;
X
Xreturn ret_val;
X}
X
Xstatic int KeepGoing()
X
X{
Xvoid PutQuestion();
Xint GetAnswer();
Xvoid StartBold();
Xvoid EndBold();
X
XStartBold();
XPutQuestion("Another query");
XEndBold();
Xreturn GetAnswer();
X}
X
Xvoid ToLower(str)
X
Xchar *str;
X
X{
Xwhile (*str)
X{
Xif (*str >= 'A' && *str <= 'Z')
X*str += 'a' - 'A';
Xstr++;
X}
X}
X
Xnode_t *GetNode(question)
X
Xchar *question;
X
X{
Xnode_t *node_p;
Xchar *malloc();
X
Xif ((node_p = (node_t *) malloc(sizeof(node_t))) == NULL)
Xreturn NULL;
Xnode_p->question = malloc(strlen(question) + 2);
Xstrcpy(node_p->question, question);
Xnode_p->yes_p = 0;
Xnode_p->no_p = 0;
Xreturn 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{
Xinitscr();
X#ifdef BOGUS_ECHO
Xecho();
X#endif
Xwerase(stdscr);
X#ifdef BOGUS_BOX
Xbox(stdscr, '|', '-');
X#else
Xbox(stdscr, 0, 0);
X#endif
Xwrefresh(stdscr);
XHeader = newwin(1, COLS - 2, 1, 1);
Xwstandout(Header);
Xmvwaddstr(Header, 0, (COLS - 2) / 2 - sizeof(HEAD) / 2 - 1, HEAD);
Xwstandend(Header);
Xwrefresh(Header);
XFileMsg = newwin(3, COLS - 2, 2, 1);
X#ifdef BOGUS_BOX
Xbox(FileMsg, '|', '-');
X#else
Xbox(FileMsg, 0, 0);
X#endif
XQuestAnsBorder = newwin(LINES - 6, COLS - 2, 5, 1);
X#ifdef BOGUS_BOX
Xbox(QuestAnsBorder, '|', '-');
X#else
Xbox(QuestAnsBorder, 0, 0);
X#endif
XQuestAns = newwin(LINES - 8, COLS - 4, 6, 2);
Xwmove(QuestAns, 0, 0);
Xscrollok(QuestAns, TRUE);
Xidlok(QuestAns, TRUE);
Xwrefresh(FileMsg);
Xwrefresh(QuestAnsBorder);
Xwrefresh(QuestAns);
Xreturn;
X}
X
Xchar *FileQuestion(prompt)
X
Xchar *prompt;
X
X{
Xstatic char answer[256];
X
Xwerase(FileMsg);
X#ifdef BOGUS_BOX
Xbox(FileMsg, '|', '-');
X#else
Xbox(FileMsg, 0, 0);
X#endif
Xmvwaddstr(FileMsg, 1, 1, prompt);
Xwaddch(FileMsg, ' ');
X#ifdef BOGUS_GET
Xwrefresh(FileMsg);
X#endif
Xwgetstr(FileMsg, answer);
Xreturn answer;
X}
X
Xvoid PutFileMsg(str)
X
Xchar *str;
X
X{
Xwerase(FileMsg);
X#ifdef BOGUS_BOX
Xbox(FileMsg, '|', '-');
X#else
Xbox(FileMsg, 0, 0);
X#endif
Xmvwaddstr(FileMsg, 1, 1, str);
Xwrefresh(FileMsg);
X}
X
Xvoid EndCurses()
X
X{
Xdelwin(Header);
Xdelwin(FileMsg);
Xdelwin(QuestAns);
Xdelwin(QuestAnsBorder);
Xendwin();
X}
X
Xvoid PutQuestion(question)
X
Xchar *question;
X
X{
Xwprintw(QuestAns, "%s?", question);
X}
X
Xvoid PutFinalQuestion(question)
X
Xchar *question;
X
X{
Xchar buf[256];
Xvoid PutQuestion();
X
Xstrcpy(buf, "final guess: is it ");
Xstrcat(buf, question);
XPutQuestion(buf);
X}
X
Xvoid PutMsg(msg)
X
Xchar *msg;
X
X{
Xwaddstr(QuestAns, msg);
Xwaddch(QuestAns, '\n');
X}
X
Xvoid GetQuestLine(prompt, reply)
X
Xchar *prompt;
Xchar *reply;
X
X{
Xwprintw(QuestAns, "%s ", prompt);
X#ifdef BOGUS_GET
Xwrefresh(QuestAns);
X#endif
Xwgetstr(QuestAns, reply);
X}
X
Xvoid StartBold()
X
X{
Xwstandout(QuestAns);
X}
X
Xvoid EndBold()
X
X{
Xwstandend(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