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