xref: /dragonfly/games/hangman/getguess.c (revision 984263bc)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)getguess.c	8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/hangman/getguess.c,v 1.6 1999/12/10 03:22:59 billf Exp $";
40 #endif /* not lint */
41 
42 #include <sys/ttydefaults.h>
43 #include "hangman.h"
44 
45 /*
46  * getguess:
47  *	Get another guess
48  */
49 void
50 getguess()
51 {
52 	int	i;
53 	int	ch;
54 	bool	correct;
55 
56 	leaveok(stdscr, FALSE);
57 	for (;;) {
58 		move(PROMPTY, PROMPTX + sizeof "Guess: ");
59 		refresh();
60 		ch = readch();
61 		if (isalpha(ch)) {
62 			if (isupper(ch))
63 				ch = tolower(ch);
64 			if (Guessed[ch - 'a'])
65 				mvprintw(MESGY, MESGX, "Already guessed '%c'", ch);
66 			else
67 				break;
68 		}
69 		else if (ch == CTRL('D'))
70 			die(0);
71 		else
72 			mvprintw(MESGY, MESGX, "Not a valid guess: '%s'",
73 				unctrl(ch));
74 	}
75 	leaveok(stdscr, TRUE);
76 	move(MESGY, MESGX);
77 	clrtoeol();
78 
79 	Guessed[ch - 'a'] = TRUE;
80 	correct = FALSE;
81 	for (i = 0; Word[i] != '\0'; i++)
82 		if (Word[i] == ch) {
83 			Known[i] = ch;
84 			correct = TRUE;
85 		}
86 	if (!correct)
87 		Errors++;
88 }
89 
90 /*
91  * readch;
92  *	Read a character from the input
93  */
94 char
95 readch()
96 {
97 	int	cnt;
98 	auto char	ch;
99 
100 	cnt = 0;
101 	for (;;) {
102 		if (read(0, &ch, sizeof ch) <= 0)
103 		{
104 			if (++cnt > 100)
105 				die(0);
106 		}
107 		else if (ch == CTRL('L')) {
108 			wrefresh(curscr);
109 			mvcur(0, 0, curscr->_cury, curscr->_curx);
110 		}
111 		else
112 			return ch;
113 	}
114 }
115