xref: /csrg-svn/games/mille/misc.c (revision 33175)
130203Sbostic /*
230203Sbostic  * Copyright (c) 1983 Regents of the University of California.
3*33175Sbostic  * All rights reserved.
4*33175Sbostic  *
5*33175Sbostic  * Redistribution and use in source and binary forms are permitted
6*33175Sbostic  * provided that this notice is preserved and that due credit is given
7*33175Sbostic  * to the University of California at Berkeley. The name of the University
8*33175Sbostic  * may not be used to endorse or promote products derived from this
9*33175Sbostic  * software without specific prior written permission. This software
10*33175Sbostic  * is provided ``as is'' without express or implied warranty.
1130203Sbostic  */
1230203Sbostic 
1330203Sbostic #ifndef lint
14*33175Sbostic static char sccsid[] = "@(#)misc.c	5.2 (Berkeley) 12/29/87";
15*33175Sbostic #endif /* not lint */
1630203Sbostic 
1730203Sbostic #include	"mille.h"
1830203Sbostic #ifndef	unctrl
1930203Sbostic #include	"unctrl.h"
2030203Sbostic #endif
2130203Sbostic 
2230203Sbostic # include	<sys/file.h>
2330203Sbostic 
2430203Sbostic # ifdef	attron
2530203Sbostic #	include	<term.h>
2630203Sbostic #	define	_tty	cur_term->Nttyb
2730203Sbostic # endif	attron
2830203Sbostic 
2930203Sbostic /*
3030203Sbostic  * @(#)misc.c	1.2 (Berkeley) 3/28/83
3130203Sbostic  */
3230203Sbostic 
3330203Sbostic #define	NUMSAFE	4
3430203Sbostic 
3530203Sbostic /* VARARGS1 */
3630203Sbostic error(str, arg)
3730203Sbostic char	*str;
3830203Sbostic {
3930203Sbostic 	stdscr = Score;
4030203Sbostic 	mvprintw(ERR_Y, ERR_X, str, arg);
4130203Sbostic 	clrtoeol();
4230203Sbostic 	putchar('');
4330203Sbostic 	refresh();
4430203Sbostic 	stdscr = Board;
4530203Sbostic 	return FALSE;
4630203Sbostic }
4730203Sbostic 
4830203Sbostic CARD
4930203Sbostic getcard()
5030203Sbostic {
5130203Sbostic 	reg int		c, c1;
5230203Sbostic 
5330203Sbostic 	for (;;) {
5430203Sbostic 		while ((c = readch()) == '\n' || c == '\r' || c == ' ')
5530203Sbostic 			continue;
5630203Sbostic 		if (islower(c))
5730203Sbostic 			c = toupper(c);
5830203Sbostic 		if (c == killchar() || c == erasechar())
5930203Sbostic 			return -1;
6030203Sbostic 		addstr(unctrl(c));
6130203Sbostic 		clrtoeol();
6230203Sbostic 		switch (c) {
6330203Sbostic 		  case '1':	case '2':	case '3':
6430203Sbostic 		  case '4':	case '5':	case '6':
6530203Sbostic 			c -= '0';
6630203Sbostic 			break;
6730203Sbostic 		  case '0':	case 'P':	case 'p':
6830203Sbostic 			c = 0;
6930203Sbostic 			break;
7030203Sbostic 		  default:
7130203Sbostic 			putchar('');
7230203Sbostic 			addch('\b');
7330203Sbostic 			if (!isprint(c))
7430203Sbostic 				addch('\b');
7530203Sbostic 			c = -1;
7630203Sbostic 			break;
7730203Sbostic 		}
7830203Sbostic 		refresh();
7930203Sbostic 		if (c >= 0) {
8030203Sbostic 			while ((c1=readch()) != '\r' && c1 != '\n' && c1 != ' ')
8130203Sbostic 				if (c1 == killchar())
8230203Sbostic 					return -1;
8330203Sbostic 				else if (c1 == erasechar()) {
8430203Sbostic 					addch('\b');
8530203Sbostic 					clrtoeol();
8630203Sbostic 					refresh();
8730203Sbostic 					goto cont;
8830203Sbostic 				}
8930203Sbostic 				else
9030203Sbostic 					write(0, "", 1);
9130203Sbostic 			return c;
9230203Sbostic 		}
9330203Sbostic cont:		;
9430203Sbostic 	}
9530203Sbostic }
9630203Sbostic 
9730203Sbostic check_ext(forcomp)
9830203Sbostic reg bool	forcomp; {
9930203Sbostic 
10030203Sbostic 
10130203Sbostic 	if (End == 700)
10230203Sbostic 		if (Play == PLAYER) {
10330203Sbostic 			if (getyn(EXTENSIONPROMPT)) {
10430203Sbostic extend:
10530203Sbostic 				if (!forcomp)
10630203Sbostic 					End = 1000;
10730203Sbostic 				return TRUE;
10830203Sbostic 			}
10930203Sbostic 			else {
11030203Sbostic done:
11130203Sbostic 				if (!forcomp)
11230203Sbostic 					Finished = TRUE;
11330203Sbostic 				return FALSE;
11430203Sbostic 			}
11530203Sbostic 		}
11630203Sbostic 		else {
11730203Sbostic 			reg PLAY	*pp, *op;
11830203Sbostic 			reg int		i, safe, miles;
11930203Sbostic 
12030203Sbostic 			pp = &Player[COMP];
12130203Sbostic 			op = &Player[PLAYER];
12230203Sbostic 			for (safe = 0, i = 0; i < NUMSAFE; i++)
12330203Sbostic 				if (pp->safety[i] != S_UNKNOWN)
12430203Sbostic 					safe++;
12530203Sbostic 			if (safe < 2)
12630203Sbostic 				goto done;
12730203Sbostic 			if (op->mileage == 0 || onecard(op)
12830203Sbostic 			    || (op->can_go && op->mileage >= 500))
12930203Sbostic 				goto done;
13030203Sbostic 			for (miles = 0, i = 0; i < NUMSAFE; i++)
13130203Sbostic 				if (op->safety[i] != S_PLAYED
13230203Sbostic 				    && pp->safety[i] == S_UNKNOWN)
13330203Sbostic 					miles++;
13430203Sbostic 			if (miles + safe == NUMSAFE)
13530203Sbostic 				goto extend;
13630203Sbostic 			for (miles = 0, i = 0; i < HAND_SZ; i++)
13730203Sbostic 				if ((safe = pp->hand[i]) <= C_200)
13830203Sbostic 					miles += Value[safe];
13930203Sbostic 			if (miles + (Topcard - Deck) * 3 > 1000)
14030203Sbostic 				goto extend;
14130203Sbostic 			goto done;
14230203Sbostic 		}
14330203Sbostic 	else
14430203Sbostic 		goto done;
14530203Sbostic }
14630203Sbostic 
14730203Sbostic /*
14830203Sbostic  *	Get a yes or no answer to the given question.  Saves are
14930203Sbostic  * also allowed.  Return TRUE if the answer was yes, FALSE if no.
15030203Sbostic  */
15130203Sbostic getyn(promptno)
15230203Sbostic register int	promptno; {
15330203Sbostic 
15430203Sbostic 	reg char	c;
15530203Sbostic 
15630203Sbostic 	Saved = FALSE;
15730203Sbostic 	for (;;) {
15830203Sbostic 		leaveok(Board, FALSE);
15930203Sbostic 		prompt(promptno);
16030203Sbostic 		clrtoeol();
16130203Sbostic 		refresh();
16230203Sbostic 		switch (c = readch()) {
16330203Sbostic 		  case 'n':	case 'N':
16430203Sbostic 			addch('N');
16530203Sbostic 			refresh();
16630203Sbostic 			leaveok(Board, TRUE);
16730203Sbostic 			return FALSE;
16830203Sbostic 		  case 'y':	case 'Y':
16930203Sbostic 			addch('Y');
17030203Sbostic 			refresh();
17130203Sbostic 			leaveok(Board, TRUE);
17230203Sbostic 			return TRUE;
17330203Sbostic 		  case 's':	case 'S':
17430203Sbostic 			addch('S');
17530203Sbostic 			refresh();
17630203Sbostic 			Saved = save();
17730203Sbostic 			continue;
17830203Sbostic 		  default:
17930203Sbostic 			addstr(unctrl(c));
18030203Sbostic 			refresh();
18130203Sbostic 			putchar('');
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  */
19230203Sbostic check_more() {
19330203Sbostic 
19430203Sbostic 	flush_input();
19530203Sbostic 
19630203Sbostic 	On_exit = TRUE;
19730203Sbostic 	if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
19830203Sbostic 		if (getyn(ANOTHERGAMEPROMPT))
19930203Sbostic 			return;
20030203Sbostic 		else {
20130203Sbostic 			/*
20230203Sbostic 			 * must do accounting normally done in main()
20330203Sbostic 			 */
20430203Sbostic 			if (Player[PLAYER].total > Player[COMP].total)
20530203Sbostic 				Player[PLAYER].games++;
20630203Sbostic 			else if (Player[PLAYER].total < Player[COMP].total)
20730203Sbostic 				Player[COMP].games++;
20830203Sbostic 			Player[COMP].total = 0;
20930203Sbostic 			Player[PLAYER].total = 0;
21030203Sbostic 		}
21130203Sbostic 	else
21230203Sbostic 		if (getyn(ANOTHERHANDPROMPT))
21330203Sbostic 			return;
21430203Sbostic 	if (!Saved && getyn(SAVEGAMEPROMPT))
21530203Sbostic 		if (!save())
21630203Sbostic 			return;
21730203Sbostic 	die();
21830203Sbostic }
21930203Sbostic 
22030203Sbostic readch()
22130203Sbostic {
22230203Sbostic 	reg int		cnt;
22330203Sbostic 	static char	c;
22430203Sbostic 
22530203Sbostic 	for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
22630203Sbostic 		if (cnt > 100)
22730203Sbostic 			exit(1);
22830203Sbostic 	return c;
22930203Sbostic }
23030203Sbostic 
23130203Sbostic flush_input()
23230203Sbostic {
23330203Sbostic # ifdef	TIOCFLUSH
23430203Sbostic 	static int	ioctl_args = FREAD;
23530203Sbostic 
23630203Sbostic 	(void) ioctl(fileno(stdin), TIOCFLUSH, &ioctl_args);
23730203Sbostic # else
23830203Sbostic 	fflush(stdin);
23930203Sbostic # endif
24030203Sbostic }
241