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