From 50c9b42e6cc2fe234b971b4e514dc10cbd14c9e4 Mon Sep 17 00:00:00 2001 From: Paul Janzen Date: Sun, 28 Jul 2002 08:44:15 +0000 Subject: Tidy the code, follow the rules more closely (and document where we diverge), and make save files work. The last stops a number of segfaults. --- games/monop/houses.c | 108 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 95 insertions(+), 13 deletions(-) (limited to 'games/monop/houses.c') diff --git a/games/monop/houses.c b/games/monop/houses.c index 3b089ff3773..a033011e734 100644 --- a/games/monop/houses.c +++ b/games/monop/houses.c @@ -1,4 +1,4 @@ -/* $OpenBSD: houses.c,v 1.3 2002/02/16 21:27:10 millert Exp $ */ +/* $OpenBSD: houses.c,v 1.4 2002/07/28 08:44:14 pjanzen Exp $ */ /* $NetBSD: houses.c,v 1.3 1995/03/23 08:34:40 cgd Exp $ */ /* @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)houses.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$OpenBSD: houses.c,v 1.3 2002/02/16 21:27:10 millert Exp $"; +static const char rcsid[] = "$OpenBSD: houses.c,v 1.4 2002/07/28 08:44:14 pjanzen Exp $"; #endif #endif /* not lint */ @@ -52,6 +52,9 @@ static MON *monops[N_MON]; static void buy_h(MON *); static void sell_h(MON *); static void list_cur(MON *); +static int avail_houses(); +static int avail_hotels(); +static bool can_only_buy_hotel(MON *); /* * These routines deal with buying and selling houses @@ -62,7 +65,7 @@ buy_houses() int num_mon; MON *mp; OWN *op; - bool good,got_morg; + bool good, got_morg; int i,p; over: @@ -79,9 +82,9 @@ over: 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) @@ -118,11 +121,24 @@ buy_h(mnp) MON *mp; int price; shrt input[3],temp[3]; - int tot; + 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); @@ -136,7 +152,7 @@ over: 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; @@ -153,10 +169,27 @@ err: printf("That makes the spread too wide. Try again\n"); } 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); + 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++) @@ -237,10 +270,11 @@ over: 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) { @@ -258,7 +292,8 @@ err: printf("That makes the spread too wide. Try again\n"); 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); + 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++) @@ -283,3 +318,50 @@ list_cur(mp) } 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); +} -- cgit v1.2.3