xref: /netbsd-src/games/monop/trade.c (revision c34afa686bf074a6dcc7f17157faae72111dde9a)
1*c34afa68Sdholland /*	$NetBSD: trade.c,v 1.16 2012/06/19 05:35:32 dholland Exp $	*/
242fb1b9dScgd 
361f28255Scgd /*
442fb1b9dScgd  * Copyright (c) 1980, 1993
542fb1b9dScgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32b118ad22Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3442fb1b9dScgd #if 0
3542fb1b9dScgd static char sccsid[] = "@(#)trade.c	8.1 (Berkeley) 5/31/93";
3642fb1b9dScgd #else
37*c34afa68Sdholland __RCSID("$NetBSD: trade.c,v 1.16 2012/06/19 05:35:32 dholland Exp $");
3842fb1b9dScgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
41b51259daSdholland #include "monop.h"
4261f28255Scgd 
4361f28255Scgd struct trd_st {			/* how much to give to other player	*/
4461f28255Scgd 	int	trader;			/* trader number		*/
4561f28255Scgd 	int	cash;			/* amount of cash 		*/
4661f28255Scgd 	int	gojf;			/* # get-out-of-jail-free cards	*/
4761f28255Scgd 	OWN	*prop_list;		/* property list		*/
4861f28255Scgd };
4961f28255Scgd 
5061f28255Scgd typedef	struct trd_st	TRADE;
5161f28255Scgd 
52092d3130Sjsm static const char	*plist[MAX_PRP+2];
5361f28255Scgd 
5461f28255Scgd static int	used[MAX_PRP];
5561f28255Scgd 
5661f28255Scgd static TRADE	trades[2];
5761f28255Scgd 
58cb5fd834Sjsm static void get_list(int, int );
59cb5fd834Sjsm static int set_list(OWN *);
60cb5fd834Sjsm static void summate(void);
61cb5fd834Sjsm static void do_trade(void);
62cb5fd834Sjsm static void move_em(TRADE *, TRADE *);
6361f28255Scgd 
64b118ad22Schristos void
trade(void)65*c34afa68Sdholland trade(void)
66b118ad22Schristos {
67b118ad22Schristos 	int tradee, i;
6861f28255Scgd 
6961f28255Scgd 	trading = TRUE;
7061f28255Scgd 	for (i = 0; i < 2; i++) {
7161f28255Scgd 		trades[i].cash = 0;
7261f28255Scgd 		trades[i].gojf = FALSE;
7361f28255Scgd 		trades[i].prop_list = NULL;
7461f28255Scgd 	}
7561f28255Scgd over:
7661f28255Scgd 	if (num_play == 1) {
7761f28255Scgd 		printf("There ain't no-one around to trade WITH!!\n");
7861f28255Scgd 		return;
7961f28255Scgd 	}
8061f28255Scgd 	if (num_play > 2) {
8161f28255Scgd 		tradee = getinp("Which player do you wish to trade with? ",
8261f28255Scgd 		    name_list);
8361f28255Scgd 		if (tradee == num_play)
8461f28255Scgd 			return;
8561f28255Scgd 		if (tradee == player) {
8661f28255Scgd 			printf("You can't trade with yourself!\n");
8761f28255Scgd 			goto over;
8861f28255Scgd 		}
8961f28255Scgd 	}
9061f28255Scgd 	else
9161f28255Scgd 		tradee = 1 - player;
9261f28255Scgd 	get_list(0, player);
9361f28255Scgd 	get_list(1, tradee);
9461f28255Scgd 	if (getyn("Do you wish a summary? ") == 0)
9561f28255Scgd 		summate();
9661f28255Scgd 	if (getyn("Is the trade ok? ") == 0)
9761f28255Scgd 		do_trade();
9861f28255Scgd }
99e42b2048Ssimonb 
10061f28255Scgd /*
10161f28255Scgd  *	This routine gets the list of things to be trader for the
10261f28255Scgd  * player, and puts in the structure given.
10361f28255Scgd  */
104b118ad22Schristos static void
get_list(int struct_no,int play_no)105*c34afa68Sdholland get_list(int struct_no, int play_no)
106b118ad22Schristos {
107b118ad22Schristos 	int sn, pn;
108b118ad22Schristos 	PLAY *pp;
1094e5cdff5Sdholland 	int numin, propnum, num_prp;
11061f28255Scgd 	OWN *op;
11161f28255Scgd 	TRADE *tp;
11261f28255Scgd 
11361f28255Scgd 	for (numin = 0; numin < MAX_PRP; numin++)
11461f28255Scgd 		used[numin] = FALSE;
11561f28255Scgd 	sn = struct_no, pn = play_no;
11661f28255Scgd 	pp = &play[pn];
11761f28255Scgd 	tp = &trades[sn];
11861f28255Scgd 	tp->trader = pn;
11961f28255Scgd 	printf("player %s (%d):\n", pp->name, pn+1);
12061f28255Scgd 	if (pp->own_list) {
12161f28255Scgd 		numin = set_list(pp->own_list);
12261f28255Scgd 		for (num_prp = numin; num_prp; ) {
1234e5cdff5Sdholland 			propnum=getinp("Which property do you wish to trade? ",
124b118ad22Schristos 			    plist);
1254e5cdff5Sdholland 			if (propnum == numin)
12661f28255Scgd 				break;
1274e5cdff5Sdholland 			else if (used[propnum])
12861f28255Scgd 				printf("You've already allocated that.\n");
12961f28255Scgd 			else {
13061f28255Scgd 				num_prp--;
1314e5cdff5Sdholland 				used[propnum] = TRUE;
1324e5cdff5Sdholland 				for (op = pp->own_list; propnum--; op = op->next)
13361f28255Scgd 					continue;
13461f28255Scgd 				add_list(pn, &(tp->prop_list), sqnum(op->sqr));
13561f28255Scgd 			}
13661f28255Scgd 		}
13761f28255Scgd 	}
13861f28255Scgd 	if (pp->money > 0) {
13961f28255Scgd 		printf("You have $%d.  ", pp->money);
14061f28255Scgd 		tp->cash = get_int("How much are you trading? ");
14161f28255Scgd 	}
14261f28255Scgd 	if (pp->num_gojf > 0) {
14361f28255Scgd once_more:
14461f28255Scgd 		printf("You have %d get-out-of-jail-free cards. ",pp->num_gojf);
14561f28255Scgd 		tp->gojf = get_int("How many are you trading? ");
14661f28255Scgd 		if (tp->gojf > pp->num_gojf) {
14761f28255Scgd 			printf("You don't have that many.  Try again.\n");
14861f28255Scgd 			goto once_more;
14961f28255Scgd 		}
15061f28255Scgd 	}
15161f28255Scgd }
152e42b2048Ssimonb 
15361f28255Scgd /*
15461f28255Scgd  *	This routine sets up the list of tradable property.
15561f28255Scgd  */
156b118ad22Schristos static int
set_list(OWN * the_list)157*c34afa68Sdholland set_list(OWN *the_list)
158b118ad22Schristos {
159b118ad22Schristos 	int i;
160b118ad22Schristos 	OWN *op;
16161f28255Scgd 
16261f28255Scgd 	i = 0;
16361f28255Scgd 	for (op = the_list; op; op = op->next)
16461f28255Scgd 		if (!used[i])
165b118ad22Schristos 			plist[i++] = op->sqr->name;
166b118ad22Schristos 	plist[i++] = "done";
167b118ad22Schristos 	plist[i--] = 0;
16861f28255Scgd 	return i;
16961f28255Scgd }
170e42b2048Ssimonb 
17161f28255Scgd /*
17261f28255Scgd  *	This routine summates the trade.
17361f28255Scgd  */
174b118ad22Schristos static void
summate(void)175*c34afa68Sdholland summate(void)
176b118ad22Schristos {
177b118ad22Schristos 	bool some;
178b118ad22Schristos 	int i;
179b118ad22Schristos 	TRADE *tp;
18061f28255Scgd 	OWN *op;
18161f28255Scgd 
18261f28255Scgd 	for (i = 0; i < 2; i++) {
18361f28255Scgd 		tp = &trades[i];
18461f28255Scgd 		some = FALSE;
18561f28255Scgd 		printf("Player %s (%d) gives:\n", play[tp->trader].name,
18661f28255Scgd 			tp->trader+1);
18761f28255Scgd 		if (tp->cash > 0)
18861f28255Scgd 			printf("\t$%d\n", tp->cash), some++;
18961f28255Scgd 		if (tp->gojf > 0)
19061f28255Scgd 			printf("\t%d get-out-of-jail-free card(s)\n", tp->gojf),
19161f28255Scgd 			some++;
19261f28255Scgd 		if (tp->prop_list) {
19361f28255Scgd 			for (op = tp->prop_list; op; op = op->next)
19461f28255Scgd 				putchar('\t'), printsq(sqnum(op->sqr), TRUE);
19561f28255Scgd 			some++;
19661f28255Scgd 		}
19761f28255Scgd 		if (!some)
19861f28255Scgd 			printf("\t-- Nothing --\n");
19961f28255Scgd 	}
20061f28255Scgd }
201e42b2048Ssimonb 
20261f28255Scgd /*
20361f28255Scgd  *	This routine actually executes the trade.
20461f28255Scgd  */
205b118ad22Schristos static void
do_trade(void)206*c34afa68Sdholland do_trade(void)
207b118ad22Schristos {
20861f28255Scgd 	move_em(&trades[0], &trades[1]);
20961f28255Scgd 	move_em(&trades[1], &trades[0]);
21061f28255Scgd }
211e42b2048Ssimonb 
21261f28255Scgd /*
21361f28255Scgd  *	This routine does a switch from one player to another
21461f28255Scgd  */
215b118ad22Schristos static void
move_em(TRADE * from,TRADE * to)216*c34afa68Sdholland move_em(TRADE *from, TRADE *to)
217b118ad22Schristos {
218b118ad22Schristos 	PLAY *pl_fr, *pl_to;
219b118ad22Schristos 	OWN *op;
22061f28255Scgd 
22161f28255Scgd 	pl_fr = &play[from->trader];
22261f28255Scgd 	pl_to = &play[to->trader];
22361f28255Scgd 
22461f28255Scgd 	pl_fr->money -= from->cash;
22561f28255Scgd 	pl_to->money += from->cash;
22661f28255Scgd 	pl_fr->num_gojf -= from->gojf;
22761f28255Scgd 	pl_to->num_gojf += from->gojf;
22861f28255Scgd 	for (op = from->prop_list; op; op = op->next) {
22961f28255Scgd 		add_list(to->trader, &(pl_to->own_list), sqnum(op->sqr));
23061f28255Scgd 		op->sqr->owner = to->trader;
23161f28255Scgd 		del_list(from->trader, &(pl_fr->own_list), sqnum(op->sqr));
23261f28255Scgd 	}
23361f28255Scgd 	set_ownlist(to->trader);
23461f28255Scgd }
235e42b2048Ssimonb 
23661f28255Scgd /*
23761f28255Scgd  *	This routine lets a player resign
23861f28255Scgd  */
239b118ad22Schristos void
resign(void)240*c34afa68Sdholland resign(void)
241b118ad22Schristos {
242b118ad22Schristos 	int i, new_own;
243b118ad22Schristos 	OWN *op;
24461f28255Scgd 	SQUARE *sqp;
24561f28255Scgd 
24661f28255Scgd 	if (cur_p->money <= 0) {
24761f28255Scgd 		switch (board[cur_p->loc].type) {
24861f28255Scgd 		  case UTIL:
24961f28255Scgd 		  case RR:
25061f28255Scgd 		  case PRPTY:
25161f28255Scgd 			new_own = board[cur_p->loc].owner;
25298e588beSdholland 			/* If you ran out of money by buying current location */
25398e588beSdholland 			if (new_own == player)
25498e588beSdholland 				new_own = num_play;
25561f28255Scgd 			break;
25661f28255Scgd 		  default:		/* Chance, taxes, etc */
25761f28255Scgd 			new_own = num_play;
25861f28255Scgd 			break;
25961f28255Scgd 		}
26061f28255Scgd 		if (new_own == num_play)
26161f28255Scgd 			printf("You would resign to the bank\n");
26261f28255Scgd 		else
26361f28255Scgd 			printf("You would resign to %s\n", name_list[new_own]);
26461f28255Scgd 	}
26561f28255Scgd 	else if (num_play == 1) {
26661f28255Scgd 		new_own = num_play;
26761f28255Scgd 		printf("You would resign to the bank\n");
26861f28255Scgd 	}
26961f28255Scgd 	else {
27061f28255Scgd 		name_list[num_play] = "bank";
27161f28255Scgd 		do {
27261f28255Scgd 			new_own = getinp("Who do you wish to resign to? ",
27361f28255Scgd 			    name_list);
27461f28255Scgd 			if (new_own == player)
27561f28255Scgd 				printf("You can't resign to yourself!!\n");
27661f28255Scgd 		} while (new_own == player);
27761f28255Scgd 		name_list[num_play] = "done";
27861f28255Scgd 	}
279b118ad22Schristos 	if (getyn("Do you really want to resign? ") != 0)
28061f28255Scgd 		return;
28161f28255Scgd 	if (num_play == 1) {
28261f28255Scgd 		printf("Then NOBODY wins (not even YOU!)\n");
28361f28255Scgd 		exit(0);
28461f28255Scgd 	}
28561f28255Scgd 	if (new_own < num_play) {	/* resign to player		*/
28661f28255Scgd 		printf("resigning to player\n");
28761f28255Scgd 		trades[0].trader = new_own;
28861f28255Scgd 		trades[0].cash = trades[0].gojf = 0;
28961f28255Scgd 		trades[0].prop_list = NULL;
29061f28255Scgd 		trades[1].trader = player;
29161f28255Scgd 		trades[1].cash = cur_p->money > 0 ? cur_p->money : 0;
29261f28255Scgd 		trades[1].gojf = cur_p->num_gojf;
29361f28255Scgd 		trades[1].prop_list = cur_p->own_list;
29461f28255Scgd 		do_trade();
29561f28255Scgd 	}
29661f28255Scgd 	else {				/* resign to bank		*/
29761f28255Scgd 		printf("resigning to bank\n");
29861f28255Scgd 		for (op = cur_p->own_list; op; op = op->next) {
29961f28255Scgd 			sqp = op->sqr;
30061f28255Scgd 			sqp->owner = -1;
30161f28255Scgd 			sqp->desc->morg = FALSE;
30261f28255Scgd 			if (sqp->type == PRPTY) {
303ce5670f7Sjsm 				is_not_monop(sqp->desc->mon_desc);
30461f28255Scgd 				sqp->desc->houses = 0;
30561f28255Scgd 			}
30661f28255Scgd 		}
30761f28255Scgd 		if (cur_p->num_gojf)
30861f28255Scgd 			ret_card(cur_p);
30961f28255Scgd 	}
31067021c2aSdholland 	free(play[player].name);
31161f28255Scgd 	for (i = player; i < num_play; i++) {
31261f28255Scgd 		name_list[i] = name_list[i+1];
31361f28255Scgd 		if (i + 1 < num_play)
314b118ad22Schristos 			play[i] = play[i+1];
31561f28255Scgd 	}
31698e588beSdholland 	name_list[num_play--] = NULL;
31761f28255Scgd 	for (i = 0; i < N_SQRS; i++)
31861f28255Scgd 		if (board[i].owner > player)
31961f28255Scgd 			--board[i].owner;
32098e588beSdholland 	player = player == 0 ? num_play - 1 : player - 1;
32161f28255Scgd 	next_play();
32261f28255Scgd 	if (num_play < 2) {
32361f28255Scgd 		printf("\nThen %s WINS!!!!!\n", play[0].name);
32461f28255Scgd 		printhold(0);
32561f28255Scgd 		printf("That's a grand worth of $%d.\n",
32661f28255Scgd 			play[0].money+prop_worth(&play[0]));
32761f28255Scgd 		exit(0);
32861f28255Scgd 	}
32961f28255Scgd }
330