ANSIfication; bug report 4.3BSD/bin/223
[unix-history] / usr / src / games / monop / trade.c
CommitLineData
3297b3e8 1/*
d99e6414 2 * Copyright (c) 1980 Regents of the University of California.
3297b3e8
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
d99e6414
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
3297b3e8
KB
16 */
17
18#ifndef lint
d99e6414 19static char sccsid[] = "@(#)trade.c 5.3 (Berkeley) %G%";
3297b3e8
KB
20#endif /* not lint */
21
22# include "monop.ext"
23
24static struct trd_st { /* how much to give to other player */
25 int trader; /* trader number */
26 int cash; /* amount of cash */
27 int gojf; /* # get-out-of-jail-free cards */
28 OWN *prop_list; /* property list */
29};
30
31typedef struct trd_st TRADE;
32
33static char *list[MAX_PRP+2];
34
35static int used[MAX_PRP];
36
37static TRADE trades[2];
38
39trade() {
40
41 reg int tradee, i;
42
43 trading = TRUE;
44 for (i = 0; i < 2; i++) {
45 trades[i].cash = 0;
46 trades[i].gojf = FALSE;
47 trades[i].prop_list = NULL;
48 }
49over:
50 if (num_play == 1) {
51 printf("There ain't no-one around to trade WITH!!\n");
52 return;
53 }
54 if (num_play > 2) {
55 tradee = getinp("Which player do you wish to trade with? ",
56 name_list);
57 if (tradee == num_play)
58 return;
59 if (tradee == player) {
60 printf("You can't trade with yourself!\n");
61 goto over;
62 }
63 }
64 else
65 tradee = 1 - player;
66 get_list(0, player);
67 get_list(1, tradee);
68 if (getyn("Do you wish a summary? ") == 0)
69 summate();
70 if (getyn("Is the trade ok? ") == 0)
71 do_trade();
72}
73/*
74 * This routine gets the list of things to be trader for the
75 * player, and puts in the structure given.
76 */
77get_list(struct_no, play_no)
78int struct_no, play_no; {
79
80 reg int sn, pn;
81 reg PLAY *pp;
82 int numin, prop, num_prp;
83 OWN *op;
84 TRADE *tp;
85
86 for (numin = 0; numin < MAX_PRP; numin++)
87 used[numin] = FALSE;
88 sn = struct_no, pn = play_no;
89 pp = &play[pn];
90 tp = &trades[sn];
91 tp->trader = pn;
92 printf("player %s (%d):\n", pp->name, pn+1);
93 if (pp->own_list) {
94 numin = set_list(pp->own_list);
95 for (num_prp = numin; num_prp; ) {
96 prop = getinp("Which property do you wish to trade? ",
97 list);
98 if (prop == numin)
99 break;
100 else if (used[prop])
101 printf("You've already allocated that.\n");
102 else {
103 num_prp--;
104 used[prop] = TRUE;
105 for (op = pp->own_list; prop--; op = op->next)
106 continue;
107 add_list(pn, &(tp->prop_list), sqnum(op->sqr));
108 }
109 }
110 }
111 if (pp->money > 0) {
112 printf("You have $%d. ", pp->money);
113 tp->cash = get_int("How much are you trading? ");
114 }
115 if (pp->num_gojf > 0) {
116once_more:
117 printf("You have %d get-out-of-jail-free cards. ",pp->num_gojf);
118 tp->gojf = get_int("How many are you trading? ");
119 if (tp->gojf > pp->num_gojf) {
120 printf("You don't have that many. Try again.\n");
121 goto once_more;
122 }
123 }
124}
125/*
126 * This routine sets up the list of tradable property.
127 */
128set_list(the_list)
129reg OWN *the_list; {
130
131 reg int i;
132 reg OWN *op;
133
134 i = 0;
135 for (op = the_list; op; op = op->next)
136 if (!used[i])
137 list[i++] = op->sqr->name;
138 list[i++] = "done";
139 list[i--] = 0;
140 return i;
141}
142/*
143 * This routine summates the trade.
144 */
145summate() {
146
147 reg bool some;
148 reg int i;
149 reg TRADE *tp;
150 OWN *op;
151
152 for (i = 0; i < 2; i++) {
153 tp = &trades[i];
154 some = FALSE;
155 printf("Player %s (%d) gives:\n", play[tp->trader].name,
156 tp->trader+1);
157 if (tp->cash > 0)
158 printf("\t$%d\n", tp->cash), some++;
159 if (tp->gojf > 0)
160 printf("\t%d get-out-of-jail-free card(s)\n", tp->gojf),
161 some++;
162 if (tp->prop_list) {
163 for (op = tp->prop_list; op; op = op->next)
164 putchar('\t'), printsq(sqnum(op->sqr), TRUE);
165 some++;
166 }
167 if (!some)
168 printf("\t-- Nothing --\n");
169 }
170}
171/*
172 * This routine actually executes the trade.
173 */
174do_trade() {
175
176 move_em(&trades[0], &trades[1]);
177 move_em(&trades[1], &trades[0]);
178}
179/*
180 * This routine does a switch from one player to another
181 */
182move_em(from, to)
183TRADE *from, *to; {
184
185 reg PLAY *pl_fr, *pl_to;
186 reg OWN *op;
187
188 pl_fr = &play[from->trader];
189 pl_to = &play[to->trader];
190
191 pl_fr->money -= from->cash;
192 pl_to->money += from->cash;
193 pl_fr->num_gojf -= from->gojf;
194 pl_to->num_gojf += from->gojf;
195 for (op = from->prop_list; op; op = op->next) {
196 add_list(to->trader, &(pl_to->own_list), sqnum(op->sqr));
197 op->sqr->owner = to->trader;
198 del_list(from->trader, &(pl_fr->own_list), sqnum(op->sqr));
199 }
200 set_ownlist(to->trader);
201}
202/*
203 * This routine lets a player resign
204 */
205resign() {
206
207 reg int i, new_own;
208 reg OWN *op;
209 SQUARE *sqp;
210
211 if (cur_p->money <= 0) {
212 switch (board[cur_p->loc].type) {
213 case UTIL:
214 case RR:
215 case PRPTY:
216 new_own = board[cur_p->loc].owner;
217 break;
2497392a 218 default: /* Chance, taxes, etc */
3297b3e8
KB
219 new_own = num_play;
220 break;
221 }
222 if (new_own == num_play)
223 printf("You would resign to the bank\n");
224 else
225 printf("You would resign to %s\n", name_list[new_own]);
226 }
227 else if (num_play == 1) {
228 new_own = num_play;
229 printf("You would resign to the bank\n");
230 }
231 else {
232 name_list[num_play] = "bank";
233 do {
234 new_own = getinp("Who do you wish to resign to? ",
235 name_list);
236 if (new_own == player)
237 printf("You can't resign to yourself!!\n");
238 } while (new_own == player);
239 name_list[num_play] = "done";
240 }
241 if (getyn("Do you really want to resign? ", yn) != 0)
242 return;
243 if (num_play == 1) {
244 printf("Then NOBODY wins (not even YOU!)\n");
245 exit(0);
246 }
247 if (new_own < num_play) { /* resign to player */
248 printf("resigning to player\n");
249 trades[0].trader = new_own;
250 trades[0].cash = trades[0].gojf = 0;
251 trades[0].prop_list = NULL;
252 trades[1].trader = player;
253 trades[1].cash = cur_p->money > 0 ? cur_p->money : 0;
254 trades[1].gojf = cur_p->num_gojf;
255 trades[1].prop_list = cur_p->own_list;
256 do_trade();
257 }
258 else { /* resign to bank */
259 printf("resigning to bank\n");
260 for (op = cur_p->own_list; op; op = op->next) {
261 sqp = op->sqr;
262 sqp->owner = -1;
263 sqp->desc->morg = FALSE;
2497392a 264 if (sqp->type == PRPTY) {
3297b3e8
KB
265 isnot_monop(sqp->desc->mon_desc);
266 sqp->desc->houses = 0;
267 }
268 }
269 if (cur_p->num_gojf)
270 ret_card(cur_p);
271 }
272 for (i = player; i < num_play; i++) {
273 name_list[i] = name_list[i+1];
274 if (i + 1 < num_play)
275 cpy_st(&play[i], &play[i+1], sizeof (PLAY));
276 }
277 name_list[num_play--] = 0;
278 for (i = 0; i < N_SQRS; i++)
279 if (board[i].owner > player)
280 --board[i].owner;
281 player = --player < 0 ? num_play - 1 : player;
282 next_play();
283 if (num_play < 2) {
284 printf("\nThen %s WINS!!!!!\n", play[0].name);
285 printhold(0);
286 printf("That's a grand worth of $%d.\n",
287 play[0].money+prop_worth(&play[0]));
288 exit(0);
289 }
290}