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