monop.c revision 1.15.22.2 1 1.15.22.2 matt /* monop.c,v 1.15.22.1 2008/01/09 01:30:52 matt Exp */
2 1.3 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1980, 1993
5 1.3 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.14 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.4 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.4 christos __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
35 1.4 christos The Regents of the University of California. All rights reserved.\n");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.3 cgd #if 0
40 1.3 cgd static char sccsid[] = "@(#)monop.c 8.1 (Berkeley) 5/31/93";
41 1.3 cgd #else
42 1.15.22.2 matt __RCSID("monop.c,v 1.15.22.1 2008/01/09 01:30:52 matt Exp");
43 1.3 cgd #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.4 christos #include <stdio.h>
47 1.4 christos #include <signal.h>
48 1.4 christos #include <stdlib.h>
49 1.15.22.2 matt #include <time.h>
50 1.4 christos #include <unistd.h>
51 1.15.22.2 matt #include "deck.h"
52 1.15.22.2 matt #include "monop.h"
53 1.4 christos
54 1.15 jsm int main(int, char *[]);
55 1.15 jsm static void getplayers(void);
56 1.15 jsm static void init_players(void);
57 1.15 jsm static void init_monops(void);
58 1.15 jsm static void do_quit(int);
59 1.15.22.2 matt
60 1.15.22.2 matt
61 1.15.22.2 matt bool fixing, /* set if fixing up debt */
62 1.15.22.2 matt trading, /* set if in process of trading */
63 1.15.22.2 matt told_em, /* set if told user he's out of debt */
64 1.15.22.2 matt spec; /* set if moving by card to RR or UTIL */
65 1.15.22.2 matt
66 1.15.22.2 matt const char *name_list[MAX_PL+2], /* list of players' names */
67 1.15.22.2 matt *const comlist[] = { /* list of normal commands */
68 1.15.22.2 matt "quit", /* 0 */ "print", /* 1 */
69 1.15.22.2 matt "where", /* 2 */ "own holdings", /* 3 */
70 1.15.22.2 matt "holdings", /* 4 */ "mortgage", /* 5 */
71 1.15.22.2 matt "unmortgage", /* 6 */ "buy houses", /* 7 */
72 1.15.22.2 matt "sell houses", /* 8 */ "card", /* 9 */
73 1.15.22.2 matt "pay", /* 10 */ "trade", /* 11 */
74 1.15.22.2 matt "resign", /* 12 */ "save", /* 13 */
75 1.15.22.2 matt "restore", /* 14 */ "roll", /* 15 */
76 1.15.22.2 matt "", /* 16 */
77 1.15.22.2 matt 0
78 1.15.22.2 matt },
79 1.15.22.2 matt *const yncoms[] = { /* list of commands for yes/no answers */
80 1.15.22.2 matt "yes", /* 0 */ "no", /* 1 */
81 1.15.22.2 matt "quit", /* 2 */ "print", /* 3 */
82 1.15.22.2 matt "where", /* 4 */ "own holdings", /* 5 */
83 1.15.22.2 matt "holdings", /* 6 */
84 1.15.22.2 matt 0
85 1.15.22.2 matt },
86 1.15.22.2 matt *const lucky_mes[] = { /* "got lucky" messages */
87 1.15.22.2 matt "You lucky stiff", "You got lucky",
88 1.15.22.2 matt "What a lucky person!", "You must have a 4-leaf clover",
89 1.15.22.2 matt "My, my! Aren't we lucky!", "Luck smiles upon you",
90 1.15.22.2 matt "You got lucky this time", "Lucky person!",
91 1.15.22.2 matt "Your karma must certainly be together",
92 1.15.22.2 matt "How beautifully Cosmic", "Wow, you must be really with it"
93 1.15.22.2 matt /* "I want your autograph", -- Save for later */
94 1.15.22.2 matt };
95 1.15.22.2 matt
96 1.15.22.2 matt int player, /* current player number */
97 1.15.22.2 matt num_play, /* current number of players */
98 1.15.22.2 matt num_doub, /* # of doubles current player rolled */
99 1.15.22.2 matt /* # of "got lucky" messages */
100 1.15.22.2 matt num_luck = sizeof lucky_mes / sizeof (char *);
101 1.15.22.2 matt
102 1.15.22.2 matt /* list of command functions */
103 1.15.22.2 matt void (*const func[])(void) = { /* array of function calls for commands */
104 1.15.22.2 matt quit, /* quit game |* 0 *| */
105 1.15.22.2 matt printboard, /* print board |* 1 *| */
106 1.15.22.2 matt where, /* where players are |* 2 *| */
107 1.15.22.2 matt list, /* own holdings |* 3 *| */
108 1.15.22.2 matt list_all, /* holdings list |* 4 *| */
109 1.15.22.2 matt mortgage, /* mortgage property |* 5 *| */
110 1.15.22.2 matt unmortgage, /* unmortgage property |* 6 *| */
111 1.15.22.2 matt buy_houses, /* buy houses |* 7 *| */
112 1.15.22.2 matt sell_houses, /* sell houses |* 8 *| */
113 1.15.22.2 matt card, /* card for jail |* 9 *| */
114 1.15.22.2 matt pay, /* pay for jail |* 10 *| */
115 1.15.22.2 matt trade, /* trade |* 11 *| */
116 1.15.22.2 matt resign, /* resign |* 12 *| */
117 1.15.22.2 matt save, /* save game |* 13 *| */
118 1.15.22.2 matt restore, /* restore game |* 14 *| */
119 1.15.22.2 matt do_move, /* roll |* 15 *| */
120 1.15.22.2 matt do_move /* "" |* 16 *| */
121 1.15.22.2 matt };
122 1.15.22.2 matt
123 1.15.22.2 matt DECK deck[2]; /* Chance and Community Chest */
124 1.15.22.2 matt
125 1.15.22.2 matt PLAY *play, /* player structure array ("calloc"ed) */
126 1.15.22.2 matt *cur_p; /* pointer to current player's struct */
127 1.15.22.2 matt
128 1.15.22.2 matt RR_S rr[N_RR]; /* railroad descriptions */
129 1.15.22.2 matt
130 1.15.22.2 matt UTIL_S util[2]; /* utility descriptions */
131 1.15.22.2 matt
132 1.15.22.2 matt #define MONINIT(num_in, h_cost, not_m, mon_n, sq1,sq2,sq3) \
133 1.15.22.2 matt {0, -1, num_in, 0, h_cost, not_m, mon_n, {sq1,sq2,sq3}, {0,0,0}}
134 1.15.22.2 matt /* name owner num_own sq */
135 1.15.22.2 matt
136 1.15.22.2 matt MON mon[N_MON] = { /* monopoly descriptions */
137 1.15.22.2 matt /* num_in h_cost not_m mon_n sqnums */
138 1.15.22.2 matt MONINIT(2, 1, "Purple", "PURPLE", 1,3, 0),
139 1.15.22.2 matt MONINIT(3, 1, "Lt. Blue", "LT. BLUE", 6,8,9),
140 1.15.22.2 matt MONINIT(3, 2, "Violet", "VIOLET", 11,13,14),
141 1.15.22.2 matt MONINIT(3, 2, "Orange", "ORANGE", 16,18,19),
142 1.15.22.2 matt MONINIT(3, 3, "Red", "RED", 21,23,24),
143 1.15.22.2 matt MONINIT(3, 3, "Yellow", "YELLOW", 26,27,29),
144 1.15.22.2 matt MONINIT(3, 4, "Green", "GREEN", 31,32,34),
145 1.15.22.2 matt MONINIT(2, 4, "Dk. Blue", "DK. BLUE", 37,39, 0),
146 1.15.22.2 matt };
147 1.15.22.2 matt #undef MONINIT
148 1.15.22.2 matt
149 1.15.22.2 matt PROP prop[N_PROP] = { /* typical properties */
150 1.15.22.2 matt /* morg monop square houses mon_desc rent */
151 1.15.22.2 matt {0, 0, 1, 0, &mon[0], { 2, 10, 30, 90, 160, 250} },
152 1.15.22.2 matt {0, 0, 3, 0, &mon[0], { 4, 20, 60, 180, 320, 450} },
153 1.15.22.2 matt {0, 0, 6, 0, &mon[1], { 6, 30, 90, 270, 400, 550} },
154 1.15.22.2 matt {0, 0, 7, 0, &mon[1], { 6, 30, 90, 270, 400, 550} },
155 1.15.22.2 matt {0, 0, 9, 0, &mon[1], { 8, 40,100, 300, 450, 600} },
156 1.15.22.2 matt {0, 0, 11, 0, &mon[2], {10, 50,150, 450, 625, 750} },
157 1.15.22.2 matt {0, 0, 13, 0, &mon[2], {10, 50,150, 450, 625, 750} },
158 1.15.22.2 matt {0, 0, 14, 0, &mon[2], {12, 60,180, 500, 700, 900} },
159 1.15.22.2 matt {0, 0, 16, 0, &mon[3], {14, 70,200, 550, 750, 950} },
160 1.15.22.2 matt {0, 0, 17, 0, &mon[3], {14, 70,200, 550, 750, 950} },
161 1.15.22.2 matt {0, 0, 19, 0, &mon[3], {16, 80,220, 600, 800,1000} },
162 1.15.22.2 matt {0, 0, 21, 0, &mon[4], {18, 90,250, 700, 875,1050} },
163 1.15.22.2 matt {0, 0, 23, 0, &mon[4], {18, 90,250, 700, 875,1050} },
164 1.15.22.2 matt {0, 0, 24, 0, &mon[4], {20,100,300, 750, 925,1100} },
165 1.15.22.2 matt {0, 0, 26, 0, &mon[5], {22,110,330, 800, 975,1150} },
166 1.15.22.2 matt {0, 0, 27, 0, &mon[5], {22,110,330, 800, 975,1150} },
167 1.15.22.2 matt {0, 0, 29, 0, &mon[5], {24,120,360, 850,1025,1200} },
168 1.15.22.2 matt {0, 0, 31, 0, &mon[6], {26,130,390, 900,1100,1275} },
169 1.15.22.2 matt {0, 0, 32, 0, &mon[6], {26,130,390, 900,1100,1275} },
170 1.15.22.2 matt {0, 0, 34, 0, &mon[6], {28,150,450,1000,1200,1400} },
171 1.15.22.2 matt {0, 0, 37, 0, &mon[7], {35,175,500,1100,1300,1500} },
172 1.15.22.2 matt {0, 0, 39, 0, &mon[7], {50,200,600,1400,1700,2000} }
173 1.15.22.2 matt };
174 1.15.22.2 matt
175 1.15.22.2 matt SQUARE board[N_SQRS+1] = { /* board itself (+1 for Jail) */
176 1.15.22.2 matt /* name (COLOR) owner type desc cost */
177 1.15.22.2 matt
178 1.15.22.2 matt {"=== GO ===", -1, SAFE, NULL, 0 },
179 1.15.22.2 matt {"Mediterranean Ave. (P)", -1, PRPTY, &prop[0], 60 },
180 1.15.22.2 matt {"Community Chest i", -1, CC, NULL, 0 },
181 1.15.22.2 matt {"Baltic Ave. (P)", -1, PRPTY, &prop[1], 60 },
182 1.15.22.2 matt {"Income Tax", -1, INC_TAX, NULL, 0 },
183 1.15.22.2 matt {"Reading RR", -1, RR, &rr[0], 200 },
184 1.15.22.2 matt {"Oriental Ave. (L)", -1, PRPTY, &prop[2], 100 },
185 1.15.22.2 matt {"Chance i", -1, CHANCE, NULL, 0 },
186 1.15.22.2 matt {"Vermont Ave. (L)", -1, PRPTY, &prop[3], 100 },
187 1.15.22.2 matt {"Connecticut Ave. (L)", -1, PRPTY, &prop[4], 120 },
188 1.15.22.2 matt {"Just Visiting", -1, SAFE, NULL, 0 },
189 1.15.22.2 matt {"St. Charles Pl. (V)", -1, PRPTY, &prop[5], 140 },
190 1.15.22.2 matt {"Electric Co.", -1, UTIL, &util[0], 150 },
191 1.15.22.2 matt {"States Ave. (V)", -1, PRPTY, &prop[6], 140 },
192 1.15.22.2 matt {"Virginia Ave. (V)", -1, PRPTY, &prop[7], 160 },
193 1.15.22.2 matt {"Pennsylvania RR", -1, RR, &rr[1], 200 },
194 1.15.22.2 matt {"St. James Pl. (O)", -1, PRPTY, &prop[8], 180 },
195 1.15.22.2 matt {"Community Chest ii", -1, CC, NULL, 0 },
196 1.15.22.2 matt {"Tennessee Ave. (O)", -1, PRPTY, &prop[9], 180 },
197 1.15.22.2 matt {"New York Ave. (O)", -1, PRPTY, &prop[10], 200 },
198 1.15.22.2 matt {"Free Parking", -1, SAFE, NULL, 0 },
199 1.15.22.2 matt {"Kentucky Ave. (R)", -1, PRPTY, &prop[11], 220 },
200 1.15.22.2 matt {"Chance ii", -1, CHANCE, NULL, 0 },
201 1.15.22.2 matt {"Indiana Ave. (R)", -1, PRPTY, &prop[12], 220 },
202 1.15.22.2 matt {"Illinois Ave. (R)", -1, PRPTY, &prop[13], 240 },
203 1.15.22.2 matt {"B&O RR", -1, RR, &rr[2], 200 },
204 1.15.22.2 matt {"Atlantic Ave. (Y)", -1, PRPTY, &prop[14], 260 },
205 1.15.22.2 matt {"Ventnor Ave. (Y)", -1, PRPTY, &prop[15], 260 },
206 1.15.22.2 matt {"Water Works", -1, UTIL, &util[1], 150 },
207 1.15.22.2 matt {"Marvin Gardens (Y)", -1, PRPTY, &prop[16], 280 },
208 1.15.22.2 matt {"GO TO JAIL", -1, GOTO_J, NULL, 0 },
209 1.15.22.2 matt {"Pacific Ave. (G)", -1, PRPTY, &prop[17], 300 },
210 1.15.22.2 matt {"N. Carolina Ave. (G)", -1, PRPTY, &prop[18], 300 },
211 1.15.22.2 matt {"Community Chest iii", -1, CC, NULL, 0 },
212 1.15.22.2 matt {"Pennsylvania Ave. (G)", -1, PRPTY, &prop[19], 320 },
213 1.15.22.2 matt {"Short Line RR", -1, RR, &rr[3], 200 },
214 1.15.22.2 matt {"Chance iii", -1, CHANCE, NULL, 0 },
215 1.15.22.2 matt {"Park Place (D)", -1, PRPTY, &prop[20], 350 },
216 1.15.22.2 matt {"Luxury Tax", -1, LUX_TAX, NULL, 0 },
217 1.15.22.2 matt {"Boardwalk (D)", -1, PRPTY, &prop[21], 400 },
218 1.15.22.2 matt {"JAIL", -1, IN_JAIL, NULL, 0 }
219 1.15.22.2 matt };
220 1.15.22.2 matt
221 1.1 cgd
222 1.1 cgd /*
223 1.1 cgd * This program implements a monopoly game
224 1.1 cgd */
225 1.4 christos int
226 1.1 cgd main(ac, av)
227 1.6 simonb int ac;
228 1.6 simonb char *av[];
229 1.6 simonb {
230 1.9 jsm /* Revoke setgid privileges */
231 1.11 mycroft setgid(getgid());
232 1.9 jsm
233 1.15.22.2 matt srandom((unsigned long)time(NULL));
234 1.15.22.2 matt num_luck = sizeof lucky_mes / sizeof (char *);
235 1.15.22.2 matt init_decks();
236 1.15.22.2 matt init_monops();
237 1.1 cgd if (ac > 1) {
238 1.15.22.2 matt if (rest_f(av[1]) < 0)
239 1.1 cgd restore();
240 1.1 cgd }
241 1.1 cgd else {
242 1.1 cgd getplayers();
243 1.1 cgd init_players();
244 1.1 cgd }
245 1.5 hubertf signal(SIGINT, do_quit);
246 1.1 cgd for (;;) {
247 1.1 cgd printf("\n%s (%d) (cash $%d) on %s\n", cur_p->name, player + 1,
248 1.1 cgd cur_p->money, board[cur_p->loc].name);
249 1.1 cgd printturn();
250 1.1 cgd force_morg();
251 1.1 cgd execute(getinp("-- Command: ", comlist));
252 1.1 cgd }
253 1.1 cgd }
254 1.4 christos
255 1.4 christos /*ARGSUSED*/
256 1.4 christos static void
257 1.4 christos do_quit(n)
258 1.15.22.1 matt int n __unused;
259 1.4 christos {
260 1.4 christos quit();
261 1.4 christos }
262 1.6 simonb
263 1.1 cgd /*
264 1.1 cgd * This routine gets the names of the players
265 1.1 cgd */
266 1.4 christos static void
267 1.4 christos getplayers()
268 1.4 christos {
269 1.6 simonb int i, j;
270 1.6 simonb char buf[257];
271 1.1 cgd
272 1.1 cgd blew_it:
273 1.1 cgd for (;;) {
274 1.15.22.2 matt if ((num_play = get_int("How many players? ")) <= 1 ||
275 1.1 cgd num_play > MAX_PL)
276 1.15.22.2 matt printf("Sorry. Number must range from 2 to %d\n",
277 1.15.22.2 matt MAX_PL);
278 1.1 cgd else
279 1.1 cgd break;
280 1.1 cgd }
281 1.15.22.2 matt cur_p = play = calloc((size_t)num_play, sizeof (PLAY));
282 1.8 jsm if (play == NULL)
283 1.10 jsm err(1, NULL);
284 1.1 cgd for (i = 0; i < num_play; i++) {
285 1.15.22.2 matt do {
286 1.15.22.2 matt printf("Player %d's name: ", i + 1);
287 1.15.22.2 matt fgets(buf, sizeof(buf), stdin);
288 1.15.22.2 matt if (feof(stdin)) {
289 1.15.22.2 matt printf("End of file on stdin\n");
290 1.15.22.2 matt exit(0);
291 1.15.22.2 matt }
292 1.15.22.2 matt buf[strcspn(buf, "\n")] = '\0';
293 1.15.22.2 matt } while (strlen(buf) == 0);
294 1.15.22.2 matt name_list[i] = play[i].name = strdup(buf);
295 1.8 jsm if (name_list[i] == NULL)
296 1.10 jsm err(1, NULL);
297 1.1 cgd play[i].money = 1500;
298 1.1 cgd }
299 1.1 cgd name_list[i++] = "done";
300 1.1 cgd name_list[i] = 0;
301 1.1 cgd for (i = 0; i < num_play; i++)
302 1.15.22.2 matt for (j = i + 1; j <= num_play; j++)
303 1.1 cgd if (strcasecmp(name_list[i], name_list[j]) == 0) {
304 1.15.22.2 matt if (j != num_play)
305 1.6 simonb printf("Hey!!! Some of those are "
306 1.6 simonb "IDENTICAL!! Let's try that "
307 1.15.22.2 matt "again...\n");
308 1.1 cgd else
309 1.6 simonb printf("\"done\" is a reserved word. "
310 1.6 simonb "Please try again\n");
311 1.1 cgd for (i = 0; i < num_play; i++)
312 1.4 christos free(play[i].name);
313 1.4 christos free(play);
314 1.1 cgd goto blew_it;
315 1.1 cgd }
316 1.1 cgd }
317 1.6 simonb
318 1.1 cgd /*
319 1.1 cgd * This routine figures out who goes first
320 1.1 cgd */
321 1.4 christos static void
322 1.4 christos init_players()
323 1.4 christos {
324 1.6 simonb int i, rl, cur_max;
325 1.6 simonb bool over = 0;
326 1.6 simonb int max_pl = 0;
327 1.1 cgd
328 1.1 cgd again:
329 1.1 cgd putchar('\n');
330 1.1 cgd for (cur_max = i = 0; i < num_play; i++) {
331 1.1 cgd printf("%s (%d) rolls %d\n", play[i].name, i+1, rl=roll(2, 6));
332 1.1 cgd if (rl > cur_max) {
333 1.1 cgd over = FALSE;
334 1.1 cgd cur_max = rl;
335 1.1 cgd max_pl = i;
336 1.1 cgd }
337 1.1 cgd else if (rl == cur_max)
338 1.1 cgd over++;
339 1.1 cgd }
340 1.1 cgd if (over) {
341 1.1 cgd printf("%d people rolled the same thing, so we'll try again\n",
342 1.1 cgd over + 1);
343 1.1 cgd goto again;
344 1.1 cgd }
345 1.1 cgd player = max_pl;
346 1.1 cgd cur_p = &play[max_pl];
347 1.1 cgd printf("%s (%d) goes first\n", cur_p->name, max_pl + 1);
348 1.1 cgd }
349 1.6 simonb
350 1.1 cgd /*
351 1.12 wiz * This routine initializes the monopoly structures.
352 1.1 cgd */
353 1.4 christos static void
354 1.15.22.2 matt init_monops()
355 1.4 christos {
356 1.6 simonb MON *mp;
357 1.6 simonb int i;
358 1.1 cgd
359 1.1 cgd for (mp = mon; mp < &mon[N_MON]; mp++) {
360 1.1 cgd mp->name = mp->not_m;
361 1.1 cgd for (i = 0; i < mp->num_in; i++)
362 1.1 cgd mp->sq[i] = &board[mp->sqnums[i]];
363 1.1 cgd }
364 1.1 cgd }
365