xref: /original-bsd/games/mille/misc.c (revision 65ba69af)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)misc.c	5.4 (Berkeley) 06/18/88";
20 #endif /* not lint */
21 
22 #include	"mille.h"
23 #ifndef	unctrl
24 #include	"unctrl.h"
25 #endif
26 
27 # include	<sys/file.h>
28 
29 # ifdef	attron
30 #	include	<term.h>
31 #	define	_tty	cur_term->Nttyb
32 # endif	attron
33 
34 /*
35  * @(#)misc.c	1.2 (Berkeley) 3/28/83
36  */
37 
38 #define	NUMSAFE	4
39 
40 /* VARARGS1 */
41 error(str, arg)
42 char	*str;
43 {
44 	stdscr = Score;
45 	mvprintw(ERR_Y, ERR_X, str, arg);
46 	clrtoeol();
47 	putchar('\07');
48 	refresh();
49 	stdscr = Board;
50 	return FALSE;
51 }
52 
53 CARD
54 getcard()
55 {
56 	reg int		c, c1;
57 
58 	for (;;) {
59 		while ((c = readch()) == '\n' || c == '\r' || c == ' ')
60 			continue;
61 		if (islower(c))
62 			c = toupper(c);
63 		if (c == killchar() || c == erasechar())
64 			return -1;
65 		addstr(unctrl(c));
66 		clrtoeol();
67 		switch (c) {
68 		  case '1':	case '2':	case '3':
69 		  case '4':	case '5':	case '6':
70 			c -= '0';
71 			break;
72 		  case '0':	case 'P':	case 'p':
73 			c = 0;
74 			break;
75 		  default:
76 			putchar('\07');
77 			addch('\b');
78 			if (!isprint(c))
79 				addch('\b');
80 			c = -1;
81 			break;
82 		}
83 		refresh();
84 		if (c >= 0) {
85 			while ((c1=readch()) != '\r' && c1 != '\n' && c1 != ' ')
86 				if (c1 == killchar())
87 					return -1;
88 				else if (c1 == erasechar()) {
89 					addch('\b');
90 					clrtoeol();
91 					refresh();
92 					goto cont;
93 				}
94 				else
95 					write(0, "\07", 1);
96 			return c;
97 		}
98 cont:		;
99 	}
100 }
101 
102 check_ext(forcomp)
103 reg bool	forcomp; {
104 
105 
106 	if (End == 700)
107 		if (Play == PLAYER) {
108 			if (getyn(EXTENSIONPROMPT)) {
109 extend:
110 				if (!forcomp)
111 					End = 1000;
112 				return TRUE;
113 			}
114 			else {
115 done:
116 				if (!forcomp)
117 					Finished = TRUE;
118 				return FALSE;
119 			}
120 		}
121 		else {
122 			reg PLAY	*pp, *op;
123 			reg int		i, safe, miles;
124 
125 			pp = &Player[COMP];
126 			op = &Player[PLAYER];
127 			for (safe = 0, i = 0; i < NUMSAFE; i++)
128 				if (pp->safety[i] != S_UNKNOWN)
129 					safe++;
130 			if (safe < 2)
131 				goto done;
132 			if (op->mileage == 0 || onecard(op)
133 			    || (op->can_go && op->mileage >= 500))
134 				goto done;
135 			for (miles = 0, i = 0; i < NUMSAFE; i++)
136 				if (op->safety[i] != S_PLAYED
137 				    && pp->safety[i] == S_UNKNOWN)
138 					miles++;
139 			if (miles + safe == NUMSAFE)
140 				goto extend;
141 			for (miles = 0, i = 0; i < HAND_SZ; i++)
142 				if ((safe = pp->hand[i]) <= C_200)
143 					miles += Value[safe];
144 			if (miles + (Topcard - Deck) * 3 > 1000)
145 				goto extend;
146 			goto done;
147 		}
148 	else
149 		goto done;
150 }
151 
152 /*
153  *	Get a yes or no answer to the given question.  Saves are
154  * also allowed.  Return TRUE if the answer was yes, FALSE if no.
155  */
156 getyn(promptno)
157 register int	promptno; {
158 
159 	reg char	c;
160 
161 	Saved = FALSE;
162 	for (;;) {
163 		leaveok(Board, FALSE);
164 		prompt(promptno);
165 		clrtoeol();
166 		refresh();
167 		switch (c = readch()) {
168 		  case 'n':	case 'N':
169 			addch('N');
170 			refresh();
171 			leaveok(Board, TRUE);
172 			return FALSE;
173 		  case 'y':	case 'Y':
174 			addch('Y');
175 			refresh();
176 			leaveok(Board, TRUE);
177 			return TRUE;
178 		  case 's':	case 'S':
179 			addch('S');
180 			refresh();
181 			Saved = save();
182 			continue;
183 		  default:
184 			addstr(unctrl(c));
185 			refresh();
186 			putchar('\07');
187 			break;
188 		}
189 	}
190 }
191 
192 /*
193  *	Check to see if more games are desired.  If not, and game
194  * came from a saved file, make sure that they don't want to restore
195  * it.  Exit appropriately.
196  */
197 check_more() {
198 
199 	flush_input();
200 
201 	On_exit = TRUE;
202 	if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
203 		if (getyn(ANOTHERGAMEPROMPT))
204 			return;
205 		else {
206 			/*
207 			 * must do accounting normally done in main()
208 			 */
209 			if (Player[PLAYER].total > Player[COMP].total)
210 				Player[PLAYER].games++;
211 			else if (Player[PLAYER].total < Player[COMP].total)
212 				Player[COMP].games++;
213 			Player[COMP].total = 0;
214 			Player[PLAYER].total = 0;
215 		}
216 	else
217 		if (getyn(ANOTHERHANDPROMPT))
218 			return;
219 	if (!Saved && getyn(SAVEGAMEPROMPT))
220 		if (!save())
221 			return;
222 	die();
223 }
224 
225 readch()
226 {
227 	reg int		cnt;
228 	static char	c;
229 
230 	for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
231 		if (cnt > 100)
232 			exit(1);
233 	return c;
234 }
235 
236 flush_input()
237 {
238 # ifdef	TIOCFLUSH
239 	static int	ioctl_args = FREAD;
240 
241 	(void) ioctl(fileno(stdin), TIOCFLUSH, &ioctl_args);
242 # else
243 	fflush(stdin);
244 # endif
245 }
246