Home | History | Annotate | Line # | Download | only in monop
misc.c revision 1.21
      1 /*	$NetBSD: misc.c,v 1.21 2009/08/12 08:10:49 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.21 2009/08/12 08:10:49 dholland Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <ctype.h>
     42 #include <limits.h>
     43 #include <signal.h>
     44 #include <errno.h>
     45 
     46 #include "monop.h"
     47 
     48 static void is_monop(MON *, int);
     49 
     50 /*
     51  *	This routine executes a truncated set of commands until a
     52  * "yes or "no" answer is gotten.
     53  */
     54 int
     55 getyn(prompt)
     56 	const char *prompt;
     57 {
     58 	int com;
     59 
     60 	for (;;)
     61 		if ((com=getinp(prompt, yncoms)) < 2)
     62 			return com;
     63 		else
     64 			(*func[com-2])();
     65 }
     66 
     67 /*
     68  *	This routine tells the player if he's out of money.
     69  */
     70 void
     71 notify()
     72 {
     73 	if (cur_p->money < 0)
     74 		printf("That leaves you $%d in debt\n", -cur_p->money);
     75 	else if (cur_p->money == 0)
     76 		printf("that leaves you broke\n");
     77 	else if (fixing && !told_em && cur_p->money > 0) {
     78 		printf("-- You are now Solvent ---\n");
     79 		told_em = TRUE;
     80 	}
     81 }
     82 
     83 /*
     84  *	This routine switches to the next player
     85  */
     86 void
     87 next_play()
     88 {
     89 	player = (player + 1) % num_play;
     90 	cur_p = &play[player];
     91 	num_doub = 0;
     92 }
     93 
     94 /*
     95  *	This routine gets an integer from the keyboard after the
     96  * given prompt.
     97  */
     98 int
     99 get_int(prompt)
    100 	const char *prompt;
    101 {
    102 	long num;
    103 	char *sp;
    104 	char buf[257];
    105 
    106 	for (;;) {
    107 		printf("%s", prompt);
    108 		fgets(buf, sizeof(buf), stdin);
    109 		if (feof(stdin))
    110 			return 0;
    111 		sp = strchr(buf, '\n');
    112 		if (sp)
    113 			*sp = '\0';
    114 		errno = 0;
    115 		num = strtol(buf, &sp, 10);
    116 		if (errno || strlen(sp) > 0 || num < 0 || num >= INT_MAX) {
    117 			printf("I can't understand that\n");
    118 			continue;
    119 		}
    120 		return num;
    121 	}
    122 }
    123 
    124 /*
    125  *	This routine sets the monopoly flag from the list given.
    126  */
    127 void
    128 set_ownlist(pl)
    129 	int pl;
    130 {
    131 	int num;		/* general counter		*/
    132 	MON *orig;		/* remember starting monop ptr	*/
    133 	OWN *op;		/* current owned prop		*/
    134 	OWN *orig_op;		/* original prop before loop	*/
    135 
    136 	op = play[pl].own_list;
    137 #ifdef DEBUG
    138 	printf("op [%p] = play[pl [%d] ].own_list;\n", op, pl);
    139 #endif
    140 	while (op) {
    141 #ifdef DEBUG
    142 		printf("op->sqr->type = %d\n", op->sqr->type);
    143 #endif
    144 		switch (op->sqr->type) {
    145 		  case UTIL:
    146 #ifdef DEBUG
    147 			printf("  case UTIL:\n");
    148 #endif
    149 			for (num = 0; op && op->sqr->type == UTIL;
    150 			    op = op->next)
    151 				num++;
    152 			play[pl].num_util = num;
    153 #ifdef DEBUG
    154 			printf("play[pl].num_util = num [%d];\n", num);
    155 #endif
    156 			break;
    157 		  case RR:
    158 #ifdef DEBUG
    159 			printf("  case RR:\n");
    160 #endif
    161 			for (num = 0; op && op->sqr->type == RR;
    162 			    op = op->next) {
    163 #ifdef DEBUG
    164 				printf("iter: %d\n", num);
    165 				printf("op = %p, op->sqr = %p, "
    166 				    "op->sqr->type = %d\n", op, op->sqr,
    167 				    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: "
    201 				    "orig = %p\n", orig);
    202 				printf("player # %d\n", pl+1);
    203 				printhold(pl);
    204 				printf("orig_op = %p\n", orig_op);
    205 				if (orig_op) {
    206 					printf("orig_op->sqr->type = %d (PRPTY)\n",
    207 					    orig_op->sqr->type);
    208 					printf("orig_op->next = %p\n",
    209 					    orig_op->next);
    210 					printf("orig_op->sqr->desc = %p\n",
    211 					    orig_op->sqr->desc);
    212 				}
    213 				printf("op = %p\n", op);
    214 				if (op) {
    215 					printf("op->sqr->type = %d (PRPTY)\n",
    216 					    op->sqr->type);
    217 					printf("op->next = %p\n", op->next);
    218 					printf("op->sqr->desc = %p\n",
    219 					    op->sqr->desc);
    220 				}
    221 				printf("num = %d\n", num);
    222 				exit(1);
    223 			}
    224 #ifdef DEBUG
    225 			printf("orig->num_in = %d\n", orig->num_in);
    226 #endif
    227 			if (num == orig->num_in)
    228 				is_monop(orig, pl);
    229 			else
    230 				is_not_monop(orig);
    231 			break;
    232 		}
    233 	}
    234 }
    235 
    236 /*
    237  *	This routine sets things up as if it is a new monopoly
    238  */
    239 static void
    240 is_monop(mp, pl)
    241 	MON *mp;
    242 	int pl;
    243 {
    244 	int i;
    245 
    246 	mp->owner = pl;
    247 	mp->num_own = mp->num_in;
    248 	for (i = 0; i < mp->num_in; i++)
    249 		mp->sq[i]->desc->monop = TRUE;
    250 	mp->name = mp->mon_n;
    251 }
    252 
    253 /*
    254  *	This routine sets things up as if it is no longer a monopoly
    255  */
    256 void
    257 is_not_monop(mp)
    258 	MON *mp;
    259 {
    260 	int i;
    261 
    262 	mp->owner = -1;
    263 	for (i = 0; i < mp->num_in; i++)
    264 		mp->sq[i]->desc->monop = FALSE;
    265 	mp->name = mp->not_m;
    266 }
    267 
    268 /*
    269  *	This routine gives a list of the current player's routine
    270  */
    271 void
    272 list()
    273 {
    274 	printhold(player);
    275 }
    276 
    277 /*
    278  *	This routine gives a list of a given players holdings
    279  */
    280 void
    281 list_all()
    282 {
    283 	int pl;
    284 
    285 	while ((pl = getinp("Whose holdings do you want to see? ", name_list))
    286 	    < num_play)
    287 		printhold(pl);
    288 }
    289 
    290 /*
    291  *	This routine gives the players a chance before it exits.
    292  */
    293 void
    294 quit()
    295 {
    296 	putchar('\n');
    297 	if (getyn("Do you all really want to quit? ") == 0)
    298 		exit(0);
    299 }
    300