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