xref: /original-bsd/games/mille/misc.c (revision 89e46f9f)
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.7 (Berkeley) 03/08/92";
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 		  case CTRL('L'):
174 			wrefresh(curscr);
175 			break;
176 		  default:
177 			addstr(unctrl(c));
178 			refresh();
179 			putchar('\07');
180 			break;
181 		}
182 	}
183 }
184 
185 /*
186  *	Check to see if more games are desired.  If not, and game
187  * came from a saved file, make sure that they don't want to restore
188  * it.  Exit appropriately.
189  */
190 check_more() {
191 
192 	On_exit = TRUE;
193 	if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
194 		if (getyn(ANOTHERGAMEPROMPT))
195 			return;
196 		else {
197 			/*
198 			 * must do accounting normally done in main()
199 			 */
200 			if (Player[PLAYER].total > Player[COMP].total)
201 				Player[PLAYER].games++;
202 			else if (Player[PLAYER].total < Player[COMP].total)
203 				Player[COMP].games++;
204 			Player[COMP].total = 0;
205 			Player[PLAYER].total = 0;
206 		}
207 	else
208 		if (getyn(ANOTHERHANDPROMPT))
209 			return;
210 	if (!Saved && getyn(SAVEGAMEPROMPT))
211 		if (!save())
212 			return;
213 	die(0);
214 }
215 
216 readch()
217 {
218 	reg int		cnt;
219 	static char	c;
220 
221 	for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
222 		if (cnt > 100)
223 			exit(1);
224 	return c;
225 }
226