misc.c revision 1.5 1 1.5 jtc /* $NetBSD: misc.c,v 1.5 1997/05/23 23:09:40 jtc Exp $ */
2 1.4 cgd
3 1.1 cgd /*
4 1.3 jtc * Copyright (c) 1983, 1993
5 1.3 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.4 cgd #if 0
38 1.4 cgd static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
39 1.4 cgd #else
40 1.5 jtc static char rcsid[] = "$NetBSD: misc.c,v 1.5 1997/05/23 23:09:40 jtc Exp $";
41 1.4 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.3 jtc #include <sys/file.h>
45 1.3 jtc #include <termios.h>
46 1.3 jtc
47 1.1 cgd #include "mille.h"
48 1.1 cgd #ifndef unctrl
49 1.1 cgd #include "unctrl.h"
50 1.1 cgd #endif
51 1.1 cgd
52 1.1 cgd
53 1.1 cgd # ifdef attron
54 1.1 cgd # include <term.h>
55 1.1 cgd # endif attron
56 1.1 cgd
57 1.1 cgd /*
58 1.1 cgd * @(#)misc.c 1.2 (Berkeley) 3/28/83
59 1.1 cgd */
60 1.1 cgd
61 1.1 cgd #define NUMSAFE 4
62 1.1 cgd
63 1.1 cgd /* VARARGS1 */
64 1.1 cgd error(str, arg)
65 1.1 cgd char *str;
66 1.1 cgd {
67 1.1 cgd stdscr = Score;
68 1.1 cgd mvprintw(ERR_Y, ERR_X, str, arg);
69 1.1 cgd clrtoeol();
70 1.1 cgd putchar('\07');
71 1.1 cgd refresh();
72 1.1 cgd stdscr = Board;
73 1.1 cgd return FALSE;
74 1.1 cgd }
75 1.1 cgd
76 1.1 cgd CARD
77 1.1 cgd getcard()
78 1.1 cgd {
79 1.5 jtc register int c, c1;
80 1.1 cgd
81 1.1 cgd for (;;) {
82 1.1 cgd while ((c = readch()) == '\n' || c == '\r' || c == ' ')
83 1.1 cgd continue;
84 1.1 cgd if (islower(c))
85 1.1 cgd c = toupper(c);
86 1.1 cgd if (c == killchar() || c == erasechar())
87 1.1 cgd return -1;
88 1.1 cgd addstr(unctrl(c));
89 1.1 cgd clrtoeol();
90 1.1 cgd switch (c) {
91 1.1 cgd case '1': case '2': case '3':
92 1.1 cgd case '4': case '5': case '6':
93 1.1 cgd c -= '0';
94 1.1 cgd break;
95 1.1 cgd case '0': case 'P': case 'p':
96 1.1 cgd c = 0;
97 1.1 cgd break;
98 1.1 cgd default:
99 1.1 cgd putchar('\07');
100 1.1 cgd addch('\b');
101 1.1 cgd if (!isprint(c))
102 1.1 cgd addch('\b');
103 1.1 cgd c = -1;
104 1.1 cgd break;
105 1.1 cgd }
106 1.1 cgd refresh();
107 1.1 cgd if (c >= 0) {
108 1.1 cgd while ((c1=readch()) != '\r' && c1 != '\n' && c1 != ' ')
109 1.1 cgd if (c1 == killchar())
110 1.1 cgd return -1;
111 1.1 cgd else if (c1 == erasechar()) {
112 1.1 cgd addch('\b');
113 1.1 cgd clrtoeol();
114 1.1 cgd refresh();
115 1.1 cgd goto cont;
116 1.1 cgd }
117 1.1 cgd else
118 1.1 cgd write(0, "\07", 1);
119 1.1 cgd return c;
120 1.1 cgd }
121 1.1 cgd cont: ;
122 1.1 cgd }
123 1.1 cgd }
124 1.1 cgd
125 1.1 cgd check_ext(forcomp)
126 1.5 jtc register bool forcomp; {
127 1.1 cgd
128 1.1 cgd
129 1.1 cgd if (End == 700)
130 1.1 cgd if (Play == PLAYER) {
131 1.1 cgd if (getyn(EXTENSIONPROMPT)) {
132 1.1 cgd extend:
133 1.1 cgd if (!forcomp)
134 1.1 cgd End = 1000;
135 1.1 cgd return TRUE;
136 1.1 cgd }
137 1.1 cgd else {
138 1.1 cgd done:
139 1.1 cgd if (!forcomp)
140 1.1 cgd Finished = TRUE;
141 1.1 cgd return FALSE;
142 1.1 cgd }
143 1.1 cgd }
144 1.1 cgd else {
145 1.5 jtc register PLAY *pp, *op;
146 1.5 jtc register int i, safe, miles;
147 1.1 cgd
148 1.1 cgd pp = &Player[COMP];
149 1.1 cgd op = &Player[PLAYER];
150 1.1 cgd for (safe = 0, i = 0; i < NUMSAFE; i++)
151 1.1 cgd if (pp->safety[i] != S_UNKNOWN)
152 1.1 cgd safe++;
153 1.1 cgd if (safe < 2)
154 1.1 cgd goto done;
155 1.1 cgd if (op->mileage == 0 || onecard(op)
156 1.1 cgd || (op->can_go && op->mileage >= 500))
157 1.1 cgd goto done;
158 1.1 cgd for (miles = 0, i = 0; i < NUMSAFE; i++)
159 1.1 cgd if (op->safety[i] != S_PLAYED
160 1.1 cgd && pp->safety[i] == S_UNKNOWN)
161 1.1 cgd miles++;
162 1.1 cgd if (miles + safe == NUMSAFE)
163 1.1 cgd goto extend;
164 1.1 cgd for (miles = 0, i = 0; i < HAND_SZ; i++)
165 1.1 cgd if ((safe = pp->hand[i]) <= C_200)
166 1.1 cgd miles += Value[safe];
167 1.1 cgd if (miles + (Topcard - Deck) * 3 > 1000)
168 1.1 cgd goto extend;
169 1.1 cgd goto done;
170 1.1 cgd }
171 1.1 cgd else
172 1.1 cgd goto done;
173 1.1 cgd }
174 1.1 cgd
175 1.1 cgd /*
176 1.1 cgd * Get a yes or no answer to the given question. Saves are
177 1.1 cgd * also allowed. Return TRUE if the answer was yes, FALSE if no.
178 1.1 cgd */
179 1.1 cgd getyn(promptno)
180 1.5 jtc register int promptno;
181 1.5 jtc {
182 1.5 jtc register char c;
183 1.1 cgd
184 1.1 cgd Saved = FALSE;
185 1.1 cgd for (;;) {
186 1.1 cgd leaveok(Board, FALSE);
187 1.1 cgd prompt(promptno);
188 1.1 cgd clrtoeol();
189 1.1 cgd refresh();
190 1.1 cgd switch (c = readch()) {
191 1.1 cgd case 'n': case 'N':
192 1.1 cgd addch('N');
193 1.1 cgd refresh();
194 1.1 cgd leaveok(Board, TRUE);
195 1.1 cgd return FALSE;
196 1.1 cgd case 'y': case 'Y':
197 1.1 cgd addch('Y');
198 1.1 cgd refresh();
199 1.1 cgd leaveok(Board, TRUE);
200 1.1 cgd return TRUE;
201 1.1 cgd case 's': case 'S':
202 1.1 cgd addch('S');
203 1.1 cgd refresh();
204 1.1 cgd Saved = save();
205 1.1 cgd continue;
206 1.3 jtc case CTRL('L'):
207 1.3 jtc wrefresh(curscr);
208 1.3 jtc break;
209 1.1 cgd default:
210 1.1 cgd addstr(unctrl(c));
211 1.1 cgd refresh();
212 1.1 cgd putchar('\07');
213 1.1 cgd break;
214 1.1 cgd }
215 1.1 cgd }
216 1.1 cgd }
217 1.1 cgd
218 1.1 cgd /*
219 1.1 cgd * Check to see if more games are desired. If not, and game
220 1.1 cgd * came from a saved file, make sure that they don't want to restore
221 1.1 cgd * it. Exit appropriately.
222 1.1 cgd */
223 1.1 cgd check_more() {
224 1.1 cgd
225 1.1 cgd On_exit = TRUE;
226 1.1 cgd if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
227 1.1 cgd if (getyn(ANOTHERGAMEPROMPT))
228 1.1 cgd return;
229 1.1 cgd else {
230 1.1 cgd /*
231 1.1 cgd * must do accounting normally done in main()
232 1.1 cgd */
233 1.1 cgd if (Player[PLAYER].total > Player[COMP].total)
234 1.1 cgd Player[PLAYER].games++;
235 1.1 cgd else if (Player[PLAYER].total < Player[COMP].total)
236 1.1 cgd Player[COMP].games++;
237 1.1 cgd Player[COMP].total = 0;
238 1.1 cgd Player[PLAYER].total = 0;
239 1.1 cgd }
240 1.1 cgd else
241 1.1 cgd if (getyn(ANOTHERHANDPROMPT))
242 1.1 cgd return;
243 1.1 cgd if (!Saved && getyn(SAVEGAMEPROMPT))
244 1.1 cgd if (!save())
245 1.1 cgd return;
246 1.3 jtc die(0);
247 1.1 cgd }
248 1.1 cgd
249 1.1 cgd readch()
250 1.1 cgd {
251 1.5 jtc register int cnt;
252 1.1 cgd static char c;
253 1.1 cgd
254 1.1 cgd for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
255 1.1 cgd if (cnt > 100)
256 1.1 cgd exit(1);
257 1.1 cgd return c;
258 1.1 cgd }
259