John Gilmore's changes for ANSI C and general cleanup
[unix-history] / usr / src / games / monop / monop.c
CommitLineData
5b162ab4
KB
1/*
2 * Copyright (c) 1987 Regents of the University of California.
e10e1c15
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
5b162ab4
KB
11 */
12
13#ifndef lint
e10e1c15
KB
14char copyright[] =
15"@(#) Copyright (c) 1987 Regents of the University of California.\n\
16 All rights reserved.\n";
17#endif /* not lint */
18
19#ifndef lint
2497392a 20static char sccsid[] = "@(#)monop.c 5.4 (Berkeley) %G%";
e10e1c15 21#endif /* not lint */
5b162ab4
KB
22
23# include "monop.def"
24
25/*
26 * This program implements a monopoly game
27 */
28main(ac, av)
29reg int ac;
30reg char *av[]; {
31
32
33 srand(getpid());
34 if (ac > 1) {
35 if (!rest_f(av[1]))
36 restore();
37 }
38 else {
39 getplayers();
40 init_players();
41 init_monops();
42 }
43 num_luck = sizeof lucky_mes / sizeof (char *);
44 init_decks();
45 signal(2, quit);
46 for (;;) {
47 printf("\n%s (%d) (cash $%d) on %s\n", cur_p->name, player + 1,
48 cur_p->money, board[cur_p->loc].name);
49 printturn();
50 force_morg();
51 execute(getinp("-- Command: ", comlist));
52 }
53}
54/*
55 * This routine gets the names of the players
56 */
57getplayers() {
58
59 reg char *sp;
60 reg int i, j;
61 char buf[257];
62
63blew_it:
64 for (;;) {
65 if ((num_play=get_int("How many players? ")) <= 0 ||
66 num_play > MAX_PL)
67 printf("Sorry. Number must range from 1 to 9\n");
68 else
69 break;
70 }
71 cur_p = play = (PLAY *) calloc(num_play, sizeof (PLAY));
72 for (i = 0; i < num_play; i++) {
73over:
74 printf("Player %d's name: ", i + 1);
75 for (sp = buf; (*sp=getchar()) != '\n'; sp++)
76 continue;
77 if (sp == buf)
78 goto over;
79 *sp++ = '\0';
80 strcpy(name_list[i]=play[i].name=(char *)calloc(1,sp-buf),buf);
81 play[i].money = 1500;
82 }
83 name_list[i++] = "done";
84 name_list[i] = 0;
85 for (i = 0; i < num_play; i++)
86 for (j = i + 1; j < num_play; j++)
665236b2 87 if (strcasecmp(name_list[i], name_list[j]) == 0) {
5b162ab4
KB
88 if (i != num_play - 1)
89 printf("Hey!!! Some of those are IDENTICAL!! Let's try that again....\n");
90 else
91 printf("\"done\" is a reserved word. Please try again\n");
92 for (i = 0; i < num_play; i++)
93 cfree(play[i].name);
94 cfree(play);
95 goto blew_it;
96 }
97}
98/*
99 * This routine figures out who goes first
100 */
101init_players() {
102
103 reg int i, rl, cur_max;
104 bool over;
105 int max_pl;
106
107again:
108 putchar('\n');
109 for (cur_max = i = 0; i < num_play; i++) {
110 printf("%s (%d) rolls %d\n", play[i].name, i+1, rl=roll(2, 6));
111 if (rl > cur_max) {
112 over = FALSE;
113 cur_max = rl;
114 max_pl = i;
115 }
116 else if (rl == cur_max)
117 over++;
118 }
119 if (over) {
120 printf("%d people rolled the same thing, so we'll try again\n",
121 over + 1);
122 goto again;
123 }
124 player = max_pl;
125 cur_p = &play[max_pl];
126 printf("%s (%d) goes first\n", cur_p->name, max_pl + 1);
127}
128/*
129 * This routine initalizes the monopoly structures.
130 */
131init_monops() {
132
133 reg MON *mp;
134 reg int i;
135
136 for (mp = mon; mp < &mon[N_MON]; mp++) {
137 mp->name = mp->not_m;
138 for (i = 0; i < mp->num_in; i++)
2497392a 139 mp->sq[i] = &board[mp->sqnum[i]];
5b162ab4
KB
140 }
141}