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