xref: /csrg-svn/games/monop/monop.c (revision 60815)
132258Sbostic /*
2*60815Sbostic  * Copyright (c) 1980, 1993
3*60815Sbostic  *	The Regents of the University of California.  All rights reserved.
433202Sbostic  *
542583Sbostic  * %sccs.include.redist.c%
632258Sbostic  */
732258Sbostic 
832258Sbostic #ifndef lint
9*60815Sbostic static char copyright[] =
10*60815Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*60815Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233202Sbostic #endif /* not lint */
1332258Sbostic 
1433202Sbostic #ifndef lint
15*60815Sbostic static char sccsid[] = "@(#)monop.c	8.1 (Berkeley) 05/31/93";
1633202Sbostic #endif /* not lint */
1733202Sbostic 
1832258Sbostic # include	"monop.def"
1932258Sbostic 
2032258Sbostic /*
2132258Sbostic  *	This program implements a monopoly game
2232258Sbostic  */
main(ac,av)2332258Sbostic main(ac, av)
2432258Sbostic reg int		ac;
2532258Sbostic reg char	*av[]; {
2632258Sbostic 
2732258Sbostic 
2832258Sbostic 	srand(getpid());
2932258Sbostic 	if (ac > 1) {
3032258Sbostic 		if (!rest_f(av[1]))
3132258Sbostic 			restore();
3232258Sbostic 	}
3332258Sbostic 	else {
3432258Sbostic 		getplayers();
3532258Sbostic 		init_players();
3632258Sbostic 		init_monops();
3732258Sbostic 	}
3832258Sbostic 	num_luck = sizeof lucky_mes / sizeof (char *);
3932258Sbostic 	init_decks();
4032258Sbostic 	signal(2, quit);
4132258Sbostic 	for (;;) {
4232258Sbostic 		printf("\n%s (%d) (cash $%d) on %s\n", cur_p->name, player + 1,
4332258Sbostic 			cur_p->money, board[cur_p->loc].name);
4432258Sbostic 		printturn();
4532258Sbostic 		force_morg();
4632258Sbostic 		execute(getinp("-- Command: ", comlist));
4732258Sbostic 	}
4832258Sbostic }
4932258Sbostic /*
5032258Sbostic  *	This routine gets the names of the players
5132258Sbostic  */
getplayers()5232258Sbostic getplayers() {
5332258Sbostic 
5432258Sbostic 	reg char	*sp;
5532258Sbostic 	reg int		i, j;
5632258Sbostic 	char		buf[257];
5732258Sbostic 
5832258Sbostic blew_it:
5932258Sbostic 	for (;;) {
6032258Sbostic 		if ((num_play=get_int("How many players? ")) <= 0 ||
6132258Sbostic 		    num_play > MAX_PL)
6232258Sbostic 			printf("Sorry. Number must range from 1 to 9\n");
6332258Sbostic 		else
6432258Sbostic 			break;
6532258Sbostic 	}
6632258Sbostic 	cur_p = play = (PLAY *) calloc(num_play, sizeof (PLAY));
6732258Sbostic 	for (i = 0; i < num_play; i++) {
6832258Sbostic over:
6932258Sbostic 		printf("Player %d's name: ", i + 1);
7032258Sbostic 		for (sp = buf; (*sp=getchar()) != '\n'; sp++)
7132258Sbostic 			continue;
7232258Sbostic 		if (sp == buf)
7332258Sbostic 			goto over;
7432258Sbostic 		*sp++ = '\0';
7532258Sbostic 		strcpy(name_list[i]=play[i].name=(char *)calloc(1,sp-buf),buf);
7632258Sbostic 		play[i].money = 1500;
7732258Sbostic 	}
7832258Sbostic 	name_list[i++] = "done";
7932258Sbostic 	name_list[i] = 0;
8032258Sbostic 	for (i = 0; i < num_play; i++)
8132258Sbostic 		for (j = i + 1; j < num_play; j++)
8232262Sbostic 			if (strcasecmp(name_list[i], name_list[j]) == 0) {
8332258Sbostic 				if (i != num_play - 1)
8432258Sbostic 					printf("Hey!!! Some of those are IDENTICAL!!  Let's try that again....\n");
8532258Sbostic 				else
8632258Sbostic 					printf("\"done\" is a reserved word.  Please try again\n");
8732258Sbostic 				for (i = 0; i < num_play; i++)
8832258Sbostic 					cfree(play[i].name);
8932258Sbostic 				cfree(play);
9032258Sbostic 				goto blew_it;
9132258Sbostic 			}
9232258Sbostic }
9332258Sbostic /*
9432258Sbostic  *	This routine figures out who goes first
9532258Sbostic  */
init_players()9632258Sbostic init_players() {
9732258Sbostic 
9832258Sbostic 	reg int	i, rl, cur_max;
9932258Sbostic 	bool	over;
10032258Sbostic 	int	max_pl;
10132258Sbostic 
10232258Sbostic again:
10332258Sbostic 	putchar('\n');
10432258Sbostic 	for (cur_max = i = 0; i < num_play; i++) {
10532258Sbostic 		printf("%s (%d) rolls %d\n", play[i].name, i+1, rl=roll(2, 6));
10632258Sbostic 		if (rl > cur_max) {
10732258Sbostic 			over = FALSE;
10832258Sbostic 			cur_max = rl;
10932258Sbostic 			max_pl = i;
11032258Sbostic 		}
11132258Sbostic 		else if (rl == cur_max)
11232258Sbostic 			over++;
11332258Sbostic 	}
11432258Sbostic 	if (over) {
11532258Sbostic 		printf("%d people rolled the same thing, so we'll try again\n",
11632258Sbostic 		    over + 1);
11732258Sbostic 		goto again;
11832258Sbostic 	}
11932258Sbostic 	player = max_pl;
12032258Sbostic 	cur_p = &play[max_pl];
12132258Sbostic 	printf("%s (%d) goes first\n", cur_p->name, max_pl + 1);
12232258Sbostic }
12332258Sbostic /*
12432258Sbostic  *	This routine initalizes the monopoly structures.
12532258Sbostic  */
init_monops()12632258Sbostic init_monops() {
12732258Sbostic 
12832258Sbostic 	reg MON	*mp;
12932258Sbostic 	reg int	i;
13032258Sbostic 
13132258Sbostic 	for (mp = mon; mp < &mon[N_MON]; mp++) {
13232258Sbostic 		mp->name = mp->not_m;
13332258Sbostic 		for (i = 0; i < mp->num_in; i++)
13433217Sbostic 			mp->sq[i] = &board[mp->sqnums[i]];
13532258Sbostic 	}
13632258Sbostic }
137