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