misc.c revision 1.19 1 /* $NetBSD: misc.c,v 1.19 2008/02/24 01:57:34 dholland 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.19 2008/02/24 01:57:34 dholland Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <ctype.h>
42 #include <limits.h>
43 #include <signal.h>
44
45 #include "monop.h"
46
47 /*
48 * This routine executes a truncated set of commands until a
49 * "yes or "no" answer is gotten.
50 */
51 int
52 getyn(prompt)
53 const char *prompt;
54 {
55 int com;
56
57 for (;;)
58 if ((com=getinp(prompt, yncoms)) < 2)
59 return com;
60 else
61 (*func[com-2])();
62 }
63
64 /*
65 * This routine tells the player if he's out of money.
66 */
67 void
68 notify()
69 {
70 if (cur_p->money < 0)
71 printf("That leaves you $%d in debt\n", -cur_p->money);
72 else if (cur_p->money == 0)
73 printf("that leaves you broke\n");
74 else if (fixing && !told_em && cur_p->money > 0) {
75 printf("-- You are now Solvent ---\n");
76 told_em = TRUE;
77 }
78 }
79
80 /*
81 * This routine switches to the next player
82 */
83 void
84 next_play()
85 {
86 player = (player + 1) % num_play;
87 cur_p = &play[player];
88 num_doub = 0;
89 }
90
91 /*
92 * This routine gets an integer from the keyboard after the
93 * given prompt.
94 */
95 int
96 get_int(prompt)
97 const char *prompt;
98 {
99 long num;
100 char *sp;
101 char buf[257];
102
103 for (;;) {
104 printf("%s", prompt);
105 fgets(buf, sizeof(buf), stdin);
106 if (feof(stdin))
107 return 0;
108 sp = strchr(buf, '\n');
109 if (sp)
110 *sp = '\0';
111 errno = 0;
112 num = strtol(buf, &sp, 10);
113 if (errno || strlen(sp) > 0 || num < 0 || num >= INT_MAX) {
114 printf("I can't understand that\n");
115 continue;
116 }
117 return num;
118 }
119 }
120
121 /*
122 * This routine sets the monopoly flag from the list given.
123 */
124 void
125 set_ownlist(pl)
126 int pl;
127 {
128 int num; /* general counter */
129 MON *orig; /* remember starting monop ptr */
130 OWN *op; /* current owned prop */
131 OWN *orig_op; /* original prop before loop */
132
133 op = play[pl].own_list;
134 #ifdef DEBUG
135 printf("op [%p] = play[pl [%d] ].own_list;\n", op, pl);
136 #endif
137 while (op) {
138 #ifdef DEBUG
139 printf("op->sqr->type = %d\n", op->sqr->type);
140 #endif
141 switch (op->sqr->type) {
142 case UTIL:
143 #ifdef DEBUG
144 printf(" case UTIL:\n");
145 #endif
146 for (num = 0; op && op->sqr->type == UTIL;
147 op = op->next)
148 num++;
149 play[pl].num_util = num;
150 #ifdef DEBUG
151 printf("play[pl].num_util = num [%d];\n", num);
152 #endif
153 break;
154 case RR:
155 #ifdef DEBUG
156 printf(" case RR:\n");
157 #endif
158 for (num = 0; op && op->sqr->type == RR;
159 op = op->next) {
160 #ifdef DEBUG
161 printf("iter: %d\n", num);
162 printf("op = %p, op->sqr = %p, "
163 "op->sqr->type = %d\n", op, op->sqr,
164 op->sqr->type);
165 #endif
166 num++;
167 }
168 play[pl].num_rr = num;
169 #ifdef DEBUG
170 printf("play[pl].num_rr = num [%d];\n", num);
171 #endif
172 break;
173 case PRPTY:
174 #ifdef DEBUG
175 printf(" case PRPTY:\n");
176 #endif
177 orig = op->sqr->desc->mon_desc;
178 orig_op = op;
179 num = 0;
180 while (op && op->sqr->desc->mon_desc == orig) {
181 #ifdef DEBUG
182 printf("iter: %d\n", num);
183 #endif
184 num++;
185 #ifdef DEBUG
186 printf("op = op->next ");
187 #endif
188 op = op->next;
189 #ifdef DEBUG
190 printf("[%p];\n", op);
191 #endif
192 }
193 #ifdef DEBUG
194 printf("num = %d\n", num);
195 #endif
196 if (orig == NULL) {
197 printf("panic: bad monopoly descriptor: "
198 "orig = %p\n", orig);
199 printf("player # %d\n", pl+1);
200 printhold(pl);
201 printf("orig_op = %p\n", orig_op);
202 if (orig_op) {
203 printf("orig_op->sqr->type = %d (PRPTY)\n",
204 orig_op->sqr->type);
205 printf("orig_op->next = %p\n",
206 orig_op->next);
207 printf("orig_op->sqr->desc = %p\n",
208 orig_op->sqr->desc);
209 }
210 printf("op = %p\n", op);
211 if (op) {
212 printf("op->sqr->type = %d (PRPTY)\n",
213 op->sqr->type);
214 printf("op->next = %p\n", op->next);
215 printf("op->sqr->desc = %p\n",
216 op->sqr->desc);
217 }
218 printf("num = %d\n", num);
219 exit(1);
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