1 /* $OpenBSD: monop.c,v 1.9 2007/09/11 15:21:05 gilles 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 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1980, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)monop.c 8.1 (Berkeley) 5/31/93"; 42 #else 43 static const char rcsid[] = "$OpenBSD: monop.c,v 1.9 2007/09/11 15:21:05 gilles Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include <err.h> 48 #include <stdlib.h> 49 #include <unistd.h> 50 #include "monop.def" 51 52 static void getplayers(void); 53 static void init_players(void); 54 static void init_monops(void); 55 56 /* 57 * This program implements a monopoly game 58 */ 59 int 60 main(ac, av) 61 int ac; 62 char *av[]; 63 { 64 srandomdev(); 65 num_luck = sizeof lucky_mes / sizeof (char *); 66 init_decks(); 67 init_monops(); 68 if (ac > 1) { 69 if (!rest_f(av[1])) 70 restore(); 71 } 72 else { 73 getplayers(); 74 init_players(); 75 } 76 for (;;) { 77 printf("\n%s (%d) (cash $%d) on %s\n", cur_p->name, player + 1, 78 cur_p->money, board[(int)cur_p->loc].name); 79 printturn(); 80 force_morg(); 81 execute(getinp("-- Command: ", comlist)); 82 } 83 } 84 85 /* 86 * This routine gets the names of the players 87 */ 88 static void 89 getplayers() 90 { 91 int i, j; 92 char buf[257]; 93 94 blew_it: 95 for (;;) { 96 if ((num_play = get_int("How many players? ")) <= 1 || 97 num_play > MAX_PL) 98 printf("Sorry. Number must range from 2 to %d\n", 99 MAX_PL); 100 else 101 break; 102 } 103 if ((cur_p = play = (PLAY *) calloc(num_play, sizeof (PLAY))) == NULL) 104 err(1, NULL); 105 for (i = 0; i < num_play; i++) { 106 do { 107 printf("Player %d's name: ", i + 1); 108 fgets(buf, sizeof(buf), stdin); 109 if ((feof(stdin))) { 110 printf("user closed input stream, quitting...\n"); 111 exit(0); 112 } 113 buf[strcspn(buf, "\n")] = '\0'; 114 } while (strlen(buf) == 0); 115 if ((name_list[i] = play[i].name = strdup(buf)) == NULL) 116 err(1, NULL); 117 play[i].money = 1500; 118 } 119 name_list[i++] = "done"; 120 name_list[i] = 0; 121 for (i = 0; i < num_play; i++) 122 for (j = i + 1; j <= num_play; j++) 123 if (strcasecmp(name_list[i], name_list[j]) == 0) { 124 if (j != num_play) 125 printf("Hey!!! Some of those are IDENTICAL!! Let's try that again...\n"); 126 else 127 printf("\"done\" is a reserved word. Please try again\n"); 128 for (i = 0; i < num_play; i++) 129 free(play[i].name); 130 cfree(play); 131 goto blew_it; 132 } 133 } 134 /* 135 * This routine figures out who goes first 136 */ 137 static void 138 init_players() 139 { 140 int i, rl, cur_max; 141 bool over = 0; 142 int max_pl = 0; 143 144 again: 145 putchar('\n'); 146 for (cur_max = i = 0; i < num_play; i++) { 147 printf("%s (%d) rolls %d\n", play[i].name, i+1, rl=roll(2, 6)); 148 if (rl > cur_max) { 149 over = FALSE; 150 cur_max = rl; 151 max_pl = i; 152 } 153 else if (rl == cur_max) 154 over++; 155 } 156 if (over) { 157 printf("%d people rolled the same thing, so we'll try again\n", 158 over + 1); 159 goto again; 160 } 161 player = max_pl; 162 cur_p = &play[max_pl]; 163 printf("%s (%d) goes first\n", cur_p->name, max_pl + 1); 164 } 165 /* 166 * This routine initializes the monopoly structures. 167 */ 168 static void 169 init_monops() 170 { 171 MON *mp; 172 int i; 173 174 for (mp = mon; mp < &mon[N_MON]; mp++) { 175 mp->name = mp->not_m; 176 for (i = 0; i < mp->num_in; i++) 177 mp->sq[i] = &board[(int)mp->sqnums[i]]; 178 } 179 } 180