1 /* $OpenBSD: misc.c,v 1.11 2010/05/10 08:53:12 nicm 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 "monop.ext" 34 #include <ctype.h> 35 #include <signal.h> 36 37 /* 38 * This routine executes a truncated set of commands until a 39 * "yes or "no" answer is gotten. 40 */ 41 int 42 getyn(prompt) 43 char *prompt; 44 { 45 int com; 46 47 for (;;) 48 if ((com=getinp(prompt, ynlist)) < 2) 49 return com; 50 else 51 (*func[com-2])(); 52 } 53 /* 54 * This routine tells the player if he's out of money. 55 */ 56 void 57 notify() 58 { 59 if (cur_p->money < 0) 60 printf("That leaves you $%d in debt\n", -cur_p->money); 61 else if (cur_p->money == 0) 62 printf("that leaves you broke\n"); 63 else if (fixing && !told_em && cur_p->money > 0) { 64 printf("-- You are now Solvent ---\n"); 65 told_em = TRUE; 66 } 67 } 68 /* 69 * This routine switches to the next player 70 */ 71 void 72 next_play() 73 { 74 player = (player + 1) % num_play; 75 cur_p = &play[player]; 76 num_doub = 0; 77 } 78 /* 79 * This routine gets an integer from the keyboard after the 80 * given prompt. 81 */ 82 int 83 get_int(prompt) 84 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(*sp); sp++) 110 ; 111 for (; isdigit(*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 132 set_ownlist(pl) 133 int pl; 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 [%p] = 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 = %p, op->sqr = %p, 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("[%p];\n", op); 194 #endif 195 } 196 #ifdef DEBUG 197 printf("num = %d\n", num); 198 #endif 199 if (orig == NULL) { 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 if (orig_op) { 205 printf("orig_op->sqr->type = %d (PRPTY)\n", 206 orig_op->sqr->type); 207 printf("orig_op->next = %p\n", 208 orig_op->next); 209 printf("orig_op->sqr->desc = %p\n", 210 orig_op->sqr->desc); 211 } 212 printf("op = %p\n", op); 213 if (op) { 214 printf("op->sqr->type = %d (PRPTY)\n", 215 op->sqr->type); 216 printf("op->next = %p\n", op->next); 217 printf("op->sqr->desc = %p\n", 218 op->sqr->desc); 219 } 220 printf("num = %d\n", num); 221 exit(1); 222 } 223 #ifdef DEBUG 224 printf("orig->num_in = %d\n", orig->num_in); 225 #endif 226 if (num == orig->num_in) 227 is_monop(orig, pl); 228 else 229 isnot_monop(orig); 230 break; 231 } 232 } 233 } 234 /* 235 * This routine sets things up as if it is a new monopoly 236 */ 237 void 238 is_monop(mp, pl) 239 MON *mp; 240 int pl; 241 { 242 int i; 243 244 mp->owner = pl; 245 mp->num_own = mp->num_in; 246 for (i = 0; i < mp->num_in; i++) 247 mp->sq[i]->desc->monop = TRUE; 248 mp->name = mp->mon_n; 249 } 250 /* 251 * This routine sets things up as if it is no longer a monopoly 252 */ 253 void 254 isnot_monop(mp) 255 MON *mp; 256 { 257 int i; 258 259 mp->owner = -1; 260 for (i = 0; i < mp->num_in; i++) 261 mp->sq[i]->desc->monop = FALSE; 262 mp->name = mp->not_m; 263 } 264 /* 265 * This routine gives a list of the current player's routine 266 */ 267 void 268 list() 269 { 270 printhold(player); 271 } 272 /* 273 * This routine gives a list of a given players holdings 274 */ 275 void 276 list_all() 277 { 278 int pl; 279 280 while ((pl=getinp("Whose holdings do you want to see? ", name_list)) < num_play) 281 printhold(pl); 282 } 283 /* 284 * This routine gives the players a chance before it exits. 285 */ 286 void 287 quit() 288 { 289 putchar('\n'); 290 if (getyn("Do you all really want to quit? ") == 0) 291 exit(0); 292 } 293