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