xref: /original-bsd/games/monop/monop.c (revision a51a1dd1)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)monop.c	8.1 (Berkeley) 05/31/93";
16 #endif /* not lint */
17 
18 # include	"monop.def"
19 
20 /*
21  *	This program implements a monopoly game
22  */
main(ac,av)23 main(ac, av)
24 reg int		ac;
25 reg char	*av[]; {
26 
27 
28 	srand(getpid());
29 	if (ac > 1) {
30 		if (!rest_f(av[1]))
31 			restore();
32 	}
33 	else {
34 		getplayers();
35 		init_players();
36 		init_monops();
37 	}
38 	num_luck = sizeof lucky_mes / sizeof (char *);
39 	init_decks();
40 	signal(2, quit);
41 	for (;;) {
42 		printf("\n%s (%d) (cash $%d) on %s\n", cur_p->name, player + 1,
43 			cur_p->money, board[cur_p->loc].name);
44 		printturn();
45 		force_morg();
46 		execute(getinp("-- Command: ", comlist));
47 	}
48 }
49 /*
50  *	This routine gets the names of the players
51  */
getplayers()52 getplayers() {
53 
54 	reg char	*sp;
55 	reg int		i, j;
56 	char		buf[257];
57 
58 blew_it:
59 	for (;;) {
60 		if ((num_play=get_int("How many players? ")) <= 0 ||
61 		    num_play > MAX_PL)
62 			printf("Sorry. Number must range from 1 to 9\n");
63 		else
64 			break;
65 	}
66 	cur_p = play = (PLAY *) calloc(num_play, sizeof (PLAY));
67 	for (i = 0; i < num_play; i++) {
68 over:
69 		printf("Player %d's name: ", i + 1);
70 		for (sp = buf; (*sp=getchar()) != '\n'; sp++)
71 			continue;
72 		if (sp == buf)
73 			goto over;
74 		*sp++ = '\0';
75 		strcpy(name_list[i]=play[i].name=(char *)calloc(1,sp-buf),buf);
76 		play[i].money = 1500;
77 	}
78 	name_list[i++] = "done";
79 	name_list[i] = 0;
80 	for (i = 0; i < num_play; i++)
81 		for (j = i + 1; j < num_play; j++)
82 			if (strcasecmp(name_list[i], name_list[j]) == 0) {
83 				if (i != num_play - 1)
84 					printf("Hey!!! Some of those are IDENTICAL!!  Let's try that again....\n");
85 				else
86 					printf("\"done\" is a reserved word.  Please try again\n");
87 				for (i = 0; i < num_play; i++)
88 					cfree(play[i].name);
89 				cfree(play);
90 				goto blew_it;
91 			}
92 }
93 /*
94  *	This routine figures out who goes first
95  */
init_players()96 init_players() {
97 
98 	reg int	i, rl, cur_max;
99 	bool	over;
100 	int	max_pl;
101 
102 again:
103 	putchar('\n');
104 	for (cur_max = i = 0; i < num_play; i++) {
105 		printf("%s (%d) rolls %d\n", play[i].name, i+1, rl=roll(2, 6));
106 		if (rl > cur_max) {
107 			over = FALSE;
108 			cur_max = rl;
109 			max_pl = i;
110 		}
111 		else if (rl == cur_max)
112 			over++;
113 	}
114 	if (over) {
115 		printf("%d people rolled the same thing, so we'll try again\n",
116 		    over + 1);
117 		goto again;
118 	}
119 	player = max_pl;
120 	cur_p = &play[max_pl];
121 	printf("%s (%d) goes first\n", cur_p->name, max_pl + 1);
122 }
123 /*
124  *	This routine initalizes the monopoly structures.
125  */
init_monops()126 init_monops() {
127 
128 	reg MON	*mp;
129 	reg int	i;
130 
131 	for (mp = mon; mp < &mon[N_MON]; mp++) {
132 		mp->name = mp->not_m;
133 		for (i = 0; i < mp->num_in; i++)
134 			mp->sq[i] = &board[mp->sqnums[i]];
135 	}
136 }
137