games/boggle/boggle/bog.c
/*
 * Play a single game
 * Reset the word lists from last game
 * Keep track of the running stats
 */
Play a single game
Reset the word lists from last game
Keep track of the running stats
Play a single game Reset the word lists from last game Keep track of the running stats
void
playgame()
{
	int i, *p, *q;
	long t;
	char buf[MAXWORDLEN + 1];
	ngames++;
	npwords = 0;
	pwordsp = pwords;
	nmwords = 0;
	mwordsp = mwords;
	time(&start_t);
	q = &wordpath[MAXWORDLEN + 1];
	p = wordpath;
	while (p < q)
		*p++ = -1;
	showboard(board);
	startwords();
	if (setjmp(env)) {
		badword();
		goto timesup;
	}
	while (1) {
		if (getline(buf) == NULL) {
			if (feof(stdin))
				clearerr(stdin);
			break;
		}
		time(&t);
		if (t - start_t >= tlimit) {
			badword();
			break;
		}
		if (buf[0] == '\0') {
			int remaining;
			remaining = tlimit - (int) (t - start_t);
			(void)snprintf(buf, sizeof(buf),
			    "%d:%02d", remaining / 60, remaining % 60);
			showstr(buf, 1);
			continue;
		}
		if (strlen(buf) < minlength) {
			badword();
			continue;
		}
		p = wordpath;
		while (p < q && *p != -1)
			*p++ = -1;
		usedbits = 0;
		if (checkword(buf, -1, wordpath) < 0)
			badword();
		else {
			if (debug) {
				(void) printf("[");
				for (i = 0; wordpath[i] != -1; i++)
					(void) printf(" %d", wordpath[i]);
				(void) printf(" ]\n");
			}
			for (i = 0; i < npwords; i++) {
				if (strcmp(pword[i], buf) == 0)
					break;
			}
			if (i != npwords) {	
				badword();
				showword(i);
			}
			else if (!validword(buf))
				badword();
			else {
				int len;
				len = strlen(buf) + 1;
				if (npwords == MAXPWORDS - 1 ||
				    pwordsp + len >= &pwords[MAXPSPACE]) {
					warnx("Too many words!");
					cleanup();
					exit(1);
				}
				pword[npwords++] = pwordsp;
				(void) strcpy(pwordsp, buf);
				pwordsp += len;
				addword(buf);
			}
		}
	}
timesup: ;
	qsort(pword, npwords, sizeof(pword[0]), compar);
	pword[npwords] = NULL;
	checkdict();
	tnmwords += nmwords;
	tnpwords += npwords;
	results();
}
void playgame ( ) { int i , * p , * q ; long t ; char buf [ MAXWORDLEN + 1 ] ; ngames ++ ; npwords = 0 ; pwordsp = pwords ; nmwords = 0 ; mwordsp = mwords ; time ( & start_t ) ; q = & wordpath [ MAXWORDLEN + 1 ] ; p = wordpath ; while ( p < q ) * p ++ = -1 ; showboard ( board ) ; startwords ( ) ; if ( setjmp ( env ) ) { badword ( ) ; goto timesup ; } while ( 1 ) { if ( getline ( buf ) == NULL ) { if ( feof ( stdin ) ) clearerr ( stdin ) ; break ; } time ( & t ) ; if ( t - start_t >= tlimit ) { badword ( ) ; break ; } if ( buf [ 0 ] == ' \0 ' ) { int remaining ; remaining = tlimit - ( int ) ( t - start_t ) ; ( void ) snprintf ( buf , sizeof ( buf ) , " " , remaining / 60 , remaining % 60 ) ; showstr ( buf , 1 ) ; continue ; } if ( strlen ( buf ) < minlength ) { badword ( ) ; continue ; } p = wordpath ; while ( p < q && * p != -1 ) * p ++ = -1 ; usedbits = 0 ; if ( checkword ( buf , -1 , wordpath ) < 0 ) badword ( ) ; else { if ( debug ) { ( void ) printf ( " " ) ; for ( i = 0 ; wordpath [ i ] != -1 ; i ++ ) ( void ) printf ( " " , wordpath [ i ] ) ; ( void ) printf ( " \n " ) ; } for ( i = 0 ; i < npwords ; i ++ ) { if ( strcmp ( pword [ i ] , buf ) == 0 ) break ; } if ( i != npwords ) { badword ( ) ; showword ( i ) ; } else if ( ! validword ( buf ) ) badword ( ) ; else { int len ; len = strlen ( buf ) + 1 ; if ( npwords == MAXPWORDS - 1 || pwordsp + len >= & pwords [ MAXPSPACE ] ) { warnx ( " " ) ; cleanup ( ) ; exit ( 1 ) ; } pword [ npwords ++ ] = pwordsp ; ( void ) strcpy ( pwordsp , buf ) ; pwordsp += len ; addword ( buf ) ; } } } timesup : ; qsort ( pword , npwords , sizeof ( pword [ 0 ] ) , compar ) ; pword [ npwords ] = NULL ; checkdict ( ) ; tnmwords += nmwords ; tnpwords += npwords ; results ( ) ; }
Play a single game
 Reset the word lists from last game
 Keep track of the running stats
Play a single game Reset the word lists from last game Keep track of the running stats
/* Can't use register variables if setjmp() is used! */ /* already used the word */ /*
	 * Sort the player's words and terminate the list with a null
	 * entry to help out checkdict()
	 */ /*
	 * These words don't need to be sorted since the dictionary is sorted
	 */