Home | History | Annotate | Line # | Download | only in monop
misc.c revision 1.22
      1 /*	$NetBSD: misc.c,v 1.22 2011/12/01 07:05:52 ahoka 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.22 2011/12/01 07:05:52 ahoka 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 stdin is closed we cant really play anymore */
    110 		if (feof(stdin))
    111 			quit();
    112 		sp = strchr(buf, '\n');
    113 		if (sp)
    114 			*sp = '\0';
    115 		errno = 0;
    116 		num = strtol(buf, &sp, 10);
    117 		if (errno || strlen(sp) > 0 || num < 0 || num >= INT_MAX) {
    118 			printf("I can't understand that\n");
    119 			continue;
    120 		}
    121 		return num;
    122 	}
    123 }
    124 
    125 /*
    126  *	This routine sets the monopoly flag from the list given.
    127  */
    128 void
    129 set_ownlist(pl)
    130 	int pl;
    131 {
    132 	int num;		/* general counter		*/
    133 	MON *orig;		/* remember starting monop ptr	*/
    134 	OWN *op;		/* current owned prop		*/
    135 	OWN *orig_op;		/* original prop before loop	*/
    136 
    137 	op = play[pl].own_list;
    138 #ifdef DEBUG
    139 	printf("op [%p] = play[pl [%d] ].own_list;\n", op, pl);
    140 #endif
    141 	while (op) {
    142 #ifdef DEBUG
    143 		printf("op->sqr->type = %d\n", op->sqr->type);
    144 #endif
    145 		switch (op->sqr->type) {
    146 		  case UTIL:
    147 #ifdef DEBUG
    148 			printf("  case UTIL:\n");
    149 #endif
    150 			for (num = 0; op && op->sqr->type == UTIL;
    151 			    op = op->next)
    152 				num++;
    153 			play[pl].num_util = num;
    154 #ifdef DEBUG
    155 			printf("play[pl].num_util = num [%d];\n", num);
    156 #endif
    157 			break;
    158 		  case RR:
    159 #ifdef DEBUG
    160 			printf("  case RR:\n");
    161 #endif
    162 			for (num = 0; op && op->sqr->type == RR;
    163 			    op = op->next) {
    164 #ifdef DEBUG
    165 				printf("iter: %d\n", num);
    166 				printf("op = %p, op->sqr = %p, "
    167 				    "op->sqr->type = %d\n", op, op->sqr,
    168 				    op->sqr->type);
    169 #endif
    170 				num++;
    171 			}
    172 			play[pl].num_rr = num;
    173 #ifdef DEBUG
    174 			printf("play[pl].num_rr = num [%d];\n", num);
    175 #endif
    176 			break;
    177 		  case PRPTY:
    178 #ifdef DEBUG
    179 			printf("  case PRPTY:\n");
    180 #endif
    181 			orig = op->sqr->desc->mon_desc;
    182 			orig_op = op;
    183 			num = 0;
    184 			while (op && op->sqr->desc->mon_desc == orig) {
    185 #ifdef DEBUG
    186 				printf("iter: %d\n", num);
    187 #endif
    188 				num++;
    189 #ifdef DEBUG
    190 				printf("op = op->next ");
    191 #endif
    192 				op = op->next;
    193 #ifdef DEBUG
    194 				printf("[%p];\n", op);
    195 #endif
    196 			}
    197 #ifdef DEBUG
    198 			printf("num = %d\n", num);
    199 #endif
    200 			if (orig == NULL) {
    201 				printf("panic:  bad monopoly descriptor: "
    202 				    "orig = %p\n", orig);
    203 				printf("player # %d\n", pl+1);
    204 				printhold(pl);
    205 				printf("orig_op = %p\n", orig_op);
    206 				if (orig_op) {
    207 					printf("orig_op->sqr->type = %d (PRPTY)\n",
    208 					    orig_op->sqr->type);
    209 					printf("orig_op->next = %p\n",
    210 					    orig_op->next);
    211 					printf("orig_op->sqr->desc = %p\n",
    212 					    orig_op->sqr->desc);
    213 				}
    214 				printf("op = %p\n", op);
    215 				if (op) {
    216 					printf("op->sqr->type = %d (PRPTY)\n",
    217 					    op->sqr->type);
    218 					printf("op->next = %p\n", op->next);
    219 					printf("op->sqr->desc = %p\n",
    220 					    op->sqr->desc);
    221 				}
    222 				printf("num = %d\n", num);
    223 				exit(1);
    224 			}
    225 #ifdef DEBUG
    226 			printf("orig->num_in = %d\n", orig->num_in);
    227 #endif
    228 			if (num == orig->num_in)
    229 				is_monop(orig, pl);
    230 			else
    231 				is_not_monop(orig);
    232 			break;
    233 		}
    234 	}
    235 }
    236 
    237 /*
    238  *	This routine sets things up as if it is a new monopoly
    239  */
    240 static void
    241 is_monop(mp, pl)
    242 	MON *mp;
    243 	int pl;
    244 {
    245 	int i;
    246 
    247 	mp->owner = pl;
    248 	mp->num_own = mp->num_in;
    249 	for (i = 0; i < mp->num_in; i++)
    250 		mp->sq[i]->desc->monop = TRUE;
    251 	mp->name = mp->mon_n;
    252 }
    253 
    254 /*
    255  *	This routine sets things up as if it is no longer a monopoly
    256  */
    257 void
    258 is_not_monop(mp)
    259 	MON *mp;
    260 {
    261 	int i;
    262 
    263 	mp->owner = -1;
    264 	for (i = 0; i < mp->num_in; i++)
    265 		mp->sq[i]->desc->monop = FALSE;
    266 	mp->name = mp->not_m;
    267 }
    268 
    269 /*
    270  *	This routine gives a list of the current player's routine
    271  */
    272 void
    273 list()
    274 {
    275 	printhold(player);
    276 }
    277 
    278 /*
    279  *	This routine gives a list of a given players holdings
    280  */
    281 void
    282 list_all()
    283 {
    284 	int pl;
    285 
    286 	while ((pl = getinp("Whose holdings do you want to see? ", name_list))
    287 	    < num_play)
    288 		printhold(pl);
    289 }
    290 
    291 /*
    292  *	This routine gives the players a chance before it exits.
    293  */
    294 void
    295 quit()
    296 {
    297 	putchar('\n');
    298 
    299 	/* We dont even have a chance to input y/n if stdin is closed */
    300 	if (feof(stdin))
    301 		exit(0);
    302 
    303 	if (getyn("Do you all really want to quit? ") == 0)
    304 		exit(0);
    305 }
    306