121499Smckusick /* 221499Smckusick * Copyright (c) 1980 Regents of the University of California. 321499Smckusick * All rights reserved. The Berkeley software License Agreement 421499Smckusick * specifies the terms and conditions for redistribution. 521499Smckusick */ 621499Smckusick 79895Ssam #ifndef lint 821499Smckusick char copyright[] = 921499Smckusick "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 1021499Smckusick All rights reserved.\n"; 1121499Smckusick #endif not lint 129895Ssam 1321499Smckusick #ifndef lint 14*41169Sbostic static char sccsid[] = "@(#)boggle.c 5.4 (Berkeley) 04/30/90"; 1521499Smckusick #endif not lint 1621499Smckusick 179895Ssam #include <ctype.h> 189895Ssam #include <errno.h> 199895Ssam #include <setjmp.h> 209895Ssam #include <sgtty.h> 219895Ssam #include <signal.h> 229895Ssam #include <stdio.h> 23*41169Sbostic #include "pathnames.h" 249895Ssam 259895Ssam /* basic parameters */ 269895Ssam #define N 4 279895Ssam #define SSIZE 200 289895Ssam #define MAXWORDS 1000 299895Ssam #define CWIDTH 10 309895Ssam #define LWIDTH 80 319895Ssam 329895Ssam /* parameters defined in terms of above */ 339895Ssam #define BSIZE (N*N) 349895Ssam #define row(x) (x/N) 359895Ssam #define col(x) (x%N) 369895Ssam 379895Ssam /* word being searched for */ 389895Ssam int wlength; 399895Ssam int numsame; 409895Ssam char wbuff [BSIZE+1]; 419895Ssam 429895Ssam /* tty and process control */ 439895Ssam extern int errno; 449895Ssam int status; 459895Ssam int pipefd[2]; 469895Ssam int super = 0; 479895Ssam int delct = 1; 489895Ssam int zero = 0; 499895Ssam int master = 1; 509895Ssam int column; 519895Ssam int *timept; 529895Ssam int timeint[] = {60,60,50,7,1,1,1,0}; 539895Ssam long timein; 549895Ssam extern long int time(); 559895Ssam struct sgttyb origttyb, tempttyb; 569895Ssam int ctlecho = 0; 579895Ssam int lctlech = LCTLECH; 589895Ssam jmp_buf env; 599895Ssam 609895Ssam /* monitoring variables */ 619895Ssam int games; 629895Ssam int logfile = -1; 639895Ssam long logloc; 649895Ssam char logbuff[100] = {"inst\t"}; 659895Ssam extern char *ctime(), *getlogin(); 669895Ssam extern long lseek(); 679895Ssam 689895Ssam /* dictionary interface */ 699895Ssam FILE *dict; 709895Ssam 719895Ssam /* structures for doing matching */ 729895Ssam struct frame { 739895Ssam struct frame *parent; 749895Ssam int place; 759895Ssam }; 769895Ssam struct frame stack [SSIZE]; 779895Ssam struct frame *level [BSIZE+1]; 789895Ssam 799895Ssam /* the board and subsidiary structures */ 809895Ssam char present [BSIZE+1]; 819895Ssam char board [BSIZE]; 829895Ssam char olink [BSIZE]; 839895Ssam char adj [BSIZE+1][BSIZE]; 849895Ssam char occurs [26]; 859895Ssam 869895Ssam /* the boggle cubes */ 879895Ssam char *cube [BSIZE] = { 889895Ssam "forixb", "moqabj", "gurilw", "setupl", 899895Ssam "cmpdae", "acitao", "slcrae", "romash", 909895Ssam "nodesw", "hefiye", "onudtk", "tevign", 919895Ssam "anedvz", "pinesh", "abilyt", "gkyleu" 929895Ssam }; 939895Ssam 949895Ssam 959895Ssam /* storage for words found */ 969895Ssam int ubotch, ustart, wcount; 979895Ssam char *word [MAXWORDS]; 989895Ssam char *freesp; 999895Ssam char space[10000]; 1009895Ssam 1019895Ssam endline () 1029895Ssam { 1039895Ssam if (column != 0) { 1049895Ssam putchar('\n'); 1059895Ssam column = 0; 1069895Ssam } 1079895Ssam } 1089895Ssam 1099895Ssam timeout () 1109895Ssam { 1119895Ssam if (*timept > 0) { 1129895Ssam signal (SIGALRM, timeout); 1139895Ssam alarm(*timept++); 1149895Ssam } 1159895Ssam putchar('\007'); 1169895Ssam } 1179895Ssam 1189895Ssam interrupt () 1199895Ssam { 1209895Ssam signal(SIGINT, interrupt); 1219895Ssam if (delct++ >= 1) 1229895Ssam longjmp(env, 1); 1239895Ssam timept = &zero; 1249895Ssam } 1259895Ssam 1269895Ssam goodbye (stat) 1279895Ssam int stat; 1289895Ssam { 1299895Ssam if (master != 0) { 1309895Ssam wait(&status); 1319895Ssam if ( ctlecho & LCTLECH ) { 1329895Ssam ioctl( fileno(stdin), TIOCLBIS, &lctlech ); 1339895Ssam } 1349895Ssam stty(fileno(stdin), &origttyb); 1359895Ssam } 1369895Ssam exit(stat); 1379895Ssam } 1389895Ssam 1399895Ssam clearscreen () 1409895Ssam { 1419895Ssam stty (fileno(stdin), &tempttyb); 1429895Ssam printf("\n\033\f\r"); 1439895Ssam } 1449895Ssam 1459895Ssam compare (a, b) 1469895Ssam char **a, **b; 1479895Ssam { 1489895Ssam return(wordcomp(*a, *b)); 1499895Ssam } 1509895Ssam 1519895Ssam wordcomp (p, q) 1529895Ssam register char *p, *q; 1539895Ssam { 1549895Ssam if (*p=='0' && *q!='0') 1559895Ssam return(-1); 1569895Ssam if (*p!='0' && *q=='0') 1579895Ssam return(1); 1589895Ssam while (*++p == *++q && isalpha(*p)) ; 1599895Ssam if (!isalpha(*p)) 1609895Ssam return(-isalpha(*q)); 1619895Ssam if (!isalpha(*q)) 1629895Ssam return(1); 1639895Ssam return(*p-*q); 1649895Ssam } 1659895Ssam 1669895Ssam printinst () 1679895Ssam { 1689895Ssam stty (fileno(stdin), &tempttyb); 1699895Ssam printf("instructions?"); 1709895Ssam if (getchar() == 'y') { 1719895Ssam clearscreen(); 1729895Ssam printf(" The object of Boggle (TM Parker Bros.) is to find, within 3\n"); 1739895Ssam printf("minutes, as many words as possible in a 4 by 4 grid of letters. Words\n"); 1749895Ssam printf("may be formed from any sequence of 3 or more adjacent letters in the\n"); 1759895Ssam printf("grid. The letters may join horizontally, vertically, or diagonally.\n"); 1769895Ssam printf("However, no position in the grid may be used more than once within any\n"); 1779895Ssam printf("one word. In competitive play amongst humans, each player is given\n"); 1789895Ssam printf("credit for those of his words which no other player has found.\n"); 1799895Ssam printf(" This program is intended for people wishing to sharpen their\n"); 1809895Ssam printf("skills at Boggle. If you invoke the program with 4 arguments of 4\n"); 1819895Ssam printf("letters each, (e.g. \"boggle appl epie moth erhd\") the program forms the\n"); 1829895Ssam printf("obvious Boggle grid and lists all the words from /usr/dict/words found\n"); 1839895Ssam printf("therein. If you invoke the program without arguments, it will generate\n"); 1849895Ssam printf("a board for you, let you enter words for 3 minutes, and then tell you\n"); 1859895Ssam printf("how well you did relative to /usr/dict/words.\n"); 1869895Ssam printf(" In interactive play, enter your words separated by spaces, tabs,\n"); 1879895Ssam printf("or newlines. A bell will ring when there is 2:00, 1:00, 0:10, 0:02,\n"); 1889895Ssam printf("0:01, and 0:00 time left. You may complete any word started before the\n"); 1899895Ssam printf("expiration of time. You can surrender before time is up by hitting\n"); 1909895Ssam printf("'break'. While entering words, your erase character is only effective\n"); 1919895Ssam printf("within the current word and your line kill character is ignored.\n"); 1929895Ssam printf(" Advanced players may wish to invoke the program with 1 or 2 +'s as\n"); 19320195Smckusick printf("the first argument. The first + removes the restriction that positions\n"); 1949895Ssam printf("can only be used once in each word. The second + causes a position to\n"); 1959895Ssam printf("be considered adjacent to itself as well as its (up to) 8 neighbors.\n"); 1969895Ssam printf("Hit any key to begin.\n"); 1979895Ssam stty (fileno(stdin), &tempttyb); 1989895Ssam getchar(); 1999895Ssam } 2009895Ssam stty (fileno(stdin), &tempttyb); 2019895Ssam } 2029895Ssam 2039895Ssam setup () 2049895Ssam { 2059895Ssam register int i, j; 2069895Ssam int rd, cd, k; 2079895Ssam for (i=0; i<BSIZE; i++) { 2089895Ssam adj[i][i] = super>=2 ? 1 : 0; 2099895Ssam adj[BSIZE][i] = 1; 2109895Ssam for (j=0; j<i; j++) { 2119895Ssam rd = row(i)-row(j); 2129895Ssam cd = col(i)-col(j); 2139895Ssam k = 0; 2149895Ssam switch (rd) { 2159895Ssam case -1: 2169895Ssam case 1: 2179895Ssam if (-1<=cd && cd<=1) 2189895Ssam k = 1; 2199895Ssam break; 2209895Ssam case 0: 2219895Ssam if (cd==-1 || cd==1) 2229895Ssam k = 1; 2239895Ssam break; 2249895Ssam } 2259895Ssam adj[i][j] = adj[j][i] = k; 2269895Ssam } 2279895Ssam } 2289895Ssam stack[0].parent = &stack[0]; 2299895Ssam stack[0].place = BSIZE; 2309895Ssam level[0] = &stack[0]; 2319895Ssam level[1] = &stack[1]; 2329895Ssam } 2339895Ssam 2349895Ssam makelists () 2359895Ssam { 2369895Ssam register int i, c; 2379895Ssam for (i=0; i<26; i++) 2389895Ssam occurs[i] = BSIZE; 2399895Ssam for (i=0; i<BSIZE; i++) { 2409895Ssam c = board[i]; 2419895Ssam olink[i] = occurs[c-'a']; 2429895Ssam occurs[c-'a'] = i; 2439895Ssam } 2449895Ssam } 2459895Ssam 2469895Ssam genboard () 2479895Ssam { 2489895Ssam register int i, j; 2499895Ssam for (i=0; i<BSIZE; i++) 2509895Ssam board[i] = 0; 2519895Ssam for (i=0; i<BSIZE; i++) { 2529895Ssam j = rand()%BSIZE; 2539895Ssam while (board[j] != 0) 2549895Ssam j = (j+1)%BSIZE; 2559895Ssam board[j] = cube[i][rand()%6]; 2569895Ssam } 2579895Ssam } 2589895Ssam 2599895Ssam printboard () 2609895Ssam { 2619895Ssam register int i, j; 2629895Ssam for (i=0; i<N; i++) { 2639895Ssam printf("\t\t\t\t\b\b"); 2649895Ssam for (j=0; j<N; j++) { 2659895Ssam putchar ((putchar(board[i*N+j]) == 'q') ? 'u' : ' '); 2669895Ssam putchar(' '); 2679895Ssam } 2689895Ssam putchar('\n'); 2699895Ssam } 2709895Ssam putchar('\n'); 2719895Ssam } 2729895Ssam 2739895Ssam getdword () 2749895Ssam { 2759895Ssam /* input: numsame = # chars same as last word */ 2769895Ssam /* output: numsame = # same chars for next word */ 2779895Ssam /* word in wbuff[0]...wbuff[wlength-1] */ 2789895Ssam register int c; 2799895Ssam register char *p; 2809895Ssam if (numsame == EOF) 2819895Ssam return (0); 2829895Ssam p = &wbuff[numsame]+1; 2839895Ssam while ((*p++ = c = getc(dict)) != EOF && isalpha(c)) ; 2849895Ssam numsame = c; 2859895Ssam wlength = p - &wbuff[2]; 2869895Ssam return (1); 2879895Ssam } 2889895Ssam 2899895Ssam getuword () 2909895Ssam { 2919895Ssam int c; 2929895Ssam register char *p, *q, *r; 2939895Ssam numsame = 0; 2949895Ssam while (*timept>0 && (isspace(c=getchar()) || c==EOF)); 2959895Ssam if (*timept == 0) 2969895Ssam return(0); 2979895Ssam word[wcount++] = freesp; 2989895Ssam *freesp++ = '0'; 2999895Ssam r = &wbuff[1]; 3009895Ssam q = p = freesp; 3019895Ssam *p++ = c; 3029895Ssam while (!isspace(c = getchar())) { 3039895Ssam if (c == EOF) 3049895Ssam continue; 3059895Ssam if (c == origttyb.sg_erase) { 3069895Ssam if (p > q) 3079895Ssam p--; 3089895Ssam continue; 3099895Ssam } 3109895Ssam *p++ = c; 3119895Ssam } 3129895Ssam freesp = p; 3139895Ssam for (p=q; p<freesp && r<&wbuff[BSIZE]; ) 3149895Ssam if (islower(c = *p++) && (*r++ = *q++ = c) == 'q' && *p == 'u') 3159895Ssam p++; 3169895Ssam *(freesp = q) = '0'; 3179895Ssam wlength = r-&wbuff[1]; 3189895Ssam return(1); 3199895Ssam } 3209895Ssam 3219895Ssam aputuword (ways) 3229895Ssam int ways; 3239895Ssam { 3249895Ssam *word[wcount-1] = ways>=10 ? '*' : '0'+ways; 3259895Ssam } 3269895Ssam 3279895Ssam aputword (ways) 3289895Ssam int ways; 3299895Ssam { 3309895Ssam /* store (wbuff, ways) in next slot in space */ 3319895Ssam register int i; 3329895Ssam *freesp++ = ways>=10 ? '*' : '0'+ways; 3339895Ssam for (i=1; i<= wlength; i++) 3349895Ssam *freesp++ = wbuff[i]; 3359895Ssam word[++wcount] = freesp; 3369895Ssam } 3379895Ssam 3389895Ssam tputword (ways) 3399895Ssam int ways; 3409895Ssam { 3419895Ssam /* print (wbuff, ways) on terminal */ 3429895Ssam wbuff[wlength+1] = '0'; 3439895Ssam wbuff[0] = ways>=10 ? '*' : '0'+ways; 3449895Ssam outword(&wbuff[0]); 3459895Ssam } 3469895Ssam 3479895Ssam outword (p) 3489895Ssam register char *p; 3499895Ssam { 3509895Ssam register int newcol; 3519895Ssam register char *q; 3529895Ssam for (q=p+1; isalpha(*q); ) { 3539895Ssam putchar(*q); 3549895Ssam if (*q++ == 'q') { 3559895Ssam putchar('u'); 3569895Ssam column++; 3579895Ssam } 3589895Ssam } 3599895Ssam column += q-p-1; 3609895Ssam if (column > LWIDTH-CWIDTH) { 3619895Ssam putchar('\n'); 3629895Ssam column = 0; 3639895Ssam return; 3649895Ssam } 3659895Ssam newcol = ((column+CWIDTH)/CWIDTH)*CWIDTH; 3669895Ssam while (((column+8)/8)*8 <= newcol) { 3679895Ssam putchar('\t'); 3689895Ssam column = ((column+8)/8)*8; 3699895Ssam } 3709895Ssam while (column < newcol) { 3719895Ssam putchar(' '); 3729895Ssam column++; 3739895Ssam } 3749895Ssam } 3759895Ssam 3769895Ssam printdiff () 3779895Ssam { 3789895Ssam register int c, d, u; 3799895Ssam char both, donly, uonly; 3809895Ssam word[wcount] = freesp; 3819895Ssam *freesp = '0'; 3829895Ssam both = donly = uonly = column = d = 0; 3839895Ssam u = ustart; 3849895Ssam while (d < ubotch) { 3859895Ssam c = u<wcount ? wordcomp (word[d], word[u]) : -1; 3869895Ssam if (c == 0) { 3879895Ssam /* dict and user found same word */ 3889895Ssam if (both == 0) { 3899895Ssam both = 1; 3909895Ssam printf("\t\t\t we both found:\n"); 3919895Ssam } 3929895Ssam outword(word[d]); 3939895Ssam word[d++] = NULL; 3949895Ssam word[u++] = NULL; 3959895Ssam } else if (c < 0) { 3969895Ssam /* dict found it, user didn't */ 3979895Ssam donly = 1; 3989895Ssam d++; 3999895Ssam } else { 4009895Ssam /* user found it, dict didn't */ 4019895Ssam uonly = 1; 4029895Ssam u++; 4039895Ssam } 4049895Ssam } 4059895Ssam endline(); 4069895Ssam if (donly) { 4079895Ssam printf("\n\t\t\tI alone found these:\n"); 4089895Ssam for (d=0; d<ubotch; d++) 4099895Ssam if (word[d] != NULL) 4109895Ssam outword(word[d]); 4119895Ssam endline(); 4129895Ssam } 4139895Ssam if (uonly) { 4149895Ssam printf("\n\t\t\tyou alone found these:\n"); 4159895Ssam for (u=ustart; u<wcount; u++) 4169895Ssam if (word[u] != NULL) 4179895Ssam outword(word[u]); 4189895Ssam endline(); 4199895Ssam } 4209895Ssam if (ubotch < ustart) { 4219895Ssam printf("\n\t\t\t you botched these:\n"); 4229895Ssam for (u=ubotch; u<ustart; u++) 4239895Ssam outword(word[u]); 4249895Ssam endline(); 4259895Ssam } 4269895Ssam } 4279895Ssam 4289895Ssam numways (leaf, last) 4299895Ssam register struct frame *leaf; 4309895Ssam struct frame *last; 4319895Ssam { 4329895Ssam int count; 4339895Ssam register char *p; 4349895Ssam register struct frame *node; 4359895Ssam if (super > 0) 4369895Ssam return(last-leaf); 4379895Ssam count = 0; 4389895Ssam present[BSIZE] = 1; 4399895Ssam while (leaf < last) { 4409895Ssam for (p = &present[0]; p < &present[BSIZE]; *p++ = 0); 4419895Ssam for (node = leaf; present[node->place]++ == 0; node = node->parent); 4429895Ssam if (node == &stack[0]) 4439895Ssam count++; 4449895Ssam leaf++; 4459895Ssam } 4469895Ssam return(count); 4479895Ssam } 4489895Ssam 4499895Ssam evalboard (getword, putword) 4509895Ssam int (*getword)(), (*putword)(); 4519895Ssam { 4529895Ssam register struct frame *top; 4539895Ssam register int l, q; 4549895Ssam int fo, found; 4559895Ssam struct frame *parent, *lastparent; 4569895Ssam char *padj; 4579895Ssam 4589895Ssam numsame = found = 0; 4599895Ssam makelists (); 4609895Ssam 4619895Ssam while (1) { 4629895Ssam l = numsame; 4639895Ssam if (!(*getword) ()) 4649895Ssam break; 4659895Ssam top = level[l+1]; 4669895Ssam 4679895Ssam while (1) { 4689895Ssam level[l+1] = lastparent = top; 4699895Ssam /* wbuff[1]...wbuff[l] have been matched */ 4709895Ssam /* level[0],...,level[l] of tree built */ 4719895Ssam if (l == wlength) { 4729895Ssam if (wlength >= 3 && (q = numways(level[l], top)) != 0) { 4739895Ssam (*putword) (q); 4749895Ssam found++; 4759895Ssam } 4769895Ssam l = BSIZE+1; 4779895Ssam break; 4789895Ssam } 4799895Ssam if ((fo = occurs[wbuff[++l]-'a']) == BSIZE) 4809895Ssam break; 4819895Ssam /* wbuff[1]...wbuff[l-1] have been matched */ 4829895Ssam /* level[0],...,level[l-1] of tree built */ 4839895Ssam for (parent=level[l-1]; parent<lastparent; parent++) { 4849895Ssam padj = &adj[parent->place][0]; 4859895Ssam for (q=fo; q!=BSIZE; q=olink[q]) 4869895Ssam if (padj[q]) { 4879895Ssam top->parent = parent; 4889895Ssam top->place = q; 4899895Ssam if (++top >= &stack[SSIZE]) { 4909895Ssam printf("stack overflow\n"); 4919895Ssam goodbye(1); 4929895Ssam } 4939895Ssam } 4949895Ssam } 4959895Ssam /* were any nodes added? */ 4969895Ssam if (top == lastparent) 4979895Ssam break; 4989895Ssam } 4999895Ssam 5009895Ssam /* advance until first l characters of next word are different */ 5019895Ssam while (numsame >= l && (*getword)()) ; 5029895Ssam } 5039895Ssam return(found); 5049895Ssam } 5059895Ssam 5069895Ssam main (argc, argv) 5079895Ssam int argc; 5089895Ssam char **argv; 5099895Ssam { 5109895Ssam char *q; 5119895Ssam register char *p; 5129895Ssam register int i, c; 5139895Ssam 5149895Ssam gtty (fileno(stdin), &origttyb); 5159895Ssam setbuf(stdin, NULL); 5169895Ssam tempttyb = origttyb; 5179895Ssam if (setjmp(env) != 0) 5189895Ssam goodbye(0); 5199895Ssam signal (SIGINT, interrupt); 5209895Ssam timein = time(0L); 5219895Ssam if (argv[0][0] != 'a' && (logfile = open("/usr/games/boglog", 1)) >= 0) { 5229895Ssam p = &logbuff[5]; 5239895Ssam q = getlogin(); 5249895Ssam while (*p++ = *q++); 5259895Ssam p[-1] = '\t'; 5269895Ssam q = ctime(&timein); 5279895Ssam while (*p++ = *q++); 5289895Ssam logloc = lseek(logfile, 0L, 2); 5299895Ssam write(logfile, &logbuff[0], p-&logbuff[1]); 5309895Ssam } 531*41169Sbostic if ((dict = fopen(_PATH_DICTIONARY, "r")) == NULL) { 532*41169Sbostic printf("can't open %s\n", _PATH_DICTIONARY); 5339895Ssam goodbye (2); 5349895Ssam } 5359895Ssam while ( argc > 1 && ( argv[1][0] == '+' || argv[1][0] == '-' ) ) { 5369895Ssam if (argv[1][0]=='+') { 5379895Ssam while(*(argv[1]++) == '+') 5389895Ssam super++; 5399895Ssam argc--; 5409895Ssam argv++; 5419895Ssam } 5429895Ssam if ( argv[1][0] == '-' ) { 5439895Ssam timeint[0] = 60 * ( atol( &argv[1][1] ) - 2 ); 5449895Ssam if ( timeint[0] <= 0 ) { 5459895Ssam timeint[0] = 60; 5469895Ssam } 5479895Ssam argc--; 5489895Ssam argv++; 5499895Ssam } 5509895Ssam } 5519895Ssam setup (); 5529895Ssam switch (argc) { 5539895Ssam default: punt: 5549895Ssam printf("usage: boggle [+[+]] [row1 row2 row3 row4]\n"); 5559895Ssam goodbye (3); 5569895Ssam case 5: 5579895Ssam for (i=0; i<BSIZE; i++) { 5589895Ssam board[i] = c = argv[row(i)+1][col(i)]; 5599895Ssam if (!islower(c)) { 5609895Ssam printf("bad board\n"); 5619895Ssam goto punt; 5629895Ssam } 5639895Ssam } 5649895Ssam printboard(); 5659895Ssam column = 0; 5669895Ssam evalboard(getdword, tputword); 5679895Ssam endline(); 5689895Ssam if (logfile >= 0) { 5699895Ssam strncpy(&logbuff[0], "eval", 4); 5709895Ssam lseek(logfile, logloc, 0); 5719895Ssam write(logfile, &logbuff[0], 4); 5729895Ssam } 5739895Ssam goodbye(0); 5749895Ssam case 1: 5759895Ssam tempttyb.sg_flags |= CBREAK; 5769895Ssam if ( ioctl( fileno(stdin), TIOCLGET, &ctlecho ) == 0 ) { 5779895Ssam if ( ctlecho & LCTLECH ) { 5789895Ssam ioctl( fileno(stdin), TIOCLBIC, &lctlech ); 5799895Ssam } 5809895Ssam } 5819895Ssam printinst(); 5829895Ssam srand((int) timein); 5839895Ssam while (setjmp(env) == 0) { 5849895Ssam errno = 0; 5859895Ssam if (pipe(&pipefd[0]) != 0) { 5869895Ssam printf("can't create pipe\n"); 5879895Ssam goodbye(4); 5889895Ssam } 5899895Ssam genboard(); 5909895Ssam delct = wcount = 0; 5919895Ssam word[0] = freesp = &space[0]; 5929895Ssam if ((master = fork()) == 0) { 5939895Ssam close(pipefd[0]); 5949895Ssam clearscreen(); 5959895Ssam printboard(); 5969895Ssam signal(SIGALRM, timeout); 5979895Ssam timept = &timeint[0]; 5989895Ssam alarm(*timept++); 5999895Ssam evalboard(getuword, aputuword); 6009895Ssam clearscreen(); 6019895Ssam qsort(&word[0], wcount, sizeof (int), compare); 6029895Ssam for (i=0; i<wcount; i++) 6039895Ssam if (i==0 || wordcomp(word[i], word[i-1])!=0) { 6049895Ssam p = word[i]; 6059895Ssam while (isalpha(*++p)) ; 6069895Ssam write (pipefd[1], word[i], p-word[i]); 6079895Ssam } 6089895Ssam close(pipefd[1]); 6099895Ssam goodbye(0); 6109895Ssam } 6119895Ssam close(pipefd[1]); 6129895Ssam rewind(dict); 6139895Ssam getc(dict); 6149895Ssam evalboard(getdword, aputword); 6159895Ssam p = freesp; 6169895Ssam while ((i = read(pipefd[0], freesp, 512)) != 0) { 6179895Ssam if (i < 0) 6189895Ssam if (errno != EINTR) 6199895Ssam break; 6209895Ssam else 6219895Ssam i = 0; 6229895Ssam freesp += i; 6239895Ssam } 6249895Ssam close(pipefd[0]); 6259895Ssam ustart = ubotch = wcount; 6269895Ssam while (p < freesp) { 6279895Ssam word[wcount++] = p; 6289895Ssam if (*p == '0') 6299895Ssam ustart = wcount; 6309895Ssam while (isalpha(*++p)); 6319895Ssam } 6329895Ssam wait(&status); 6339895Ssam if (status != 0) 6349895Ssam goodbye (5); 6359895Ssam delct = 1; 6369895Ssam printdiff(); 6379895Ssam printboard(); 6389895Ssam games++; 6399895Ssam if (logfile >= 0) { 64032490Sbostic (void)sprintf(&logbuff[0], "%4d", games); 6419895Ssam lseek(logfile, logloc, 0); 6429895Ssam write(logfile, &logbuff[0], 4); 6439895Ssam } 6449895Ssam stty (fileno(stdin), &tempttyb); 6459895Ssam printf("\nanother game?"); 6469895Ssam if (getchar() != 'y') { 6479895Ssam putchar('\n'); 6489895Ssam break; 6499895Ssam } 6509895Ssam stty (fileno(stdin), &tempttyb); 6519895Ssam } 6529895Ssam goodbye(0); 6539895Ssam } 6549895Ssam } 655