xref: /netbsd-src/games/monop/misc.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: misc.c,v 1.13 2004/11/05 21:30:32 dsl 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.13 2004/11/05 21:30:32 dsl 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 				printf("orig_op->sqr->type = %d (PRPTY)\n",
205 				    op->sqr->type);
206 				printf("orig_op->next = %p\n", op->next);
207 				printf("orig_op->sqr->desc = %p\n",
208 				    op->sqr->desc);
209 				printf("op = %p\n", op);
210 				printf("op->sqr->type = %d (PRPTY)\n",
211 				    op->sqr->type);
212 				printf("op->next = %p\n", op->next);
213 				printf("op->sqr->desc = %p\n", op->sqr->desc);
214 				printf("num = %d\n", num);
215 			}
216 #ifdef DEBUG
217 			printf("orig->num_in = %d\n", orig->num_in);
218 #endif
219 			if (num == orig->num_in)
220 				is_monop(orig, pl);
221 			else
222 				is_not_monop(orig);
223 			break;
224 		}
225 	}
226 }
227 
228 /*
229  *	This routine sets things up as if it is a new monopoly
230  */
231 void
232 is_monop(mp, pl)
233 	MON *mp;
234 	int pl;
235 {
236 	int i;
237 
238 	mp->owner = pl;
239 	mp->num_own = mp->num_in;
240 	for (i = 0; i < mp->num_in; i++)
241 		mp->sq[i]->desc->monop = TRUE;
242 	mp->name = mp->mon_n;
243 }
244 
245 /*
246  *	This routine sets things up as if it is no longer a monopoly
247  */
248 void
249 is_not_monop(mp)
250 	MON *mp;
251 {
252 	int i;
253 
254 	mp->owner = -1;
255 	for (i = 0; i < mp->num_in; i++)
256 		mp->sq[i]->desc->monop = FALSE;
257 	mp->name = mp->not_m;
258 }
259 
260 /*
261  *	This routine gives a list of the current player's routine
262  */
263 void
264 list()
265 {
266 	printhold(player);
267 }
268 
269 /*
270  *	This routine gives a list of a given players holdings
271  */
272 void
273 list_all()
274 {
275 	int pl;
276 
277 	while ((pl = getinp("Whose holdings do you want to see? ", name_list))
278 	    < num_play)
279 		printhold(pl);
280 }
281 
282 /*
283  *	This routine gives the players a chance before it exits.
284  */
285 void
286 quit()
287 {
288 	putchar('\n');
289 	if (getyn("Do you all really want to quit? ") == 0)
290 		exit(0);
291 }
292