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