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