home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 101_01 / jotto.c < prev    next >
Text File  |  1985-11-14  |  8KB  |  320 lines

  1.  
  2. /**********************************************************
  3.  ***                            ***
  4.  ***    Copyright (c) 1981 by David M. Fogg        ***
  5.  ***                            ***
  6.  ***        2632 N.E. Fremont            ***
  7.  ***        Portland, OR 97212            ***
  8.  ***                            ***
  9.  ***        (503) 288-3502{HM} || 223-8033{WK}        ***
  10.  ***                            ***
  11.  ***    Permission is herewith granted for non-     ***
  12.  ***    commercial distribution through the BDS C    ***
  13.  ***    User's Group; any and all forms of commercial   ***
  14.  ***    redistribution are strenuously unwished-for.    ***
  15.  ***                            ***
  16.  **********************************************************/
  17.  
  18. /* ---> JOTTO - converted from Pascal version
  19.  
  20.    Copyright (c) 1980, by David M. Fogg - Portland, Ore.
  21.  
  22.    13 Nov 80: creation day
  23.    18 Nov: creation...finished
  24.    19 Nov: bugwork
  25. */
  26.  
  27. #include <std.h>
  28.  
  29. #define LETS      5    /* letters/word */
  30. #define ISCORE      21    /* initial score */
  31. #define XTITLE      40    /* Title loc */
  32. #define YTITLE      1
  33. #define XPROMPT   33    /* prompt loc */
  34. #define YPROMPT   4
  35. #define XSCORE      3    /* score col */
  36. #define XGUESS      10    /* guess col */
  37. #define XMATCH      17    /* match col */
  38. #define XSTATE      20    /* status col */
  39. #define YHEAD      0    /* header row */
  40. #define YFIRST      2    /* first guess row */
  41. #define XLABELS   33    /* list labels col */
  42. #define XLISTS      42    /* lists cols */
  43. #define YYES      6    /* confirmed letters row */
  44. #define YNO      8    /* eliminated letters row */
  45. #define YPOSS      10    /* possible letters row */
  46. #define XINST      30    /* instructions loc */
  47. #define YINST      13
  48. #define MAXWORDS  600    /* max # of words in computer list */
  49.  
  50. /*
  51.    * * *  G L O B A L S  * * *
  52. */
  53.    BOOL wordok, giveup;
  54.    int score;
  55.    char word[LETS+3];
  56.    char guess[LETS+1], gots[LETS+1];
  57.    char nots[27], maybes[27];
  58.    int count;
  59.    char guesslis[ISCORE+1][LETS+1];
  60.    int changed;
  61.  
  62. main (ac, av)
  63. int ac;
  64. char *av[];
  65. {
  66.    int numwords;
  67.    char wordlist[MAXWORDS+1][LETS+1];
  68.    char ibuf[BUFSIZ];
  69.    char wfil[15];         /* word file name */
  70.    char c;
  71.  
  72.    if (ac < 2)                  /* set wordfile name */
  73.       strcpy(wfil, "jotto.wds");
  74.    else
  75.       strcpy(wfil, av[1]);
  76.  
  77.    if (fopen(wfil, ibuf) == ERROR)      /* open wordfile */
  78.       errxit("Word file I/O error");
  79.  
  80.    numwords = 0;              /* read wordfile */
  81.    while (fgets(word, ibuf) != NULL) {
  82.       word[LETS] = NULL;
  83.       strcpy(wordlist[numwords++], word);
  84.    }
  85.    fclose(ibuf);
  86.  
  87.    nrand(0, "Hit RETURN sometime...");      /* crank up random # gen */
  88.    getchar(); puts("thanx\n");
  89.  
  90.    FOREVER {                  /* --> MAIN LOOP <-- */
  91.       score = ISCORE;
  92.       nots[0] = NULL;
  93.       strcpy(maybes, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  94.       strcpy(gots, "     ");
  95.  
  96.       doscreen();
  97.       inword(XPROMPT, YPROMPT, "Your word: ", word);
  98.       if (strcmp(word, "?????") == 0)
  99.      strcpy(word, wordlist[nrand(1) % numwords]);
  100.       toxy(XPROMPT, YPROMPT); puts("                    ");
  101.       do {
  102.      doguess();
  103.      if (strcmp(guess, word) != 0 && !giveup) dochange();
  104.      if (wordok) --score;
  105.       } while (strcmp(guess, word) != 0 && score > 0 && !giveup);
  106.       toxy(XPROMPT, YPROMPT - 1);
  107.       if (strcmp(guess, word))
  108.      printf("The word was: %s.", word);
  109.       else
  110.      printf("Congrats: you got %d points!", ++score);
  111.  
  112.       if (nomore()) break;
  113.    }
  114. }
  115.  
  116.  
  117. /*
  118.    =========<<< SUBROUTINES >>>=========
  119. */
  120.  
  121. doscreen ()              /*** --<SET UP THE SCREEN>-- ***/
  122. {
  123.    puts(CLEAR);
  124.    toxy(XTITLE, YTITLE); puts("--> J O T T O  <--");
  125.    toxy(XSCORE, YHEAD); puts("SCORE  GUESS  #  STATE\n");
  126.    toxy(XSCORE, YHEAD+1); puts("=====  =====  =  =====");
  127.    toxy(XLABELS+3, YYES); puts("YES:");
  128.    toxy(XLABELS+4, YNO); puts("NO:");
  129.    toxy(XLABELS+1, YPOSS); puts("MAYBE:");
  130.    toxy(XLISTS, YPOSS); printset(maybes);
  131.    toxy(XINST, YINST); puts("<?????> to 'Your word:' gets random word");
  132.    toxy(XINST, YINST+1); puts("<?????> instead of guess aborts game");
  133.    toxy(XINST, YINST+2); puts("Change:");
  134.    toxy(XINST+2, YINST+3); puts("<A> allows re-entry of bad word");
  135.    toxy(XINST+2, YINST+4); puts("<+X> adds 'X' to YES list");
  136.    toxy(XINST+2, YINST+5); puts("<-X> takes 'X' off YES list");
  137.    toxy(XINST+2, YINST+6); puts("<NX> move 'X' MAYBE->NO");
  138.    toxy(XINST+2, YINST+7); puts("<MX> move 'X' NO->MAYBE");
  139.    toxy(XINST+2, YINST+8); puts("<RETURN> performs changes (if any)");
  140.    toxy(XINST, YINST+10); puts("STATE: upcase=YES; locase=maybe; '.'=NO");
  141. }
  142.  
  143.  
  144. printset (s)        /*** --<OUTPUT s IN ALFA ORDER>-- ***/
  145. char *s;
  146.      /* O/P space if ltr missing, else ltr if in gots, else tolower */
  147. {
  148.    char str;
  149.  
  150.    for (str = 'A'; str <= 'Z'; ++str)
  151.       if (inset(s, str))
  152.      putchar(inset(gots, str) ? str : tolower(str));
  153.       else
  154.      putchar(' ');
  155. }
  156.  
  157.  
  158. inword (x, y, p, w)       /*** --<TO x/y, PROMPT p, KB INP->w>-- ***/
  159. int x, y;
  160. char *p, *w;
  161. {
  162.    toxy(x, y); puts(p); getword(w);
  163. }
  164.  
  165.  
  166. getword (wd)           /*** --<GET 5-LETTER wd FROM KB>-- ***/
  167. char *wd;
  168. {
  169.    int i;
  170.  
  171.    for (i = 0; i < LETS; ++i) wd[i] = getlet();
  172.  
  173.    wd[LETS] = NULL;
  174. }
  175.  
  176.  
  177. char getlet ()            /*** --<GET A VALID KB LETTER>-- ***/
  178. {
  179.    char ch;
  180.  
  181.    do {
  182.       ch = toupper(coin());
  183.    } while (!isalpha(ch) && ch != '?' && ch != '/');
  184.    if (ch == '/') ch = '?';
  185.    putchar(ch);
  186.    return (ch);
  187. }
  188.  
  189.  
  190. doguess ()         /*** --<PROCESS PLAYER'S GUESS>-- ***/
  191. {
  192.    int i, j, y;
  193.    char str[LETS+1];
  194.  
  195.    y = YFIRST + ISCORE - score;
  196.  
  197.    toxy(XSCORE, y); printf("%4d", score);
  198.    inword(XGUESS-1, y, " ", guess);
  199.    if (strcmp(guess, "?????") == 0)
  200.       giveup = YES;
  201.    else {
  202.       giveup = NO;
  203.       strcpy(str, word); count = 0;
  204.       for (i = 0; i < LETS; ++i) {
  205.      if ((j = inset(str, guess[i])) > 0) {
  206.         ++count;
  207.         str[j-1] = tolower(str[j-1]);
  208.      }
  209.       }
  210.       printf("%3d", count);
  211.       strcpy(guesslis[score], guess);
  212.       dostate(score);
  213.    }
  214. }
  215.  
  216.  
  217. dostate (s)              /*** --<UPD STATE FOR SCORE s>-- ***/
  218. int s;
  219. {
  220.    int i, j;
  221.    char ch;
  222.    char ges[LETS+1], got[LETS+1];
  223.  
  224.    toxy(XSTATE, YFIRST + ISCORE - s);
  225.    strcpy(ges, guesslis[s]); strcpy(got, gots);
  226.  
  227.    for (i = 0; i < LETS; ++i) {
  228.       ch = ges[i];
  229.       if ((j = inset(got, ch)) > 0) {
  230.      putchar(ch);
  231.      strcpy(got+j-1, got+j);
  232.       }
  233.       else
  234.      putchar((inset(nots, ch) > 0) ? '.' : tolower(ch));
  235.    }
  236. }
  237.  
  238.  
  239. modgot (old, new)          /*** --<MODIFY old->new IN gots>-- ***/
  240. char old, new;
  241. {
  242.    int j;
  243.  
  244.    if ((j = inset(gots, old)) > 0) gots[--j] = new;
  245. }
  246.  
  247.  
  248. xfer (l, src, dest)          /*** --<XFER l FROM src->dest STRING>-- ***/
  249. char l;
  250. char *src, *dest;
  251. {
  252.    int j;
  253.  
  254.    if ((j = inset(src, l)) > 0) {
  255.       strcpy(src+j-1, src+j);
  256.       dest[j = strlen(dest)] = l; dest[++j] = NULL;
  257.    }
  258. }
  259.  
  260.  
  261. dochange ()             /*** --<DO CHANGE(S)>-- ***/
  262. {
  263.    int sc;
  264.    char let, ch;
  265.  
  266.    changed = 0;
  267.    wordok = NO;
  268.  
  269.    toxy(XPROMPT, YPROMPT); puts("Change: ");
  270.    do {
  271.       ch = toupper(coin());
  272.    } while (inset("A+=-_NM\r", ch) == 0);
  273.    if (ch == 'A')
  274.       return;
  275.    else
  276.       wordok = YES;
  277.  
  278.    while (ch != '\r' && changed < LETS) {
  279.       ++changed;
  280.       switch (ch) {
  281.      case '=': ch = '+'; break;
  282.      case '_': ch = '-';
  283.       }
  284.       putchar(ch);
  285.       let = getlet(); putchar(' ');
  286.       switch (ch) {
  287.      case '+': modgot(' ', let); break;
  288.      case '-': modgot(let, ' '); break;
  289.      case 'N': xfer(let, maybes, nots); break;
  290.      case 'M': xfer(let, nots, maybes);
  291.       }
  292.       do {
  293.      ch = toupper(coin());
  294.       } while (inset("A+=-_NM\r", ch) == 0);
  295.    }
  296.    if (changed > 0) {
  297.       toxy(XPROMPT, YPROMPT); puts(EREOL);
  298.       toxy(XLISTS, YYES); puts(gots);
  299.       toxy(XLISTS, YNO); printset(nots);
  300.       toxy(XLISTS, YPOSS); printset(maybes);
  301.       for (sc = ISCORE; sc >= score ; --sc) dostate(sc);
  302.       dochange();
  303.    }
  304. }
  305.  
  306.  
  307. BOOL nomore ()           /*** --<SEE IF DONE PLAYING>-- ***/
  308. {
  309.    char ch;
  310.  
  311.    toxy(XPROMPT, YPROMPT); puts("Play again? ");
  312.  
  313.    do {
  314.       ch = toupper(coin());
  315.    } while (ch != 'Y' && ch != 'N');
  316.    putchar(ch);
  317.  
  318.    return (ch == 'N' ? YES : NO);
  319. }
  320.