trade.c 7.9 KB

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