last changes before merger with reno version. cross fingers.
[unix-history] / usr / src / games / hangman / getguess.c
CommitLineData
ef609d98 1/*
b8c620d6 2 * Copyright (c) 1983 Regents of the University of California.
ef609d98
KB
3 * All rights reserved.
4 *
7c5ab767 5 * %sccs.include.redist.c%
ef609d98
KB
6 */
7
8#ifndef lint
7c5ab767 9static char sccsid[] = "@(#)getguess.c 5.4 (Berkeley) %G%";
ef609d98
KB
10#endif /* not lint */
11
12# include "hangman.h"
13
14/*
15 * getguess:
16 * Get another guess
17 */
18getguess()
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 }
85a9d303 37 else if (ch == CTRL('D'))
ef609d98
KB
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 */
62readch()
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 }
85a9d303 74 else if (ch == CTRL('L')) {
ef609d98
KB
75 wrefresh(curscr);
76 mvcur(0, 0, curscr->_cury, curscr->_curx);
77 }
78 else
79 return ch;
80 }
81}