+/* $NetBSD: houses.c,v 1.13 2008/02/24 01:57:34 dholland Exp $ */
+
/*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ * The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-static char sccsid[] = "@(#)houses.c 5.5 (Berkeley) 6/1/90";
+#if 0
+static char sccsid[] = "@(#)houses.c 8.1 (Berkeley) 5/31/93";
+#else
+__RCSID("$NetBSD: houses.c,v 1.13 2008/02/24 01:57:34 dholland Exp $");
+#endif
#endif /* not lint */
-# include "monop.ext"
+#include "monop.h"
-static char *names[N_MON+2],
- cur_prop[80];
+static const char *names[N_MON+2];
+static char cur_prop[80];
static MON *monops[N_MON];
+static void buy_h(MON *);
+static void sell_h(MON *);
+static void list_cur(MON *);
+static int avail_houses(void);
+static int avail_hotels(void);
+static bool can_only_buy_hotel(MON *);
+
/*
* These routines deal with buying and selling houses
*/
-buy_houses() {
-
- reg int num_mon;
- reg MON *mp;
- reg OWN *op;
- bool good,got_morg;
- int i,p;
+void
+buy_houses()
+{
+ int num_mon;
+ MON *mp;
+ OWN *op;
+ bool good, got_morg;
+ int i,p;
over:
num_mon = 0;
got_morg = good = FALSE;
for (i = 0; i < mp->num_in; i++) {
if (op->sqr->desc->morg)
- got_morg++;
+ got_morg = TRUE;
if (op->sqr->desc->houses != 5)
- good++;
+ good = TRUE;
op = op->next;
}
if (!good || got_morg)
else {
names[num_mon++] = "done";
names[num_mon--] = 0;
- if ((p=getinp("Which property do you wish to buy houses for? ", names)) == num_mon)
+ if ((p = getinp(
+ "Which property do you wish to buy houses for? ",
+ names)) == num_mon)
return;
buy_h(monops[p]);
goto over;
}
}
+static void
buy_h(mnp)
-MON *mnp; {
-
- reg int i;
- reg MON *mp;
- reg int price;
- shrt input[3],temp[3];
- int tot;
- PROP *pp;
+ MON *mnp;
+{
+ int i;
+ MON *mp;
+ int price;
+ short input[3],temp[3];
+ int tot, tot2;
+ PROP *pp;
+ int nhous, nhot;
+ bool chot;
mp = mnp;
price = mp->h_cost * 50;
+ nhous = avail_houses();
+ nhot = avail_hotels();
+ chot = can_only_buy_hotel(mnp);
+ if (nhous == 0 && !chot) {
+ printf("Building shortage: no houses available.");
+ return;
+ }
+ if (nhot == 0 && chot) {
+ printf("Building shortage: no hotels available.");
+ return;
+ }
blew_it:
list_cur(mp);
printf("Houses will cost $%d\n", price);
temp[i] = 5;
continue;
}
- (void)sprintf(cur_prop, "%s (%d): ",
+ (void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
mp->sq[i]->name, pp->houses);
input[i] = get_int(cur_prop);
temp[i] = input[i] + pp->houses;
- if (temp[i] > 5) {
+ if (temp[i] > 5 || temp[i] < 0) {
printf("That's too many. The most you can buy is %d\n",
- 5 - pp->houses);
+ 5 - pp->houses);
goto over;
}
}
}
else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
goto err;
- for (tot = i = 0; i < mp->num_in; i++)
- tot += input[i];
+ for (tot = tot2 = i = 0; i < mp->num_in; i++) {
+ if (temp[i] == 5)
+ tot2++;
+ else
+ tot += input[i];
+ }
+ if (tot > nhous) {
+ printf(
+"You have asked for %d house%s but only %d are available. Try again\n",
+ tot, tot == 1 ? "":"s", nhous);
+ goto blew_it;
+ } else if (tot2 > nhot) {
+ printf(
+"You have asked for %d hotel%s but only %d are available. Try again\n",
+ tot2, tot2 == 1 ? "":"s", nhot);
+ goto blew_it;
+ }
+
if (tot) {
- printf("You asked for %d houses for $%d\n", tot, tot * price);
- if (getyn("Is that ok? ", yn) == 0) {
+ printf("You asked for %d house%s and %d hotel%s for $%d\n", tot,
+ tot == 1 ? "" : "s", tot2, tot2 == 1 ? "" : "s", tot * price);
+ if (getyn("Is that ok? ") == 0) {
cur_p->money -= tot * price;
for (tot = i = 0; i < mp->num_in; i++)
mp->sq[i]->desc->houses = temp[i];
/*
* This routine sells houses.
*/
-sell_houses() {
-
- reg int num_mon;
- reg MON *mp;
- reg OWN *op;
- bool good;
- int p;
+void
+sell_houses()
+{
+ int num_mon;
+ MON *mp;
+ OWN *op;
+ bool good;
+ int p;
over:
num_mon = 0;
good = TRUE;
- for (op = cur_p->own_list; op; op = op->next)
+ for (op = cur_p->own_list; op;)
if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
mp = op->sqr->desc->mon_desc;
names[num_mon] = (monops[num_mon]=mp)->name;
good = 0;
do
if (!good && op->sqr->desc->houses != 0)
- good++;
+ good = TRUE;
while (op->next && op->sqr->desc->mon_desc == mp
- && (op=op->next));
+ && (op = op->next));
if (!good)
--num_mon;
- }
+ } else
+ op = op->next;
if (num_mon == 0) {
printf("You don't have any houses to sell!!\n");
return;
else {
names[num_mon++] = "done";
names[num_mon--] = 0;
- if ((p=getinp("Which property do you wish to sell houses from? ", names)) == num_mon)
+ if ((p = getinp(
+ "Which property do you wish to sell houses from? ",
+ names)) == num_mon)
return;
sell_h(monops[p]);
notify();
}
}
+static void
sell_h(mnp)
-MON *mnp; {
-
- reg int i;
- reg MON *mp;
- reg int price;
- shrt input[3],temp[3];
- int tot;
- PROP *pp;
+ MON *mnp;
+{
+ int i;
+ MON *mp;
+ int price;
+ short input[3],temp[3];
+ int tot;
+ PROP *pp;
mp = mnp;
price = mp->h_cost * 25;
continue;
}
if (pp->houses < 5)
- (void)sprintf(cur_prop,"%s (%d): ",
+ (void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
mp->sq[i]->name,pp->houses);
else
- (void)sprintf(cur_prop,"%s (H): ",mp->sq[i]->name);
+ (void)snprintf(cur_prop, sizeof(cur_prop), "%s (H): ",
+ mp->sq[i]->name);
input[i] = get_int(cur_prop);
temp[i] = pp->houses - input[i];
if (temp[i] < 0) {
- printf("That's too many. The most you can sell is %d\n", pp->houses);
+ printf(
+ "That's too many. The most you can sell is %d\n",
+ pp->houses);
goto over;
}
}
for (tot = i = 0; i < mp->num_in; i++)
tot += input[i];
if (tot) {
- printf("You asked to sell %d houses for $%d\n",tot,tot * price);
- if (getyn("Is that ok? ", yn) == 0) {
+ printf("You asked to sell %d house%s for $%d\n", tot,
+ tot == 1 ? "" : "s", tot * price);
+ if (getyn("Is that ok? ") == 0) {
cur_p->money += tot * price;
for (tot = i = 0; i < mp->num_in; i++)
mp->sq[i]->desc->houses = temp[i];
}
}
+static void
list_cur(mp)
-reg MON *mp; {
-
- reg int i;
- reg SQUARE *sqp;
+ MON *mp;
+{
+ int i;
+ SQUARE *sqp;
for (i = 0; i < mp->num_in; i++) {
sqp = mp->sq[i];
}
putchar('\n');
}
+
+static int
+avail_houses()
+{
+ int i, c;
+ SQUARE *sqp;
+
+ c = 0;
+ for (i = 0; i < N_SQRS; i++) {
+ sqp = &board[i];
+ if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
+ if (sqp->desc->houses < 5 && sqp->desc->houses > 0)
+ c += sqp->desc->houses;
+ }
+ }
+ return(N_HOUSE - c);
+}
+
+static int
+avail_hotels()
+{
+ int i, c;
+ SQUARE *sqp;
+
+ c = 0;
+ for (i = 0; i < N_SQRS; i++) {
+ sqp = &board[i];
+ if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
+ if (sqp->desc->houses == 5)
+ c++;
+ }
+ }
+ return(N_HOTEL - c);
+}
+
+static bool
+can_only_buy_hotel(mp)
+ MON *mp;
+{
+ int i;
+
+ for (i = 0; i < mp->num_in; i++) {
+ if (mp->sq[i]->desc->houses < 4)
+ return(FALSE);
+ }
+ return(TRUE);
+}