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