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