BSD 4_3 development
[unix-history] / usr / src / games / hangman / getguess.c
CommitLineData
47f3a220
C
1# include "hangman.h"
2
3/*
4 * getguess:
5 * Get another guess
6 */
7getguess()
8{
9 register int i;
10 register int ch;
11 register bool correct;
12
13 leaveok(stdscr, FALSE);
14 for (;;) {
15 move(PROMPTY, PROMPTX + sizeof "Guess: ");
16 refresh();
17 ch = readch();
18 if (isalpha(ch)) {
19 if (isupper(ch))
20 ch = tolower(ch);
21 if (Guessed[ch - 'a'])
22 mvprintw(MESGY, MESGX, "Already guessed '%c'", ch);
23 else
24 break;
25 }
26 else if (ch == CTRL(D))
27 die();
28 else
29 mvprintw(MESGY, MESGX, "Not a valid guess: '%s'",
30 unctrl(ch));
31 }
32 leaveok(stdscr, TRUE);
33 move(MESGY, MESGX);
34 clrtoeol();
35
36 Guessed[ch - 'a'] = TRUE;
37 correct = FALSE;
38 for (i = 0; Word[i] != '\0'; i++)
39 if (Word[i] == ch) {
40 Known[i] = ch;
41 correct = TRUE;
42 }
43 if (!correct)
44 Errors++;
45}
46
47/*
48 * readch;
49 * Read a character from the input
50 */
51readch()
52{
53 register int cnt, r;
54 auto char ch;
55
56 cnt = 0;
57 for (;;) {
58 if (read(0, &ch, sizeof ch) <= 0)
59 {
60 if (++cnt > 100)
61 die();
62 }
63 else if (ch == CTRL(L)) {
64 wrefresh(curscr);
65 mvcur(0, 0, curscr->_cury, curscr->_curx);
66 }
67 else
68 return ch;
69 }
70}