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