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