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