xref: /csrg-svn/games/hangman/getword.c (revision 60809)
133108Sbostic /*
2*60809Sbostic  * Copyright (c) 1983, 1993
3*60809Sbostic  *	The Regents of the University of California.  All rights reserved.
433108Sbostic  *
542578Sbostic  * %sccs.include.redist.c%
633108Sbostic  */
733108Sbostic 
833108Sbostic #ifndef lint
9*60809Sbostic static char sccsid[] = "@(#)getword.c	8.1 (Berkeley) 05/31/93";
1033108Sbostic #endif /* not lint */
1133108Sbostic 
1257821Storek #include "hangman.h"
1357821Storek #include <stdlib.h>
1433108Sbostic 
1533108Sbostic /*
1633108Sbostic  * getword:
1733108Sbostic  *	Get a valid word out of the dictionary file
1833108Sbostic  */
getword()1933108Sbostic getword()
2033108Sbostic {
2133108Sbostic 	register FILE		*inf;
2233108Sbostic 	register char		*wp, *gp;
2357821Storek 	register long		 pos;
2433108Sbostic 
2533108Sbostic 	inf = Dict;
2633108Sbostic 	for (;;) {
2757821Storek 		pos = (double)rand() / (RAND_MAX + 1.0) * (double)Dict_size;
2857821Storek 		fseek(inf, pos, 0);
2933108Sbostic 		if (fgets(Word, BUFSIZ, inf) == NULL)
3033108Sbostic 			continue;
3133108Sbostic 		if (fgets(Word, BUFSIZ, inf) == NULL)
3233108Sbostic 			continue;
3333108Sbostic 		Word[strlen(Word) - 1] = '\0';
3433108Sbostic 		if (strlen(Word) < MINLEN)
3533108Sbostic 			continue;
3633108Sbostic 		for (wp = Word; *wp; wp++)
3733108Sbostic 			if (!islower(*wp))
3833108Sbostic 				goto cont;
3933108Sbostic 		break;
4033108Sbostic cont:		;
4133108Sbostic 	}
4233108Sbostic 	gp = Known;
4333108Sbostic 	wp = Word;
4433108Sbostic 	while (*wp) {
4533108Sbostic 		*gp++ = '-';
4633108Sbostic 		wp++;
4733108Sbostic 	}
4833108Sbostic 	*gp = '\0';
4933108Sbostic }
50