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