xref: /netbsd/games/monop/misc.c (revision bf9ec67e)
1 /*	$NetBSD: misc.c,v 1.10 2001/01/16 02:41:17 cgd Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: misc.c,v 1.10 2001/01/16 02:41:17 cgd Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include "monop.ext"
46 #include <ctype.h>
47 #include <signal.h>
48 
49 /*
50  *	This routine executes a truncated set of commands until a
51  * "yes or "no" answer is gotten.
52  */
53 int
54 getyn(prompt)
55 	const char *prompt;
56 {
57 	int com;
58 
59 	for (;;)
60 		if ((com=getinp(prompt, yn)) < 2)
61 			return com;
62 		else
63 			(*func[com-2])();
64 }
65 
66 /*
67  *	This routine tells the player if he's out of money.
68  */
69 void
70 notify()
71 {
72 	if (cur_p->money < 0)
73 		printf("That leaves you $%d in debt\n", -cur_p->money);
74 	else if (cur_p->money == 0)
75 		printf("that leaves you broke\n");
76 	else if (fixing && !told_em && cur_p->money > 0) {
77 		printf("-- You are now Solvent ---\n");
78 		told_em = TRUE;
79 	}
80 }
81 
82 /*
83  *	This routine switches to the next player
84  */
85 void
86 next_play()
87 {
88 	player = (player + 1) % num_play;
89 	cur_p = &play[player];
90 	num_doub = 0;
91 }
92 
93 /*
94  *	This routine gets an integer from the keyboard after the
95  * given prompt.
96  */
97 int
98 get_int(prompt)
99 	const char *prompt;
100 {
101 	int num;
102 	char *sp;
103 	int c;
104 	char buf[257];
105 
106 	for (;;) {
107 inter:
108 		printf(prompt);
109 		num = 0;
110 		for (sp = buf; (c=getchar()) != '\n'; *sp++ = c)
111 			if (c == -1)	/* check for interrupted system call */
112 				goto inter;
113 		*sp = c;
114 		if (sp == buf)
115 			continue;
116 		for (sp = buf; isspace(*sp); sp++)
117 			continue;
118 		for (; isdigit(*sp); sp++)
119 			num = num * 10 + *sp - '0';
120 		if (*sp == '\n')
121 			return num;
122 		else
123 			printf("I can't understand that\n");
124 	}
125 }
126 
127 /*
128  *	This routine sets the monopoly flag from the list given.
129  */
130 void
131 set_ownlist(pl)
132 	int pl;
133 {
134 	int num;		/* general counter		*/
135 	MON *orig;		/* remember starting monop ptr	*/
136 	OWN *op;		/* current owned prop		*/
137 	OWN *orig_op;		/* origianl prop before loop	*/
138 
139 	op = play[pl].own_list;
140 #ifdef DEBUG
141 	printf("op [%d] = play[pl [%d] ].own_list;\n", op, pl);
142 #endif
143 	while (op) {
144 #ifdef DEBUG
145 		printf("op->sqr->type = %d\n", op->sqr->type);
146 #endif
147 		switch (op->sqr->type) {
148 		  case UTIL:
149 #ifdef DEBUG
150 			printf("  case UTIL:\n");
151 #endif
152 			for (num = 0; op && op->sqr->type == UTIL;
153 			    op = op->next)
154 				num++;
155 			play[pl].num_util = num;
156 #ifdef DEBUG
157 			printf("play[pl].num_util = num [%d];\n", num);
158 #endif
159 			break;
160 		  case RR:
161 #ifdef DEBUG
162 			printf("  case RR:\n");
163 #endif
164 			for (num = 0; op && op->sqr->type == RR;
165 			    op = op->next) {
166 #ifdef DEBUG
167 				printf("iter: %d\n", num);
168 				printf("op = %d, op->sqr = %d, "
169 				    "op->sqr->type = %d\n", op, op->sqr,
170 				    op->sqr->type);
171 #endif
172 				num++;
173 			}
174 			play[pl].num_rr = num;
175 #ifdef DEBUG
176 			printf("play[pl].num_rr = num [%d];\n", num);
177 #endif
178 			break;
179 		  case PRPTY:
180 #ifdef DEBUG
181 			printf("  case PRPTY:\n");
182 #endif
183 			orig = op->sqr->desc->mon_desc;
184 			orig_op = op;
185 			num = 0;
186 			while (op && op->sqr->desc->mon_desc == orig) {
187 #ifdef DEBUG
188 				printf("iter: %d\n", num);
189 #endif
190 				num++;
191 #ifdef DEBUG
192 				printf("op = op->next ");
193 #endif
194 				op = op->next;
195 #ifdef DEBUG
196 				printf("[%d];\n", op);
197 #endif
198 			}
199 #ifdef DEBUG
200 			printf("num = %d\n");
201 #endif
202 			if (orig == 0) {
203 				printf("panic:  bad monopoly descriptor: "
204 				    "orig = %p\n", orig);
205 				printf("player # %d\n", pl+1);
206 				printhold(pl);
207 				printf("orig_op = %p\n", orig_op);
208 				printf("orig_op->sqr->type = %d (PRPTY)\n",
209 				    op->sqr->type);
210 				printf("orig_op->next = %p\n", op->next);
211 				printf("orig_op->sqr->desc = %p\n",
212 				    op->sqr->desc);
213 				printf("op = %p\n", op);
214 				printf("op->sqr->type = %d (PRPTY)\n",
215 				    op->sqr->type);
216 				printf("op->next = %p\n", op->next);
217 				printf("op->sqr->desc = %p\n", op->sqr->desc);
218 				printf("num = %d\n", num);
219 			}
220 #ifdef DEBUG
221 			printf("orig->num_in = %d\n", orig->num_in);
222 #endif
223 			if (num == orig->num_in)
224 				is_monop(orig, pl);
225 			else
226 				is_not_monop(orig);
227 			break;
228 		}
229 	}
230 }
231 
232 /*
233  *	This routine sets things up as if it is a new monopoly
234  */
235 void
236 is_monop(mp, pl)
237 	MON *mp;
238 	int pl;
239 {
240 	int i;
241 
242 	mp->owner = pl;
243 	mp->num_own = mp->num_in;
244 	for (i = 0; i < mp->num_in; i++)
245 		mp->sq[i]->desc->monop = TRUE;
246 	mp->name = mp->mon_n;
247 }
248 
249 /*
250  *	This routine sets things up as if it is no longer a monopoly
251  */
252 void
253 is_not_monop(mp)
254 	MON *mp;
255 {
256 	int i;
257 
258 	mp->owner = -1;
259 	for (i = 0; i < mp->num_in; i++)
260 		mp->sq[i]->desc->monop = FALSE;
261 	mp->name = mp->not_m;
262 }
263 
264 /*
265  *	This routine gives a list of the current player's routine
266  */
267 void
268 list()
269 {
270 	printhold(player);
271 }
272 
273 /*
274  *	This routine gives a list of a given players holdings
275  */
276 void
277 list_all()
278 {
279 	int pl;
280 
281 	while ((pl = getinp("Whose holdings do you want to see? ", name_list))
282 	    < num_play)
283 		printhold(pl);
284 }
285 
286 /*
287  *	This routine gives the players a chance before it exits.
288  */
289 void
290 quit()
291 {
292 	putchar('\n');
293 	if (getyn("Do you all really want to quit? ") == 0)
294 		exit(0);
295 }
296