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