games@tekred.CNA.TEK.COM (03/22/89)
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.]] #! /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) 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