xref: /csrg-svn/games/mille/misc.c (revision 34792)
130203Sbostic /*
230203Sbostic  * Copyright (c) 1983 Regents of the University of California.
333175Sbostic  * All rights reserved.
433175Sbostic  *
533175Sbostic  * Redistribution and use in source and binary forms are permitted
6*34792Sbostic  * provided that the above copyright notice and this paragraph are
7*34792Sbostic  * duplicated in all such forms and that any documentation,
8*34792Sbostic  * advertising materials, and other materials related to such
9*34792Sbostic  * distribution and use acknowledge that the software was developed
10*34792Sbostic  * by the University of California, Berkeley.  The name of the
11*34792Sbostic  * University may not be used to endorse or promote products derived
12*34792Sbostic  * from this software without specific prior written permission.
13*34792Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34792Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34792Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1630203Sbostic  */
1730203Sbostic 
1830203Sbostic #ifndef lint
19*34792Sbostic static char sccsid[] = "@(#)misc.c	5.4 (Berkeley) 06/18/88";
2033175Sbostic #endif /* not lint */
2130203Sbostic 
2230203Sbostic #include	"mille.h"
2330203Sbostic #ifndef	unctrl
2430203Sbostic #include	"unctrl.h"
2530203Sbostic #endif
2630203Sbostic 
2730203Sbostic # include	<sys/file.h>
2830203Sbostic 
2930203Sbostic # ifdef	attron
3030203Sbostic #	include	<term.h>
3130203Sbostic #	define	_tty	cur_term->Nttyb
3230203Sbostic # endif	attron
3330203Sbostic 
3430203Sbostic /*
3530203Sbostic  * @(#)misc.c	1.2 (Berkeley) 3/28/83
3630203Sbostic  */
3730203Sbostic 
3830203Sbostic #define	NUMSAFE	4
3930203Sbostic 
4030203Sbostic /* VARARGS1 */
4130203Sbostic error(str, arg)
4230203Sbostic char	*str;
4330203Sbostic {
4430203Sbostic 	stdscr = Score;
4530203Sbostic 	mvprintw(ERR_Y, ERR_X, str, arg);
4630203Sbostic 	clrtoeol();
4734716Sbostic 	putchar('\07');
4830203Sbostic 	refresh();
4930203Sbostic 	stdscr = Board;
5030203Sbostic 	return FALSE;
5130203Sbostic }
5230203Sbostic 
5330203Sbostic CARD
5430203Sbostic getcard()
5530203Sbostic {
5630203Sbostic 	reg int		c, c1;
5730203Sbostic 
5830203Sbostic 	for (;;) {
5930203Sbostic 		while ((c = readch()) == '\n' || c == '\r' || c == ' ')
6030203Sbostic 			continue;
6130203Sbostic 		if (islower(c))
6230203Sbostic 			c = toupper(c);
6330203Sbostic 		if (c == killchar() || c == erasechar())
6430203Sbostic 			return -1;
6530203Sbostic 		addstr(unctrl(c));
6630203Sbostic 		clrtoeol();
6730203Sbostic 		switch (c) {
6830203Sbostic 		  case '1':	case '2':	case '3':
6930203Sbostic 		  case '4':	case '5':	case '6':
7030203Sbostic 			c -= '0';
7130203Sbostic 			break;
7230203Sbostic 		  case '0':	case 'P':	case 'p':
7330203Sbostic 			c = 0;
7430203Sbostic 			break;
7530203Sbostic 		  default:
7634716Sbostic 			putchar('\07');
7730203Sbostic 			addch('\b');
7830203Sbostic 			if (!isprint(c))
7930203Sbostic 				addch('\b');
8030203Sbostic 			c = -1;
8130203Sbostic 			break;
8230203Sbostic 		}
8330203Sbostic 		refresh();
8430203Sbostic 		if (c >= 0) {
8530203Sbostic 			while ((c1=readch()) != '\r' && c1 != '\n' && c1 != ' ')
8630203Sbostic 				if (c1 == killchar())
8730203Sbostic 					return -1;
8830203Sbostic 				else if (c1 == erasechar()) {
8930203Sbostic 					addch('\b');
9030203Sbostic 					clrtoeol();
9130203Sbostic 					refresh();
9230203Sbostic 					goto cont;
9330203Sbostic 				}
9430203Sbostic 				else
9534716Sbostic 					write(0, "\07", 1);
9630203Sbostic 			return c;
9730203Sbostic 		}
9830203Sbostic cont:		;
9930203Sbostic 	}
10030203Sbostic }
10130203Sbostic 
10230203Sbostic check_ext(forcomp)
10330203Sbostic reg bool	forcomp; {
10430203Sbostic 
10530203Sbostic 
10630203Sbostic 	if (End == 700)
10730203Sbostic 		if (Play == PLAYER) {
10830203Sbostic 			if (getyn(EXTENSIONPROMPT)) {
10930203Sbostic extend:
11030203Sbostic 				if (!forcomp)
11130203Sbostic 					End = 1000;
11230203Sbostic 				return TRUE;
11330203Sbostic 			}
11430203Sbostic 			else {
11530203Sbostic done:
11630203Sbostic 				if (!forcomp)
11730203Sbostic 					Finished = TRUE;
11830203Sbostic 				return FALSE;
11930203Sbostic 			}
12030203Sbostic 		}
12130203Sbostic 		else {
12230203Sbostic 			reg PLAY	*pp, *op;
12330203Sbostic 			reg int		i, safe, miles;
12430203Sbostic 
12530203Sbostic 			pp = &Player[COMP];
12630203Sbostic 			op = &Player[PLAYER];
12730203Sbostic 			for (safe = 0, i = 0; i < NUMSAFE; i++)
12830203Sbostic 				if (pp->safety[i] != S_UNKNOWN)
12930203Sbostic 					safe++;
13030203Sbostic 			if (safe < 2)
13130203Sbostic 				goto done;
13230203Sbostic 			if (op->mileage == 0 || onecard(op)
13330203Sbostic 			    || (op->can_go && op->mileage >= 500))
13430203Sbostic 				goto done;
13530203Sbostic 			for (miles = 0, i = 0; i < NUMSAFE; i++)
13630203Sbostic 				if (op->safety[i] != S_PLAYED
13730203Sbostic 				    && pp->safety[i] == S_UNKNOWN)
13830203Sbostic 					miles++;
13930203Sbostic 			if (miles + safe == NUMSAFE)
14030203Sbostic 				goto extend;
14130203Sbostic 			for (miles = 0, i = 0; i < HAND_SZ; i++)
14230203Sbostic 				if ((safe = pp->hand[i]) <= C_200)
14330203Sbostic 					miles += Value[safe];
14430203Sbostic 			if (miles + (Topcard - Deck) * 3 > 1000)
14530203Sbostic 				goto extend;
14630203Sbostic 			goto done;
14730203Sbostic 		}
14830203Sbostic 	else
14930203Sbostic 		goto done;
15030203Sbostic }
15130203Sbostic 
15230203Sbostic /*
15330203Sbostic  *	Get a yes or no answer to the given question.  Saves are
15430203Sbostic  * also allowed.  Return TRUE if the answer was yes, FALSE if no.
15530203Sbostic  */
15630203Sbostic getyn(promptno)
15730203Sbostic register int	promptno; {
15830203Sbostic 
15930203Sbostic 	reg char	c;
16030203Sbostic 
16130203Sbostic 	Saved = FALSE;
16230203Sbostic 	for (;;) {
16330203Sbostic 		leaveok(Board, FALSE);
16430203Sbostic 		prompt(promptno);
16530203Sbostic 		clrtoeol();
16630203Sbostic 		refresh();
16730203Sbostic 		switch (c = readch()) {
16830203Sbostic 		  case 'n':	case 'N':
16930203Sbostic 			addch('N');
17030203Sbostic 			refresh();
17130203Sbostic 			leaveok(Board, TRUE);
17230203Sbostic 			return FALSE;
17330203Sbostic 		  case 'y':	case 'Y':
17430203Sbostic 			addch('Y');
17530203Sbostic 			refresh();
17630203Sbostic 			leaveok(Board, TRUE);
17730203Sbostic 			return TRUE;
17830203Sbostic 		  case 's':	case 'S':
17930203Sbostic 			addch('S');
18030203Sbostic 			refresh();
18130203Sbostic 			Saved = save();
18230203Sbostic 			continue;
18330203Sbostic 		  default:
18430203Sbostic 			addstr(unctrl(c));
18530203Sbostic 			refresh();
18634716Sbostic 			putchar('\07');
18730203Sbostic 			break;
18830203Sbostic 		}
18930203Sbostic 	}
19030203Sbostic }
19130203Sbostic 
19230203Sbostic /*
19330203Sbostic  *	Check to see if more games are desired.  If not, and game
19430203Sbostic  * came from a saved file, make sure that they don't want to restore
19530203Sbostic  * it.  Exit appropriately.
19630203Sbostic  */
19730203Sbostic check_more() {
19830203Sbostic 
19930203Sbostic 	flush_input();
20030203Sbostic 
20130203Sbostic 	On_exit = TRUE;
20230203Sbostic 	if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
20330203Sbostic 		if (getyn(ANOTHERGAMEPROMPT))
20430203Sbostic 			return;
20530203Sbostic 		else {
20630203Sbostic 			/*
20730203Sbostic 			 * must do accounting normally done in main()
20830203Sbostic 			 */
20930203Sbostic 			if (Player[PLAYER].total > Player[COMP].total)
21030203Sbostic 				Player[PLAYER].games++;
21130203Sbostic 			else if (Player[PLAYER].total < Player[COMP].total)
21230203Sbostic 				Player[COMP].games++;
21330203Sbostic 			Player[COMP].total = 0;
21430203Sbostic 			Player[PLAYER].total = 0;
21530203Sbostic 		}
21630203Sbostic 	else
21730203Sbostic 		if (getyn(ANOTHERHANDPROMPT))
21830203Sbostic 			return;
21930203Sbostic 	if (!Saved && getyn(SAVEGAMEPROMPT))
22030203Sbostic 		if (!save())
22130203Sbostic 			return;
22230203Sbostic 	die();
22330203Sbostic }
22430203Sbostic 
22530203Sbostic readch()
22630203Sbostic {
22730203Sbostic 	reg int		cnt;
22830203Sbostic 	static char	c;
22930203Sbostic 
23030203Sbostic 	for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
23130203Sbostic 		if (cnt > 100)
23230203Sbostic 			exit(1);
23330203Sbostic 	return c;
23430203Sbostic }
23530203Sbostic 
23630203Sbostic flush_input()
23730203Sbostic {
23830203Sbostic # ifdef	TIOCFLUSH
23930203Sbostic 	static int	ioctl_args = FREAD;
24030203Sbostic 
24130203Sbostic 	(void) ioctl(fileno(stdin), TIOCFLUSH, &ioctl_args);
24230203Sbostic # else
24330203Sbostic 	fflush(stdin);
24430203Sbostic # endif
24530203Sbostic }
246