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