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