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