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