monop.c revision 1.24 1 1.24 lukem /* $NetBSD: monop.c,v 1.24 2008/07/20 01:03:21 lukem 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.24 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 1.24 lukem The Regents of the University of California. All rights reserved.");
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.24 lukem __RCSID("$NetBSD: monop.c,v 1.24 2008/07/20 01:03:21 lukem 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.18 dholland #include <time.h>
50 1.4 christos #include <unistd.h>
51 1.21 dholland #include "deck.h"
52 1.21 dholland #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.1 cgd
60 1.21 dholland
61 1.21 dholland bool fixing, /* set if fixing up debt */
62 1.21 dholland trading, /* set if in process of trading */
63 1.21 dholland told_em, /* set if told user he's out of debt */
64 1.21 dholland spec; /* set if moving by card to RR or UTIL */
65 1.21 dholland
66 1.21 dholland const char *name_list[MAX_PL+2], /* list of players' names */
67 1.21 dholland *const comlist[] = { /* list of normal commands */
68 1.21 dholland "quit", /* 0 */ "print", /* 1 */
69 1.21 dholland "where", /* 2 */ "own holdings", /* 3 */
70 1.21 dholland "holdings", /* 4 */ "mortgage", /* 5 */
71 1.21 dholland "unmortgage", /* 6 */ "buy houses", /* 7 */
72 1.21 dholland "sell houses", /* 8 */ "card", /* 9 */
73 1.21 dholland "pay", /* 10 */ "trade", /* 11 */
74 1.21 dholland "resign", /* 12 */ "save", /* 13 */
75 1.21 dholland "restore", /* 14 */ "roll", /* 15 */
76 1.21 dholland "", /* 16 */
77 1.21 dholland 0
78 1.21 dholland },
79 1.21 dholland *const yncoms[] = { /* list of commands for yes/no answers */
80 1.21 dholland "yes", /* 0 */ "no", /* 1 */
81 1.21 dholland "quit", /* 2 */ "print", /* 3 */
82 1.21 dholland "where", /* 4 */ "own holdings", /* 5 */
83 1.21 dholland "holdings", /* 6 */
84 1.21 dholland 0
85 1.21 dholland },
86 1.21 dholland *const lucky_mes[] = { /* "got lucky" messages */
87 1.21 dholland "You lucky stiff", "You got lucky",
88 1.21 dholland "What a lucky person!", "You must have a 4-leaf clover",
89 1.21 dholland "My, my! Aren't we lucky!", "Luck smiles upon you",
90 1.21 dholland "You got lucky this time", "Lucky person!",
91 1.21 dholland "Your karma must certainly be together",
92 1.21 dholland "How beautifully Cosmic", "Wow, you must be really with it"
93 1.21 dholland /* "I want your autograph", -- Save for later */
94 1.21 dholland };
95 1.21 dholland
96 1.21 dholland int player, /* current player number */
97 1.21 dholland num_play, /* current number of players */
98 1.21 dholland num_doub, /* # of doubles current player rolled */
99 1.21 dholland /* # of "got lucky" messages */
100 1.21 dholland num_luck = sizeof lucky_mes / sizeof (char *);
101 1.21 dholland
102 1.21 dholland /* list of command functions */
103 1.21 dholland void (*const func[])(void) = { /* array of function calls for commands */
104 1.21 dholland quit, /* quit game |* 0 *| */
105 1.21 dholland printboard, /* print board |* 1 *| */
106 1.21 dholland where, /* where players are |* 2 *| */
107 1.21 dholland list, /* own holdings |* 3 *| */
108 1.21 dholland list_all, /* holdings list |* 4 *| */
109 1.21 dholland mortgage, /* mortgage property |* 5 *| */
110 1.21 dholland unmortgage, /* unmortgage property |* 6 *| */
111 1.21 dholland buy_houses, /* buy houses |* 7 *| */
112 1.21 dholland sell_houses, /* sell houses |* 8 *| */
113 1.21 dholland card, /* card for jail |* 9 *| */
114 1.21 dholland pay, /* pay for jail |* 10 *| */
115 1.21 dholland trade, /* trade |* 11 *| */
116 1.21 dholland resign, /* resign |* 12 *| */
117 1.21 dholland save, /* save game |* 13 *| */
118 1.21 dholland restore, /* restore game |* 14 *| */
119 1.21 dholland do_move, /* roll |* 15 *| */
120 1.21 dholland do_move /* "" |* 16 *| */
121 1.21 dholland };
122 1.21 dholland
123 1.21 dholland DECK deck[2]; /* Chance and Community Chest */
124 1.21 dholland
125 1.21 dholland PLAY *play, /* player structure array ("calloc"ed) */
126 1.21 dholland *cur_p; /* pointer to current player's struct */
127 1.21 dholland
128 1.21 dholland RR_S rr[N_RR]; /* railroad descriptions */
129 1.21 dholland
130 1.21 dholland UTIL_S util[2]; /* utility descriptions */
131 1.21 dholland
132 1.21 dholland #define MONINIT(num_in, h_cost, not_m, mon_n, sq1,sq2,sq3) \
133 1.21 dholland {0, -1, num_in, 0, h_cost, not_m, mon_n, {sq1,sq2,sq3}, {0,0,0}}
134 1.21 dholland /* name owner num_own sq */
135 1.21 dholland
136 1.21 dholland MON mon[N_MON] = { /* monopoly descriptions */
137 1.21 dholland /* num_in h_cost not_m mon_n sqnums */
138 1.21 dholland MONINIT(2, 1, "Purple", "PURPLE", 1,3, 0),
139 1.21 dholland MONINIT(3, 1, "Lt. Blue", "LT. BLUE", 6,8,9),
140 1.21 dholland MONINIT(3, 2, "Violet", "VIOLET", 11,13,14),
141 1.21 dholland MONINIT(3, 2, "Orange", "ORANGE", 16,18,19),
142 1.21 dholland MONINIT(3, 3, "Red", "RED", 21,23,24),
143 1.21 dholland MONINIT(3, 3, "Yellow", "YELLOW", 26,27,29),
144 1.21 dholland MONINIT(3, 4, "Green", "GREEN", 31,32,34),
145 1.21 dholland MONINIT(2, 4, "Dk. Blue", "DK. BLUE", 37,39, 0),
146 1.21 dholland };
147 1.21 dholland #undef MONINIT
148 1.21 dholland
149 1.21 dholland PROP prop[N_PROP] = { /* typical properties */
150 1.21 dholland /* morg monop square houses mon_desc rent */
151 1.21 dholland {0, 0, 1, 0, &mon[0], { 2, 10, 30, 90, 160, 250} },
152 1.21 dholland {0, 0, 3, 0, &mon[0], { 4, 20, 60, 180, 320, 450} },
153 1.21 dholland {0, 0, 6, 0, &mon[1], { 6, 30, 90, 270, 400, 550} },
154 1.21 dholland {0, 0, 7, 0, &mon[1], { 6, 30, 90, 270, 400, 550} },
155 1.21 dholland {0, 0, 9, 0, &mon[1], { 8, 40,100, 300, 450, 600} },
156 1.21 dholland {0, 0, 11, 0, &mon[2], {10, 50,150, 450, 625, 750} },
157 1.21 dholland {0, 0, 13, 0, &mon[2], {10, 50,150, 450, 625, 750} },
158 1.21 dholland {0, 0, 14, 0, &mon[2], {12, 60,180, 500, 700, 900} },
159 1.21 dholland {0, 0, 16, 0, &mon[3], {14, 70,200, 550, 750, 950} },
160 1.21 dholland {0, 0, 17, 0, &mon[3], {14, 70,200, 550, 750, 950} },
161 1.21 dholland {0, 0, 19, 0, &mon[3], {16, 80,220, 600, 800,1000} },
162 1.21 dholland {0, 0, 21, 0, &mon[4], {18, 90,250, 700, 875,1050} },
163 1.21 dholland {0, 0, 23, 0, &mon[4], {18, 90,250, 700, 875,1050} },
164 1.21 dholland {0, 0, 24, 0, &mon[4], {20,100,300, 750, 925,1100} },
165 1.21 dholland {0, 0, 26, 0, &mon[5], {22,110,330, 800, 975,1150} },
166 1.21 dholland {0, 0, 27, 0, &mon[5], {22,110,330, 800, 975,1150} },
167 1.21 dholland {0, 0, 29, 0, &mon[5], {24,120,360, 850,1025,1200} },
168 1.21 dholland {0, 0, 31, 0, &mon[6], {26,130,390, 900,1100,1275} },
169 1.21 dholland {0, 0, 32, 0, &mon[6], {26,130,390, 900,1100,1275} },
170 1.21 dholland {0, 0, 34, 0, &mon[6], {28,150,450,1000,1200,1400} },
171 1.21 dholland {0, 0, 37, 0, &mon[7], {35,175,500,1100,1300,1500} },
172 1.21 dholland {0, 0, 39, 0, &mon[7], {50,200,600,1400,1700,2000} }
173 1.21 dholland };
174 1.21 dholland
175 1.21 dholland SQUARE board[N_SQRS+1] = { /* board itself (+1 for Jail) */
176 1.21 dholland /* name (COLOR) owner type desc cost */
177 1.21 dholland
178 1.21 dholland {"=== GO ===", -1, SAFE, NULL, 0 },
179 1.21 dholland {"Mediterranean Ave. (P)", -1, PRPTY, &prop[0], 60 },
180 1.21 dholland {"Community Chest i", -1, CC, NULL, 0 },
181 1.21 dholland {"Baltic Ave. (P)", -1, PRPTY, &prop[1], 60 },
182 1.21 dholland {"Income Tax", -1, INC_TAX, NULL, 0 },
183 1.21 dholland {"Reading RR", -1, RR, &rr[0], 200 },
184 1.21 dholland {"Oriental Ave. (L)", -1, PRPTY, &prop[2], 100 },
185 1.21 dholland {"Chance i", -1, CHANCE, NULL, 0 },
186 1.21 dholland {"Vermont Ave. (L)", -1, PRPTY, &prop[3], 100 },
187 1.21 dholland {"Connecticut Ave. (L)", -1, PRPTY, &prop[4], 120 },
188 1.21 dholland {"Just Visiting", -1, SAFE, NULL, 0 },
189 1.21 dholland {"St. Charles Pl. (V)", -1, PRPTY, &prop[5], 140 },
190 1.21 dholland {"Electric Co.", -1, UTIL, &util[0], 150 },
191 1.21 dholland {"States Ave. (V)", -1, PRPTY, &prop[6], 140 },
192 1.21 dholland {"Virginia Ave. (V)", -1, PRPTY, &prop[7], 160 },
193 1.21 dholland {"Pennsylvania RR", -1, RR, &rr[1], 200 },
194 1.21 dholland {"St. James Pl. (O)", -1, PRPTY, &prop[8], 180 },
195 1.21 dholland {"Community Chest ii", -1, CC, NULL, 0 },
196 1.21 dholland {"Tennessee Ave. (O)", -1, PRPTY, &prop[9], 180 },
197 1.21 dholland {"New York Ave. (O)", -1, PRPTY, &prop[10], 200 },
198 1.21 dholland {"Free Parking", -1, SAFE, NULL, 0 },
199 1.21 dholland {"Kentucky Ave. (R)", -1, PRPTY, &prop[11], 220 },
200 1.21 dholland {"Chance ii", -1, CHANCE, NULL, 0 },
201 1.21 dholland {"Indiana Ave. (R)", -1, PRPTY, &prop[12], 220 },
202 1.21 dholland {"Illinois Ave. (R)", -1, PRPTY, &prop[13], 240 },
203 1.21 dholland {"B&O RR", -1, RR, &rr[2], 200 },
204 1.21 dholland {"Atlantic Ave. (Y)", -1, PRPTY, &prop[14], 260 },
205 1.21 dholland {"Ventnor Ave. (Y)", -1, PRPTY, &prop[15], 260 },
206 1.21 dholland {"Water Works", -1, UTIL, &util[1], 150 },
207 1.21 dholland {"Marvin Gardens (Y)", -1, PRPTY, &prop[16], 280 },
208 1.21 dholland {"GO TO JAIL", -1, GOTO_J, NULL, 0 },
209 1.21 dholland {"Pacific Ave. (G)", -1, PRPTY, &prop[17], 300 },
210 1.21 dholland {"N. Carolina Ave. (G)", -1, PRPTY, &prop[18], 300 },
211 1.21 dholland {"Community Chest iii", -1, CC, NULL, 0 },
212 1.21 dholland {"Pennsylvania Ave. (G)", -1, PRPTY, &prop[19], 320 },
213 1.21 dholland {"Short Line RR", -1, RR, &rr[3], 200 },
214 1.21 dholland {"Chance iii", -1, CHANCE, NULL, 0 },
215 1.21 dholland {"Park Place (D)", -1, PRPTY, &prop[20], 350 },
216 1.21 dholland {"Luxury Tax", -1, LUX_TAX, NULL, 0 },
217 1.21 dholland {"Boardwalk (D)", -1, PRPTY, &prop[21], 400 },
218 1.21 dholland {"JAIL", -1, IN_JAIL, NULL, 0 }
219 1.21 dholland };
220 1.21 dholland
221 1.21 dholland
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.22 christos srandom((unsigned long)time(NULL));
234 1.19 dholland num_luck = sizeof lucky_mes / sizeof (char *);
235 1.19 dholland init_decks();
236 1.19 dholland init_monops();
237 1.1 cgd if (ac > 1) {
238 1.23 dholland 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.16 perry 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.19 dholland if ((num_play = get_int("How many players? ")) <= 1 ||
275 1.1 cgd num_play > MAX_PL)
276 1.19 dholland printf("Sorry. Number must range from 2 to %d\n",
277 1.19 dholland MAX_PL);
278 1.1 cgd else
279 1.1 cgd break;
280 1.1 cgd }
281 1.22 christos 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.19 dholland do {
286 1.19 dholland printf("Player %d's name: ", i + 1);
287 1.19 dholland fgets(buf, sizeof(buf), stdin);
288 1.19 dholland if (feof(stdin)) {
289 1.19 dholland printf("End of file on stdin\n");
290 1.19 dholland exit(0);
291 1.19 dholland }
292 1.19 dholland buf[strcspn(buf, "\n")] = '\0';
293 1.19 dholland } while (strlen(buf) == 0);
294 1.19 dholland 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.19 dholland for (j = i + 1; j <= num_play; j++)
303 1.1 cgd if (strcasecmp(name_list[i], name_list[j]) == 0) {
304 1.19 dholland 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.19 dholland "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.17 dholland 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