1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 05/31/93"; 10 #endif /* not lint */ 11 12 # include "monop.ext" 13 # include <ctype.h> 14 # include <signal.h> 15 16 /* 17 * This routine executes a truncated set of commands until a 18 * "yes or "no" answer is gotten. 19 */ 20 getyn(prompt) 21 reg char *prompt; { 22 23 reg int com; 24 25 for (;;) 26 if ((com=getinp(prompt, yn)) < 2) 27 return com; 28 else 29 (*func[com-2])(); 30 } 31 /* 32 * This routine tells the player if he's out of money. 33 */ 34 notify() { 35 36 if (cur_p->money < 0) 37 printf("That leaves you $%d in debt\n", -cur_p->money); 38 else if (cur_p->money == 0) 39 printf("that leaves you broke\n"); 40 else if (fixing && !told_em && cur_p->money > 0) { 41 printf("-- You are now Solvent ---\n"); 42 told_em = TRUE; 43 } 44 } 45 /* 46 * This routine switches to the next player 47 */ 48 next_play() { 49 50 player = ++player % num_play; 51 cur_p = &play[player]; 52 num_doub = 0; 53 } 54 /* 55 * This routine gets an integer from the keyboard after the 56 * given prompt. 57 */ 58 get_int(prompt) 59 reg char *prompt; { 60 61 reg int num; 62 reg char *sp; 63 char buf[257]; 64 65 for (;;) { 66 inter: 67 printf(prompt); 68 num = 0; 69 for (sp = buf; (*sp=getchar()) != '\n'; sp++) 70 if (*sp == -1) /* check for interrupted system call */ 71 goto inter; 72 if (sp == buf) 73 continue; 74 for (sp = buf; isspace(*sp); sp++) 75 continue; 76 for (; isdigit(*sp); sp++) 77 num = num * 10 + *sp - '0'; 78 if (*sp == '\n') 79 return num; 80 else 81 printf("I can't understand that\n"); 82 } 83 } 84 /* 85 * This routine sets the monopoly flag from the list given. 86 */ 87 set_ownlist(pl) 88 int pl; { 89 90 reg int num; /* general counter */ 91 reg MON *orig; /* remember starting monop ptr */ 92 reg OWN *op; /* current owned prop */ 93 OWN *orig_op; /* origianl prop before loop */ 94 95 op = play[pl].own_list; 96 #ifdef DEBUG 97 printf("op [%d] = play[pl [%d] ].own_list;\n", op, pl); 98 #endif 99 while (op) { 100 #ifdef DEBUG 101 printf("op->sqr->type = %d\n", op->sqr->type); 102 #endif 103 switch (op->sqr->type) { 104 case UTIL: 105 #ifdef DEBUG 106 printf(" case UTIL:\n"); 107 #endif 108 for (num = 0; op && op->sqr->type == UTIL; op = op->next) 109 num++; 110 play[pl].num_util = num; 111 #ifdef DEBUG 112 printf("play[pl].num_util = num [%d];\n", num); 113 #endif 114 break; 115 case RR: 116 #ifdef DEBUG 117 printf(" case RR:\n"); 118 #endif 119 for (num = 0; op && op->sqr->type == RR; op = op->next) { 120 #ifdef DEBUG 121 printf("iter: %d\n", num); 122 printf("op = %d, op->sqr = %d, op->sqr->type = %d\n", op, op->sqr, op->sqr->type); 123 #endif 124 num++; 125 } 126 play[pl].num_rr = num; 127 #ifdef DEBUG 128 printf("play[pl].num_rr = num [%d];\n", num); 129 #endif 130 break; 131 case PRPTY: 132 #ifdef DEBUG 133 printf(" case PRPTY:\n"); 134 #endif 135 orig = op->sqr->desc->mon_desc; 136 orig_op = op; 137 num = 0; 138 while (op && op->sqr->desc->mon_desc == orig) { 139 #ifdef DEBUG 140 printf("iter: %d\n", num); 141 #endif 142 num++; 143 #ifdef DEBUG 144 printf("op = op->next "); 145 #endif 146 op = op->next; 147 #ifdef DEBUG 148 printf("[%d];\n", op); 149 #endif 150 } 151 #ifdef DEBUG 152 printf("num = %d\n"); 153 #endif 154 if (orig == 0) { 155 printf("panic: bad monopoly descriptor: orig = %d\n", orig); 156 printf("player # %d\n", pl+1); 157 printhold(pl); 158 printf("orig_op = %d\n", orig_op); 159 printf("orig_op->sqr->type = %d (PRPTY)\n", op->sqr->type); 160 printf("orig_op->next = %d\n", op->next); 161 printf("orig_op->sqr->desc = %d\n", op->sqr->desc); 162 printf("op = %d\n", op); 163 printf("op->sqr->type = %d (PRPTY)\n", op->sqr->type); 164 printf("op->next = %d\n", op->next); 165 printf("op->sqr->desc = %d\n", op->sqr->desc); 166 printf("num = %d\n", num); 167 } 168 #ifdef DEBUG 169 printf("orig->num_in = %d\n", orig->num_in); 170 #endif 171 if (num == orig->num_in) 172 is_monop(orig, pl); 173 else 174 isnot_monop(orig); 175 break; 176 } 177 } 178 } 179 /* 180 * This routine sets things up as if it is a new monopoly 181 */ 182 is_monop(mp, pl) 183 reg MON *mp; 184 int pl; { 185 186 reg char *sp; 187 reg int i; 188 189 mp->owner = pl; 190 mp->num_own = mp->num_in; 191 for (i = 0; i < mp->num_in; i++) 192 mp->sq[i]->desc->monop = TRUE; 193 mp->name = mp->mon_n; 194 } 195 /* 196 * This routine sets things up as if it is no longer a monopoly 197 */ 198 isnot_monop(mp) 199 reg MON *mp; { 200 201 reg char *sp; 202 reg int i; 203 204 mp->owner = -1; 205 for (i = 0; i < mp->num_in; i++) 206 mp->sq[i]->desc->monop = FALSE; 207 mp->name = mp->not_m; 208 } 209 /* 210 * This routine gives a list of the current player's routine 211 */ 212 list() { 213 214 printhold(player); 215 } 216 /* 217 * This routine gives a list of a given players holdings 218 */ 219 list_all() { 220 221 reg int pl; 222 223 while ((pl=getinp("Whose holdings do you want to see? ", name_list)) < num_play) 224 printhold(pl); 225 } 226 /* 227 * This routine gives the players a chance before it exits. 228 */ 229 void 230 quit() { 231 232 putchar('\n'); 233 if (getyn("Do you all really want to quit? ", yn) == 0) 234 exit(0); 235 signal(SIGINT, quit); 236 } 237 /* 238 * This routine copies one structure to another 239 */ 240 cpy_st(s1, s2, size) 241 reg int *s1, *s2, size; { 242 243 size /= 2; 244 while (size--) 245 *s1++ = *s2++; 246 } 247