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