misc.c revision 1.1.1.2 1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. 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[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
36 #endif /* not lint */
37
38 # include "monop.ext"
39 # include <ctype.h>
40 # include <signal.h>
41
42 /*
43 * This routine executes a truncated set of commands until a
44 * "yes or "no" answer is gotten.
45 */
46 getyn(prompt)
47 reg char *prompt; {
48
49 reg int com;
50
51 for (;;)
52 if ((com=getinp(prompt, yn)) < 2)
53 return com;
54 else
55 (*func[com-2])();
56 }
57 /*
58 * This routine tells the player if he's out of money.
59 */
60 notify() {
61
62 if (cur_p->money < 0)
63 printf("That leaves you $%d in debt\n", -cur_p->money);
64 else if (cur_p->money == 0)
65 printf("that leaves you broke\n");
66 else if (fixing && !told_em && cur_p->money > 0) {
67 printf("-- You are now Solvent ---\n");
68 told_em = TRUE;
69 }
70 }
71 /*
72 * This routine switches to the next player
73 */
74 next_play() {
75
76 player = ++player % num_play;
77 cur_p = &play[player];
78 num_doub = 0;
79 }
80 /*
81 * This routine gets an integer from the keyboard after the
82 * given prompt.
83 */
84 get_int(prompt)
85 reg char *prompt; {
86
87 reg int num;
88 reg char *sp;
89 char buf[257];
90
91 for (;;) {
92 inter:
93 printf(prompt);
94 num = 0;
95 for (sp = buf; (*sp=getchar()) != '\n'; sp++)
96 if (*sp == -1) /* check for interrupted system call */
97 goto inter;
98 if (sp == buf)
99 continue;
100 for (sp = buf; isspace(*sp); sp++)
101 continue;
102 for (; isdigit(*sp); sp++)
103 num = num * 10 + *sp - '0';
104 if (*sp == '\n')
105 return num;
106 else
107 printf("I can't understand that\n");
108 }
109 }
110 /*
111 * This routine sets the monopoly flag from the list given.
112 */
113 set_ownlist(pl)
114 int pl; {
115
116 reg int num; /* general counter */
117 reg MON *orig; /* remember starting monop ptr */
118 reg OWN *op; /* current owned prop */
119 OWN *orig_op; /* origianl prop before loop */
120
121 op = play[pl].own_list;
122 #ifdef DEBUG
123 printf("op [%d] = play[pl [%d] ].own_list;\n", op, pl);
124 #endif
125 while (op) {
126 #ifdef DEBUG
127 printf("op->sqr->type = %d\n", op->sqr->type);
128 #endif
129 switch (op->sqr->type) {
130 case UTIL:
131 #ifdef DEBUG
132 printf(" case UTIL:\n");
133 #endif
134 for (num = 0; op && op->sqr->type == UTIL; op = op->next)
135 num++;
136 play[pl].num_util = num;
137 #ifdef DEBUG
138 printf("play[pl].num_util = num [%d];\n", num);
139 #endif
140 break;
141 case RR:
142 #ifdef DEBUG
143 printf(" case RR:\n");
144 #endif
145 for (num = 0; op && op->sqr->type == RR; op = op->next) {
146 #ifdef DEBUG
147 printf("iter: %d\n", num);
148 printf("op = %d, op->sqr = %d, op->sqr->type = %d\n", op, op->sqr, op->sqr->type);
149 #endif
150 num++;
151 }
152 play[pl].num_rr = num;
153 #ifdef DEBUG
154 printf("play[pl].num_rr = num [%d];\n", num);
155 #endif
156 break;
157 case PRPTY:
158 #ifdef DEBUG
159 printf(" case PRPTY:\n");
160 #endif
161 orig = op->sqr->desc->mon_desc;
162 orig_op = op;
163 num = 0;
164 while (op && op->sqr->desc->mon_desc == orig) {
165 #ifdef DEBUG
166 printf("iter: %d\n", num);
167 #endif
168 num++;
169 #ifdef DEBUG
170 printf("op = op->next ");
171 #endif
172 op = op->next;
173 #ifdef DEBUG
174 printf("[%d];\n", op);
175 #endif
176 }
177 #ifdef DEBUG
178 printf("num = %d\n");
179 #endif
180 if (orig == 0) {
181 printf("panic: bad monopoly descriptor: orig = %d\n", orig);
182 printf("player # %d\n", pl+1);
183 printhold(pl);
184 printf("orig_op = %d\n", orig_op);
185 printf("orig_op->sqr->type = %d (PRPTY)\n", op->sqr->type);
186 printf("orig_op->next = %d\n", op->next);
187 printf("orig_op->sqr->desc = %d\n", op->sqr->desc);
188 printf("op = %d\n", op);
189 printf("op->sqr->type = %d (PRPTY)\n", op->sqr->type);
190 printf("op->next = %d\n", op->next);
191 printf("op->sqr->desc = %d\n", op->sqr->desc);
192 printf("num = %d\n", num);
193 }
194 #ifdef DEBUG
195 printf("orig->num_in = %d\n", orig->num_in);
196 #endif
197 if (num == orig->num_in)
198 is_monop(orig, pl);
199 else
200 isnot_monop(orig);
201 break;
202 }
203 }
204 }
205 /*
206 * This routine sets things up as if it is a new monopoly
207 */
208 is_monop(mp, pl)
209 reg MON *mp;
210 int pl; {
211
212 reg char *sp;
213 reg int i;
214
215 mp->owner = pl;
216 mp->num_own = mp->num_in;
217 for (i = 0; i < mp->num_in; i++)
218 mp->sq[i]->desc->monop = TRUE;
219 mp->name = mp->mon_n;
220 }
221 /*
222 * This routine sets things up as if it is no longer a monopoly
223 */
224 isnot_monop(mp)
225 reg MON *mp; {
226
227 reg char *sp;
228 reg int i;
229
230 mp->owner = -1;
231 for (i = 0; i < mp->num_in; i++)
232 mp->sq[i]->desc->monop = FALSE;
233 mp->name = mp->not_m;
234 }
235 /*
236 * This routine gives a list of the current player's routine
237 */
238 list() {
239
240 printhold(player);
241 }
242 /*
243 * This routine gives a list of a given players holdings
244 */
245 list_all() {
246
247 reg int pl;
248
249 while ((pl=getinp("Whose holdings do you want to see? ", name_list)) < num_play)
250 printhold(pl);
251 }
252 /*
253 * This routine gives the players a chance before it exits.
254 */
255 void
256 quit() {
257
258 putchar('\n');
259 if (getyn("Do you all really want to quit? ", yn) == 0)
260 exit(0);
261 signal(SIGINT, quit);
262 }
263 /*
264 * This routine copies one structure to another
265 */
266 cpy_st(s1, s2, size)
267 reg int *s1, *s2, size; {
268
269 size /= 2;
270 while (size--)
271 *s1++ = *s2++;
272 }
273