xref: /original-bsd/games/hangman/getguess.c (revision 9f9a0d6d)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)getguess.c	5.4 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 # include	"hangman.h"
13 
14 /*
15  * getguess:
16  *	Get another guess
17  */
18 getguess()
19 {
20 	register int	i;
21 	register int	ch;
22 	register bool	correct;
23 
24 	leaveok(stdscr, FALSE);
25 	for (;;) {
26 		move(PROMPTY, PROMPTX + sizeof "Guess: ");
27 		refresh();
28 		ch = readch();
29 		if (isalpha(ch)) {
30 			if (isupper(ch))
31 				ch = tolower(ch);
32 			if (Guessed[ch - 'a'])
33 				mvprintw(MESGY, MESGX, "Already guessed '%c'", ch);
34 			else
35 				break;
36 		}
37 		else if (ch == CTRL('D'))
38 			die();
39 		else
40 			mvprintw(MESGY, MESGX, "Not a valid guess: '%s'",
41 				unctrl(ch));
42 	}
43 	leaveok(stdscr, TRUE);
44 	move(MESGY, MESGX);
45 	clrtoeol();
46 
47 	Guessed[ch - 'a'] = TRUE;
48 	correct = FALSE;
49 	for (i = 0; Word[i] != '\0'; i++)
50 		if (Word[i] == ch) {
51 			Known[i] = ch;
52 			correct = TRUE;
53 		}
54 	if (!correct)
55 		Errors++;
56 }
57 
58 /*
59  * readch;
60  *	Read a character from the input
61  */
62 readch()
63 {
64 	register int	cnt, r;
65 	auto char	ch;
66 
67 	cnt = 0;
68 	for (;;) {
69 		if (read(0, &ch, sizeof ch) <= 0)
70 		{
71 			if (++cnt > 100)
72 				die();
73 		}
74 		else if (ch == CTRL('L')) {
75 			wrefresh(curscr);
76 			mvcur(0, 0, curscr->_cury, curscr->_curx);
77 		}
78 		else
79 			return ch;
80 	}
81 }
82