Annotation of researchv10no/games/hangman.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include <sys/types.h>
                      3: #include <sys/stat.h>
                      4: #define DICT "/usr/dict/words"
                      5: #define EDICT "/usr1/dict/web2"
                      6: #define MAXERR 7
                      7: #define MINSCORE 0
                      8: #define MINLEN 7
                      9: char *dictfile;
                     10: int alive,lost;
                     11: double frand();
                     12: FILE *dict;
                     13: long int dictlen;
                     14: float errors=0, words=0;
                     15: main(argc,argv) char **argv;
                     16: {
                     17:        if(argc==1) dictfile=DICT;
                     18:        else if(*argv[1]=='-') dictfile=EDICT;
                     19:        else dictfile=argv[1];
                     20:        setup();
                     21:        for(;;)
                     22:        {       startnew();
                     23:                while(alive>0)
                     24:                {       stateout();
                     25:                        getguess();
                     26:                }
                     27:                words=words+1;
                     28:                if(lost) wordout();
                     29:                else youwon();
                     30:        }
                     31: }
                     32: setup()
                     33: {
                     34:        struct stat statb;
                     35:        long t;
                     36:        t = time(NULL);
                     37:        srand(t+(t>>16));
                     38:        if((dict=fopen(dictfile,"r"))==NULL) fatal("no dictionary");
                     39:        if(stat(dictfile,&statb)<0) fatal("can't stat");
                     40:        dictlen=statb.st_size;
                     41: }
                     42: char word[26],alph[26],realword[26];
                     43: startnew()
                     44: {      int i;
                     45:        long int pos;
                     46:        char buf[128];
                     47:        for(i=0;i<26;i++) word[i]=alph[i]=realword[i]=0;
                     48:        pos=frand()*dictlen;
                     49:        fseek(dict,pos,0);
                     50:        fscanf(dict,"%s\n",buf);
                     51:        getword();
                     52:        alive=MAXERR;
                     53:        lost=0;
                     54: }
                     55: stateout()
                     56: {      int i;
                     57:        printf("guesses: ");
                     58:        for(i=0;i<26;i++)
                     59:                if(alph[i]!=0) putchar(alph[i]);
                     60:        printf(" word: %s ",word);
                     61:        printf("errors: %d/%d\n",MAXERR-alive,MAXERR);
                     62: }
                     63: getguess()
                     64: {      char gbuf[128],c;
                     65:        int ok=0,i;
                     66: loop:
                     67:        printf("guess: ");
                     68:        if(gets(gbuf)==NULL)
                     69:        {       printf("The word was %s\n", realword);
                     70:                exit(0);
                     71:        }
                     72:        if((c=gbuf[0])<'a' || c>'z')
                     73:        {       printf("lower case\n");
                     74:                goto loop;
                     75:        }
                     76:        if(alph[c-'a']!=0)
                     77:        {       printf("you guessed that\n");
                     78:                goto loop;
                     79:        }
                     80:        else alph[c-'a']=c;
                     81:        for(i=0;realword[i]!=0;i++)
                     82:                if(realword[i]==c)
                     83:                {       word[i]=c;
                     84:                        ok=1;
                     85:                }
                     86:        if(ok==0)
                     87:        {       alive--;
                     88:                errors=errors+1;
                     89:                if(alive<=0) lost=1;
                     90:                return;
                     91:        }
                     92:        for(i=0;word[i]!=0;i++)
                     93:                if(word[i]=='.') return;
                     94:        alive=0;
                     95:        lost=0;
                     96:        return;
                     97: }
                     98: wordout()
                     99: {
                    100:        errors=errors+2;
                    101:        printf("the answer was %s, you blew it\n",realword);
                    102: }
                    103: youwon()
                    104: {
                    105:        printf("you win, the word is %s\n",realword);
                    106: }
                    107: fatal(s) char *s;
                    108: {
                    109:        fprintf(stderr,"%s\n",s);
                    110:        exit(1);
                    111: }
                    112: getword()
                    113: {      char wbuf[128],c;
                    114:        int i,j;
                    115: loop:
                    116:        if(fscanf(dict,"%s\n",wbuf)==EOF)
                    117:        {       fseek(dict,0L,0);
                    118:                goto loop;
                    119:        }
                    120:        if((c=wbuf[0])>'z' || c<'a') goto loop;
                    121:        for(i=j=0;wbuf[j]!=0;i++,j++)
                    122:        {       if(wbuf[j]=='-') j++;
                    123:                wbuf[i]=wbuf[j];
                    124:        }
                    125:        wbuf[i]=0;
                    126:        if(i<MINLEN) goto loop;
                    127:        for(j=0;j<i;j++)
                    128:                if((c=wbuf[j])<'a' || c>'z') goto loop;
                    129:        pscore();
                    130:        strcpy(realword,wbuf);
                    131:        for(j=0;j<i;word[j++]='.');
                    132: }
                    133: pscore()
                    134: {
                    135:        if(words!=0) printf("(%4.2f/%.0f) ",errors/words,words);
                    136: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.