xref: /csrg-svn/games/mille/misc.c (revision 52875)
130203Sbostic /*
230203Sbostic  * Copyright (c) 1983 Regents of the University of California.
333175Sbostic  * All rights reserved.
433175Sbostic  *
542578Sbostic  * %sccs.include.redist.c%
630203Sbostic  */
730203Sbostic 
830203Sbostic #ifndef lint
9*52875Storek static char sccsid[] = "@(#)misc.c	5.7 (Berkeley) 03/08/92";
1033175Sbostic #endif /* not lint */
1130203Sbostic 
1230203Sbostic #include	"mille.h"
1330203Sbostic #ifndef	unctrl
1430203Sbostic #include	"unctrl.h"
1530203Sbostic #endif
1630203Sbostic 
1730203Sbostic # include	<sys/file.h>
1830203Sbostic 
1930203Sbostic # ifdef	attron
2030203Sbostic #	include	<term.h>
2130203Sbostic #	define	_tty	cur_term->Nttyb
2230203Sbostic # endif	attron
2330203Sbostic 
2430203Sbostic /*
2530203Sbostic  * @(#)misc.c	1.2 (Berkeley) 3/28/83
2630203Sbostic  */
2730203Sbostic 
2830203Sbostic #define	NUMSAFE	4
2930203Sbostic 
3030203Sbostic /* VARARGS1 */
3130203Sbostic error(str, arg)
3230203Sbostic char	*str;
3330203Sbostic {
3430203Sbostic 	stdscr = Score;
3530203Sbostic 	mvprintw(ERR_Y, ERR_X, str, arg);
3630203Sbostic 	clrtoeol();
3734716Sbostic 	putchar('\07');
3830203Sbostic 	refresh();
3930203Sbostic 	stdscr = Board;
4030203Sbostic 	return FALSE;
4130203Sbostic }
4230203Sbostic 
4330203Sbostic CARD
4430203Sbostic getcard()
4530203Sbostic {
4630203Sbostic 	reg int		c, c1;
4730203Sbostic 
4830203Sbostic 	for (;;) {
4930203Sbostic 		while ((c = readch()) == '\n' || c == '\r' || c == ' ')
5030203Sbostic 			continue;
5130203Sbostic 		if (islower(c))
5230203Sbostic 			c = toupper(c);
5330203Sbostic 		if (c == killchar() || c == erasechar())
5430203Sbostic 			return -1;
5530203Sbostic 		addstr(unctrl(c));
5630203Sbostic 		clrtoeol();
5730203Sbostic 		switch (c) {
5830203Sbostic 		  case '1':	case '2':	case '3':
5930203Sbostic 		  case '4':	case '5':	case '6':
6030203Sbostic 			c -= '0';
6130203Sbostic 			break;
6230203Sbostic 		  case '0':	case 'P':	case 'p':
6330203Sbostic 			c = 0;
6430203Sbostic 			break;
6530203Sbostic 		  default:
6634716Sbostic 			putchar('\07');
6730203Sbostic 			addch('\b');
6830203Sbostic 			if (!isprint(c))
6930203Sbostic 				addch('\b');
7030203Sbostic 			c = -1;
7130203Sbostic 			break;
7230203Sbostic 		}
7330203Sbostic 		refresh();
7430203Sbostic 		if (c >= 0) {
7530203Sbostic 			while ((c1=readch()) != '\r' && c1 != '\n' && c1 != ' ')
7630203Sbostic 				if (c1 == killchar())
7730203Sbostic 					return -1;
7830203Sbostic 				else if (c1 == erasechar()) {
7930203Sbostic 					addch('\b');
8030203Sbostic 					clrtoeol();
8130203Sbostic 					refresh();
8230203Sbostic 					goto cont;
8330203Sbostic 				}
8430203Sbostic 				else
8534716Sbostic 					write(0, "\07", 1);
8630203Sbostic 			return c;
8730203Sbostic 		}
8830203Sbostic cont:		;
8930203Sbostic 	}
9030203Sbostic }
9130203Sbostic 
9230203Sbostic check_ext(forcomp)
9330203Sbostic reg bool	forcomp; {
9430203Sbostic 
9530203Sbostic 
9630203Sbostic 	if (End == 700)
9730203Sbostic 		if (Play == PLAYER) {
9830203Sbostic 			if (getyn(EXTENSIONPROMPT)) {
9930203Sbostic extend:
10030203Sbostic 				if (!forcomp)
10130203Sbostic 					End = 1000;
10230203Sbostic 				return TRUE;
10330203Sbostic 			}
10430203Sbostic 			else {
10530203Sbostic done:
10630203Sbostic 				if (!forcomp)
10730203Sbostic 					Finished = TRUE;
10830203Sbostic 				return FALSE;
10930203Sbostic 			}
11030203Sbostic 		}
11130203Sbostic 		else {
11230203Sbostic 			reg PLAY	*pp, *op;
11330203Sbostic 			reg int		i, safe, miles;
11430203Sbostic 
11530203Sbostic 			pp = &Player[COMP];
11630203Sbostic 			op = &Player[PLAYER];
11730203Sbostic 			for (safe = 0, i = 0; i < NUMSAFE; i++)
11830203Sbostic 				if (pp->safety[i] != S_UNKNOWN)
11930203Sbostic 					safe++;
12030203Sbostic 			if (safe < 2)
12130203Sbostic 				goto done;
12230203Sbostic 			if (op->mileage == 0 || onecard(op)
12330203Sbostic 			    || (op->can_go && op->mileage >= 500))
12430203Sbostic 				goto done;
12530203Sbostic 			for (miles = 0, i = 0; i < NUMSAFE; i++)
12630203Sbostic 				if (op->safety[i] != S_PLAYED
12730203Sbostic 				    && pp->safety[i] == S_UNKNOWN)
12830203Sbostic 					miles++;
12930203Sbostic 			if (miles + safe == NUMSAFE)
13030203Sbostic 				goto extend;
13130203Sbostic 			for (miles = 0, i = 0; i < HAND_SZ; i++)
13230203Sbostic 				if ((safe = pp->hand[i]) <= C_200)
13330203Sbostic 					miles += Value[safe];
13430203Sbostic 			if (miles + (Topcard - Deck) * 3 > 1000)
13530203Sbostic 				goto extend;
13630203Sbostic 			goto done;
13730203Sbostic 		}
13830203Sbostic 	else
13930203Sbostic 		goto done;
14030203Sbostic }
14130203Sbostic 
14230203Sbostic /*
14330203Sbostic  *	Get a yes or no answer to the given question.  Saves are
14430203Sbostic  * also allowed.  Return TRUE if the answer was yes, FALSE if no.
14530203Sbostic  */
14630203Sbostic getyn(promptno)
14730203Sbostic register int	promptno; {
14830203Sbostic 
14930203Sbostic 	reg char	c;
15030203Sbostic 
15130203Sbostic 	Saved = FALSE;
15230203Sbostic 	for (;;) {
15330203Sbostic 		leaveok(Board, FALSE);
15430203Sbostic 		prompt(promptno);
15530203Sbostic 		clrtoeol();
15630203Sbostic 		refresh();
15730203Sbostic 		switch (c = readch()) {
15830203Sbostic 		  case 'n':	case 'N':
15930203Sbostic 			addch('N');
16030203Sbostic 			refresh();
16130203Sbostic 			leaveok(Board, TRUE);
16230203Sbostic 			return FALSE;
16330203Sbostic 		  case 'y':	case 'Y':
16430203Sbostic 			addch('Y');
16530203Sbostic 			refresh();
16630203Sbostic 			leaveok(Board, TRUE);
16730203Sbostic 			return TRUE;
16830203Sbostic 		  case 's':	case 'S':
16930203Sbostic 			addch('S');
17030203Sbostic 			refresh();
17130203Sbostic 			Saved = save();
17230203Sbostic 			continue;
173*52875Storek 		  case CTRL('L'):
174*52875Storek 			wrefresh(curscr);
175*52875Storek 			break;
17630203Sbostic 		  default:
17730203Sbostic 			addstr(unctrl(c));
17830203Sbostic 			refresh();
17934716Sbostic 			putchar('\07');
18030203Sbostic 			break;
18130203Sbostic 		}
18230203Sbostic 	}
18330203Sbostic }
18430203Sbostic 
18530203Sbostic /*
18630203Sbostic  *	Check to see if more games are desired.  If not, and game
18730203Sbostic  * came from a saved file, make sure that they don't want to restore
18830203Sbostic  * it.  Exit appropriately.
18930203Sbostic  */
19030203Sbostic check_more() {
19130203Sbostic 
19230203Sbostic 	On_exit = TRUE;
19330203Sbostic 	if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
19430203Sbostic 		if (getyn(ANOTHERGAMEPROMPT))
19530203Sbostic 			return;
19630203Sbostic 		else {
19730203Sbostic 			/*
19830203Sbostic 			 * must do accounting normally done in main()
19930203Sbostic 			 */
20030203Sbostic 			if (Player[PLAYER].total > Player[COMP].total)
20130203Sbostic 				Player[PLAYER].games++;
20230203Sbostic 			else if (Player[PLAYER].total < Player[COMP].total)
20330203Sbostic 				Player[COMP].games++;
20430203Sbostic 			Player[COMP].total = 0;
20530203Sbostic 			Player[PLAYER].total = 0;
20630203Sbostic 		}
20730203Sbostic 	else
20830203Sbostic 		if (getyn(ANOTHERHANDPROMPT))
20930203Sbostic 			return;
21030203Sbostic 	if (!Saved && getyn(SAVEGAMEPROMPT))
21130203Sbostic 		if (!save())
21230203Sbostic 			return;
213*52875Storek 	die(0);
21430203Sbostic }
21530203Sbostic 
21630203Sbostic readch()
21730203Sbostic {
21830203Sbostic 	reg int		cnt;
21930203Sbostic 	static char	c;
22030203Sbostic 
22130203Sbostic 	for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
22230203Sbostic 		if (cnt > 100)
22330203Sbostic 			exit(1);
22430203Sbostic 	return c;
22530203Sbostic }
226