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