home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 159_01 / english.c < prev    next >
C/C++ Source or Header  |  1990-05-04  |  3KB  |  195 lines

  1.  
  2. /*  ENGLISH.C   no mod for V 1.43
  3.   
  4.                 Changed all calls of rand() to irand() for Eco-C88 (BW)
  5. */
  6.  
  7. #include "advent.h"
  8.  
  9. /*
  10.         Analyze a two word sentence
  11. */
  12. BOOL PASCAL english(VOID)
  13. {
  14.  
  15.     auto     char      *msg;
  16.     auto     SHORT    type1, type2, val1, val2;
  17.  
  18.     verb = object = motion = 0;
  19.     type2 = val2 = -1;
  20.     type1 = val1 = -1;
  21.     msg = "bad grammar...";
  22.     getin();
  23.     if (!analyze(word1, &type1, &val1))
  24.     return(FALSE);
  25.     if (type1 == 2 && val1 == SAY)
  26.     {
  27.     verb = SAY;
  28.     object = 1;
  29.     return(TRUE);
  30.     }
  31.  
  32.     if (strcmp(word2, ""))
  33.     {
  34.     if (!analyze(word2, &type2, &val2))
  35.         return(FALSE);
  36.     }
  37.  
  38.     /* check his grammar */
  39.     if (type1 == 3)
  40.     {
  41.     rspeak(val1);
  42.     return(FALSE);
  43.     }
  44.     else
  45.     {
  46.     if (type2 == 3)
  47.     {
  48.         rspeak(val2);
  49.         return(FALSE);
  50.     }
  51.     else
  52.     {
  53.         if (type1 == 0)
  54.         {
  55.         if (type2 == 0)
  56.         {
  57.             printf("%s\n", msg);
  58.             return(FALSE);
  59.         }
  60.         else
  61.             motion = val1;
  62.         }
  63.         else
  64.         {
  65.         if (type2 == 0)
  66.             motion = val2;
  67.         else
  68.         {
  69.             if (type1 == 1)
  70.             {
  71.             object = val1;
  72.             if (type2 == 2)
  73.                 verb = val2;
  74.             if (type2 == 1)
  75.             {
  76.                 printf("%s\n", msg);
  77.                 return(FALSE);
  78.             }
  79.             }
  80.             else
  81.             {
  82.             if (type1 == 2)
  83.             {
  84.                 verb = val1;
  85.                 if (type2 == 1)
  86.                 object = val2;
  87.                 if (type2 == 2)
  88.                 {
  89.                 printf("%s\n", msg);
  90.                 return(FALSE);
  91.                 }
  92.             }
  93.             else
  94.                 bug(36);
  95.             }
  96.         }
  97.         }
  98.     }
  99.     }
  100.     return(TRUE);
  101. }
  102.  
  103.  
  104. /*
  105.                 Routine to analyze a word.
  106. */
  107. BOOL PASCAL analyze(char *word, SHORT *type, SHORT *value)
  108. {
  109.     auto     SHORT     wordval, msg;
  110.  
  111.     /* make sure I understand */
  112.     if ((wordval = vocab(word, 0)) == -1)
  113.     {
  114.     switch (rand() % 3)
  115.     {
  116.         case 0:
  117.         msg = 60;
  118.         break;
  119.         case 1:
  120.         msg = 61;
  121.         break;
  122.         default:
  123.         msg = 13;
  124.     }
  125.     rspeak(msg);
  126.     return(FALSE);
  127.     }
  128.     *type  = wordval / 1000;
  129.     *value = wordval % 1000;
  130.     return(TRUE);
  131. }
  132.  
  133. /*
  134.         routine to get two words of input
  135.         and convert them to lower case
  136. */
  137. VOID PASCAL getin(VOID)
  138. {
  139.     auto     char      *bptr;
  140.     auto     char    linebuff[65];
  141.  
  142.     putchar('>');
  143.     word1[0] = word2[0] = '\0';
  144.     bptr = linebuff;
  145.     gets(linebuff);
  146.     skipspc(&bptr);
  147.     getword(&bptr, word1);
  148.     if (!*bptr)
  149.     return;
  150.  
  151.     while (!isspace(*bptr))
  152.     {
  153.     if (!*bptr++)
  154.         return;
  155.     }
  156.  
  157.     skipspc(&bptr);
  158.     getword(&bptr, word2);
  159.  
  160.     return;
  161. }
  162.  
  163. /*
  164.         Routine to extract one word from a buffer
  165. */
  166. VOID PASCAL getword(char **buff, char *word)
  167. {
  168.     register SHORT       i;
  169.  
  170.     for (i = 0; i < WORDSIZE; ++i)
  171.     {
  172.     if (!**buff || isspace(**buff))
  173.     {
  174.         *word = NUL;
  175.         return;
  176.     }
  177.     *word++ = (char) tolower(**buff);
  178.     (*buff)++;
  179.     }
  180.     *--word = NUL;
  181.  
  182.     return;
  183. }
  184.  
  185. /*
  186.         Routine to skip spaces
  187. */
  188. VOID PASCAL skipspc(char **buff)
  189. {
  190.     while (isspace(**buff))
  191.     ++(*buff);
  192.  
  193.     return;
  194. }
  195.