xref: /csrg-svn/games/mille/mille.c (revision 60812)
130202Sbostic /*
2*60812Sbostic  * Copyright (c) 1982, 1993
3*60812Sbostic  *	The Regents of the University of California.  All rights reserved.
433173Sbostic  *
542578Sbostic  * %sccs.include.redist.c%
630202Sbostic  */
730202Sbostic 
830202Sbostic #ifndef lint
9*60812Sbostic static char copyright[] =
10*60812Sbostic "@(#) Copyright (c) 1982, 1993\n\
11*60812Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233173Sbostic #endif /* not lint */
1330202Sbostic 
1433173Sbostic #ifndef lint
15*60812Sbostic static char sccsid[] = "@(#)mille.c	8.1 (Berkeley) 05/31/93";
1633173Sbostic #endif /* not lint */
1733173Sbostic 
1830202Sbostic # include	"mille.h"
1930202Sbostic # include	<signal.h>
2030202Sbostic # ifdef attron
2130202Sbostic #	include	<term.h>
2230202Sbostic # endif	attron
2330202Sbostic 
2430202Sbostic /*
2530202Sbostic  * @(#)mille.c	1.3 (Berkeley) 5/10/83
2630202Sbostic  */
2730202Sbostic 
2846750Sbostic void	rub();
2930202Sbostic 
main(ac,av)3030202Sbostic main(ac, av)
3130202Sbostic reg int		ac;
3230202Sbostic reg char	*av[]; {
3330202Sbostic 
3430202Sbostic 	reg bool	restore;
3530202Sbostic 
3633173Sbostic 	/* run as the user */
3733173Sbostic 	setuid(getuid());
3833173Sbostic 
3930202Sbostic 	if (strcmp(av[0], "a.out") == 0) {
4030202Sbostic 		outf = fopen("q", "w");
4130202Sbostic 		setbuf(outf, (char *)NULL);
4230202Sbostic 		Debug = TRUE;
4330202Sbostic 	}
4430202Sbostic 	restore = FALSE;
4530202Sbostic 	switch (ac) {
4630202Sbostic 	  case 2:
4730202Sbostic 		rest_f(av[1]);
4830202Sbostic 		restore = TRUE;
4930202Sbostic 	  case 1:
5030202Sbostic 		break;
5130202Sbostic 	  default:
5230202Sbostic 		printf("usage: milles [ restore_file ]\n");
5330202Sbostic 		exit(-1);
5430202Sbostic 		/* NOTREACHED */
5530202Sbostic 	}
5630202Sbostic 	Play = PLAYER;
5730202Sbostic 	initscr();
5830202Sbostic 	delwin(stdscr);
5930202Sbostic 	stdscr = Board = newwin(BOARD_Y, BOARD_X, 0, 0);
6030202Sbostic 	Score = newwin(SCORE_Y, SCORE_X, 0, 40);
6130202Sbostic 	Miles = newwin(MILES_Y, MILES_X, 17, 0);
6230202Sbostic #ifdef attron
6330202Sbostic 	idlok(Board, TRUE);
6430202Sbostic 	idlok(Score, TRUE);
6530202Sbostic 	idlok(Miles, TRUE);
6630202Sbostic #endif
6730202Sbostic 	leaveok(Score, TRUE);
6830202Sbostic 	leaveok(Miles, TRUE);
6930202Sbostic 	clearok(curscr, TRUE);
7030202Sbostic # ifndef PROF
7130202Sbostic 	srandom(getpid());
7230202Sbostic # else
7330202Sbostic 	srandom(0);
7430202Sbostic # endif
7530202Sbostic 	crmode();
7630202Sbostic 	noecho();
7730202Sbostic 	signal(SIGINT, rub);
7830202Sbostic 	for (;;) {
7930202Sbostic 		if (!restore || (Player[PLAYER].total >= 5000
8030202Sbostic 		    || Player[COMP].total >= 5000)) {
8130202Sbostic 			if (Player[COMP].total < Player[PLAYER].total)
8230202Sbostic 				Player[PLAYER].games++;
8330202Sbostic 			else if (Player[COMP].total > Player[PLAYER].total)
8430202Sbostic 				Player[COMP].games++;
8530202Sbostic 			Player[COMP].total = 0;
8630202Sbostic 			Player[PLAYER].total = 0;
8730202Sbostic 		}
8830202Sbostic 		do {
8930202Sbostic 			if (!restore)
9030202Sbostic 				Handstart = Play = other(Handstart);
9130202Sbostic 			if (!restore || On_exit) {
9230202Sbostic 				shuffle();
9330202Sbostic 				init();
9430202Sbostic 			}
9530202Sbostic 			newboard();
9630202Sbostic 			if (restore)
9730202Sbostic 				mvwaddstr(Score, ERR_Y, ERR_X, Initstr);
9830202Sbostic 			prboard();
9930202Sbostic 			do {
10030202Sbostic 				domove();
10130202Sbostic 				if (Finished)
10230202Sbostic 					newscore();
10330202Sbostic 				prboard();
10430202Sbostic 			} while (!Finished);
10530202Sbostic 			check_more();
10630202Sbostic 			restore = On_exit = FALSE;
10730202Sbostic 		} while (Player[COMP].total < 5000
10830202Sbostic 		    && Player[PLAYER].total < 5000);
10930202Sbostic 	}
11030202Sbostic }
11130202Sbostic 
11230202Sbostic /*
11330202Sbostic  *	Routine to trap rubouts, and make sure they really want to
11430202Sbostic  * quit.
11530202Sbostic  */
11646750Sbostic void
rub()11730202Sbostic rub() {
11830202Sbostic 
11933173Sbostic 	(void)signal(SIGINT, SIG_IGN);
12030202Sbostic 	if (getyn(REALLYPROMPT))
12152874Storek 		die(0);
12233173Sbostic 	(void)signal(SIGINT, rub);
12330202Sbostic }
12430202Sbostic 
12530202Sbostic /*
12630202Sbostic  *	Time to go beddy-by
12730202Sbostic  */
die(code)12852874Storek die(code)
12952874Storek int code; {
13030202Sbostic 
13133173Sbostic 	(void)signal(SIGINT, SIG_IGN);
13230202Sbostic 	if (outf)
13330202Sbostic 		fflush(outf);
13430202Sbostic 	mvcur(0, COLS - 1, LINES - 1, 0);
13530202Sbostic 	endwin();
13652874Storek 	exit(code);
13730202Sbostic }
138