xref: /openbsd/games/mille/mille.c (revision bda84ce9)
1 /*	$OpenBSD: mille.c,v 1.26 2021/10/23 11:22:49 mestre Exp $	*/
2 /*	$NetBSD: mille.c,v 1.4 1995/03/24 05:01:48 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <err.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #ifdef DEBUG
37 #include <string.h>
38 #endif
39 #include <unistd.h>
40 
41 #include "mille.h"
42 
43 /*
44  * @(#)mille.c	1.3 (Berkeley) 5/10/83
45  */
46 
47 int
main(int ac,char * av[])48 main(int ac, char *av[])
49 {
50 	bool	restore;
51 	extern char *__progname;
52 
53 #ifdef DEBUG
54 	if (strcmp(av[0], "a.out") == 0) {
55 		outf = fopen("q", "w");
56 		setvbuf(outf, NULL, _IONBF, 0);
57 		Debug = TRUE;
58 	}
59 #endif
60 	restore = FALSE;
61 	switch (ac) {
62 	  case 2:
63 		rest_f(av[1]);
64 		restore = TRUE;
65 	  case 1:
66 		break;
67 	  default:
68 		fprintf(stderr, "usage: %s [file]\n", __progname);
69 		return 1;
70 	}
71 	Play = PLAYER;
72 	initscr();
73 
74 	if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
75 		err(1, "pledge");
76 
77 	if ((LINES < 24) || (COLS < 80)) {
78 		endwin();
79 		fprintf(stderr, "Screen must be at least 24x80\n");
80 		return 1;
81 	}
82 	delwin(stdscr);
83 	stdscr = Board = newwin(BOARD_Y, BOARD_X, 0, 0);
84 	Score = newwin(SCORE_Y, SCORE_X, 0, 40);
85 	Miles = newwin(MILES_Y, MILES_X, 17, 0);
86 	leaveok(Score, TRUE);
87 	leaveok(Miles, TRUE);
88 	clearok(curscr, TRUE);
89 	cbreak();
90 	noecho();
91 	signal(SIGINT, rub);
92 	for (;;) {
93 		if (!restore || (Player[PLAYER].total >= 5000
94 		    || Player[COMP].total >= 5000)) {
95 			if (Player[COMP].total < Player[PLAYER].total)
96 				Player[PLAYER].games++;
97 			else if (Player[COMP].total > Player[PLAYER].total)
98 				Player[COMP].games++;
99 			Player[COMP].total = 0;
100 			Player[PLAYER].total = 0;
101 		}
102 		do {
103 			if (!restore)
104 				Handstart = Play = other(Handstart);
105 			if (!restore || On_exit) {
106 				shuffle();
107 				init();
108 			}
109 			newboard();
110 			if (restore)
111 				mvwaddstr(Score, ERR_Y, ERR_X, Initstr);
112 			prboard();
113 			do {
114 				domove();
115 				if (Finished)
116 					newscore();
117 				prboard();
118 			} while (!Finished);
119 			check_more();
120 			restore = On_exit = FALSE;
121 		} while (Player[COMP].total < 5000
122 		    && Player[PLAYER].total < 5000);
123 	}
124 }
125 
126 /*
127  *	Routine to trap rubouts, and make sure they really want to
128  * quit.
129  */
130 void
rub(int dummy)131 rub(int dummy)
132 {
133 	(void)signal(SIGINT, SIG_IGN);
134 	if (getyn(REALLYPROMPT))
135 		die(0);
136 	(void)signal(SIGINT, rub);
137 }
138 
139 /*
140  *	Time to go beddy-by
141  */
142 void
die(int code)143 die(int code)
144 {
145 	(void)signal(SIGINT, SIG_IGN);
146 	if (outf)
147 		fflush(outf);
148 	mvcur(0, COLS - 1, LINES - 1, 0);
149 	endwin();
150 	exit(code);
151 }
152