xref: /original-bsd/games/hangman/getguess.c (revision 2301fdfb)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)getguess.c	5.3 (Berkeley) 06/18/88";
20 #endif /* not lint */
21 
22 # include	"hangman.h"
23 
24 /*
25  * getguess:
26  *	Get another guess
27  */
28 getguess()
29 {
30 	register int	i;
31 	register int	ch;
32 	register bool	correct;
33 
34 	leaveok(stdscr, FALSE);
35 	for (;;) {
36 		move(PROMPTY, PROMPTX + sizeof "Guess: ");
37 		refresh();
38 		ch = readch();
39 		if (isalpha(ch)) {
40 			if (isupper(ch))
41 				ch = tolower(ch);
42 			if (Guessed[ch - 'a'])
43 				mvprintw(MESGY, MESGX, "Already guessed '%c'", ch);
44 			else
45 				break;
46 		}
47 		else if (ch == CTRL('D'))
48 			die();
49 		else
50 			mvprintw(MESGY, MESGX, "Not a valid guess: '%s'",
51 				unctrl(ch));
52 	}
53 	leaveok(stdscr, TRUE);
54 	move(MESGY, MESGX);
55 	clrtoeol();
56 
57 	Guessed[ch - 'a'] = TRUE;
58 	correct = FALSE;
59 	for (i = 0; Word[i] != '\0'; i++)
60 		if (Word[i] == ch) {
61 			Known[i] = ch;
62 			correct = TRUE;
63 		}
64 	if (!correct)
65 		Errors++;
66 }
67 
68 /*
69  * readch;
70  *	Read a character from the input
71  */
72 readch()
73 {
74 	register int	cnt, r;
75 	auto char	ch;
76 
77 	cnt = 0;
78 	for (;;) {
79 		if (read(0, &ch, sizeof ch) <= 0)
80 		{
81 			if (++cnt > 100)
82 				die();
83 		}
84 		else if (ch == CTRL('L')) {
85 			wrefresh(curscr);
86 			mvcur(0, 0, curscr->_cury, curscr->_curx);
87 		}
88 		else
89 			return ch;
90 	}
91 }
92