Home | History | Annotate | Line # | Download | only in monop
monop.c revision 1.2
      1 /*
      2  * Copyright (c) 1980 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)monop.c	5.7 (Berkeley) 6/1/90";*/
     42 static char rcsid[] = "$Id: monop.c,v 1.2 1993/08/01 18:53:35 mycroft Exp $";
     43 #endif /* not lint */
     44 
     45 # include	"monop.def"
     46 
     47 /*
     48  *	This program implements a monopoly game
     49  */
     50 main(ac, av)
     51 reg int		ac;
     52 reg char	*av[]; {
     53 
     54 
     55 	srand(getpid());
     56 	if (ac > 1) {
     57 		if (!rest_f(av[1]))
     58 			restore();
     59 	}
     60 	else {
     61 		getplayers();
     62 		init_players();
     63 		init_monops();
     64 	}
     65 	num_luck = sizeof lucky_mes / sizeof (char *);
     66 	init_decks();
     67 	signal(2, quit);
     68 	for (;;) {
     69 		printf("\n%s (%d) (cash $%d) on %s\n", cur_p->name, player + 1,
     70 			cur_p->money, board[cur_p->loc].name);
     71 		printturn();
     72 		force_morg();
     73 		execute(getinp("-- Command: ", comlist));
     74 	}
     75 }
     76 /*
     77  *	This routine gets the names of the players
     78  */
     79 getplayers() {
     80 
     81 	reg char	*sp;
     82 	reg int		i, j;
     83 	char		buf[257];
     84 
     85 blew_it:
     86 	for (;;) {
     87 		if ((num_play=get_int("How many players? ")) <= 0 ||
     88 		    num_play > MAX_PL)
     89 			printf("Sorry. Number must range from 1 to 9\n");
     90 		else
     91 			break;
     92 	}
     93 	cur_p = play = (PLAY *) calloc(num_play, sizeof (PLAY));
     94 	for (i = 0; i < num_play; i++) {
     95 over:
     96 		printf("Player %d's name: ", i + 1);
     97 		for (sp = buf; (*sp=getchar()) != '\n'; sp++)
     98 			continue;
     99 		if (sp == buf)
    100 			goto over;
    101 		*sp++ = '\0';
    102 		strcpy(name_list[i]=play[i].name=(char *)calloc(1,sp-buf),buf);
    103 		play[i].money = 1500;
    104 	}
    105 	name_list[i++] = "done";
    106 	name_list[i] = 0;
    107 	for (i = 0; i < num_play; i++)
    108 		for (j = i + 1; j < num_play; j++)
    109 			if (strcasecmp(name_list[i], name_list[j]) == 0) {
    110 				if (i != num_play - 1)
    111 					printf("Hey!!! Some of those are IDENTICAL!!  Let's try that again....\n");
    112 				else
    113 					printf("\"done\" is a reserved word.  Please try again\n");
    114 				for (i = 0; i < num_play; i++)
    115 					cfree(play[i].name);
    116 				cfree(play);
    117 				goto blew_it;
    118 			}
    119 }
    120 /*
    121  *	This routine figures out who goes first
    122  */
    123 init_players() {
    124 
    125 	reg int	i, rl, cur_max;
    126 	bool	over;
    127 	int	max_pl;
    128 
    129 again:
    130 	putchar('\n');
    131 	for (cur_max = i = 0; i < num_play; i++) {
    132 		printf("%s (%d) rolls %d\n", play[i].name, i+1, rl=roll(2, 6));
    133 		if (rl > cur_max) {
    134 			over = FALSE;
    135 			cur_max = rl;
    136 			max_pl = i;
    137 		}
    138 		else if (rl == cur_max)
    139 			over++;
    140 	}
    141 	if (over) {
    142 		printf("%d people rolled the same thing, so we'll try again\n",
    143 		    over + 1);
    144 		goto again;
    145 	}
    146 	player = max_pl;
    147 	cur_p = &play[max_pl];
    148 	printf("%s (%d) goes first\n", cur_p->name, max_pl + 1);
    149 }
    150 /*
    151  *	This routine initalizes the monopoly structures.
    152  */
    153 init_monops() {
    154 
    155 	reg MON	*mp;
    156 	reg int	i;
    157 
    158 	for (mp = mon; mp < &mon[N_MON]; mp++) {
    159 		mp->name = mp->not_m;
    160 		for (i = 0; i < mp->num_in; i++)
    161 			mp->sq[i] = &board[mp->sqnums[i]];
    162 	}
    163 }
    164