diff options
Diffstat (limited to 'monop')
-rw-r--r-- | monop/Makefile | 17 | ||||
-rw-r--r-- | monop/cards.c | 224 | ||||
-rw-r--r-- | monop/deck.h | 7 | ||||
-rw-r--r-- | monop/execute.c | 19 | ||||
-rw-r--r-- | monop/initdeck.c | 276 | ||||
-rw-r--r-- | monop/pathnames.h | 34 |
6 files changed, 186 insertions, 391 deletions
diff --git a/monop/Makefile b/monop/Makefile index 2a40fc1a..45eea382 100644 --- a/monop/Makefile +++ b/monop/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.29 2008/02/24 01:41:14 dholland Exp $ +# $NetBSD: Makefile,v 1.30 2008/02/24 02:43:18 dholland Exp $ # @(#)Makefile 8.1 (Berkeley) 5/31/93 .include <bsd.own.mk> @@ -8,20 +8,5 @@ SRCS= monop.c cards.c execute.c getinp.c houses.c jail.c misc.c \ morg.c print.c prop.c rent.c roll.c spec.c trade.c MAN= monop.6 HIDEGAME=hidegame -CLEANFILES+=initdeck initdeck.lo cards.pck -.if ${MKSHARE} != "no" -FILES=cards.pck -FILESDIR=/usr/share/games -.endif - -realall: ${FILES} - -initdeck: initdeck.lo - ${_MKTARGET_LINK} - ${HOST_LINK.c} -o ${.TARGET} ${.ALLSRC} - -cards.pck: initdeck - ${_MKTARGET_CREATE} - ./initdeck ${.CURDIR}/cards.inp .include <bsd.prog.mk> diff --git a/monop/cards.c b/monop/cards.c index a085412b..0494122b 100644 --- a/monop/cards.c +++ b/monop/cards.c @@ -1,4 +1,4 @@ -/* $NetBSD: cards.c,v 1.21 2008/02/24 01:57:34 dholland Exp $ */ +/* $NetBSD: cards.c,v 1.22 2008/02/24 02:43:18 dholland Exp $ */ /* * Copyright (c) 1980, 1993 @@ -34,7 +34,7 @@ #if 0 static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: cards.c,v 1.21 2008/02/24 01:57:34 dholland Exp $"); +__RCSID("$NetBSD: cards.c,v 1.22 2008/02/24 02:43:18 dholland Exp $"); #endif #endif /* not lint */ @@ -42,47 +42,160 @@ __RCSID("$NetBSD: cards.c,v 1.21 2008/02/24 01:57:34 dholland Exp $"); #include <sys/endian.h> #include "monop.h" #include "deck.h" -#include "pathnames.h" /* * These routine deal with the card decks */ +static void set_up(DECK *); +static void printmes(const char *text); + #define GOJF 'F' /* char for get-out-of-jail-free cards */ -#ifndef DEV -static const char *cardfile = _PATH_CARDS; -#else -static const char *cardfile = "cards.pck"; -#endif +struct cardinfo { + const char *actioncode; + const char *text; +}; -static FILE *deckf; +static const struct cardinfo cc_cards[] = { + { "FF", + ">> GET OUT OF JAIL FREE <<\n" + "Keep this card until needed or sold\n" + }, + { "++25", + "Receive for Services $25.\n" + }, + { "++200", + "Bank Error in Your Favor.\n" + "Collect $200.\n" + }, + { "++20", + "Income Tax Refund.\n" + "Collect $20.\n" + }, + { "--100", + "Pay Hospital $100\n" + }, + { "++100", + "Life Insurance Matures.\n" + "Collect $100\n" + }, + { "++45", + "From sale of Stock You get $45.\n" + }, + { "TX", + "You are Assessed for street repairs.\n" + "\t$40 per House\n" + "\t$115 per Hotel\n" + }, + { "++100", + "X-mas Fund Matures.\n" + "Collect $100.\n" + }, + { "++11", + "You have won Second Prize in a Beauty Contest\n" + "Collect $11\n" + }, + { "MF0", + "Advance to GO\n" + "(Collect $200)\n" + }, + { "++100", + "You inherit $100\n" + }, + { "--150", + "Pay School Tax of $150.\n" + }, + { "MJ", + "\t\t>> GO TO JAIL <<\n" + "Go Directly to Jail. Do not pass GO Do not collect $200.\n" + }, + { "+A50", + "\t\t>> GRAND OPERA OPENING <<\n" + "Collect $50 from each player for opening night seats.\n" + }, + { "--50", + "Doctor's Fee: Pay $50.\n" + } +}; -static void set_up(DECK *); -static void printmes(void); +static const struct cardinfo ch_cards[] = { + { "FF", + ">> GET OUT OF JAIL FREE <<\n" + "Keep this card until needed or sold\n" + }, + { "MR", + "Advance to the nearest Railroad, and pay owner\n" + "Twice the rental to which he is otherwise entitled.\n" + "If Railroad is unowned you may buy it from the bank\n" + }, + { "MU", + "Advance to the nearest Utility.\n" + "If unowned, you may buy it from the bank.\n" + "If owned, throw dice and pay oner a total of ten times\n" + "the amount thrown.\n" + }, + { "MB3", + "Go Back 3 Spaces\n" + }, + { "MR", + "Advance to the nearest Railroad, and pay owner\n" + "Twice the rental to which he is otherwise entitled.\n" + "If Railroad is unowned you may buy it from the bank\n" + }, + { "MJ", + " >> GO DIRECTLY TO JAIL <<\n" + "Do not pass GO, Do not Collect $200.\n" + }, + { "MF5", + "Take a Ride on the Reading.\n" + "If you pass GO, collect $200.\n" + }, + { "MF39", + "Take a Walk on the Board Walk.\n" + " (Advance To Board Walk)\n" + }, + { "MF24", + "Advance to Illinois Ave.\n" + }, + { "MF0", + "Advance to Go\n" + }, + { "MF11", + "Advance to St. Charles Place.\n" + "If you pass GO, collect $200.\n" + }, + { "TX", + "Make general repairs on all of your Property.\n" + "For Each House pay $25.\n" + "For Each Hotel pay $100.\n" + }, + { "-A50", + "You have been elected Chairman of the Board.\n" + "Pay each player $50.\n" + }, + { "--15", + "Pay Poor Tax of $15\n" + }, + { "++50", + "Bank pays you Dividend of $50.\n" + }, + { "++150", + "Your Building and Loan Matures.\n" + "Collect $150.\n" + } +}; /* - * This routine initializes the decks from the data file, - * which it opens. + * This routine initializes the decks from the data above. */ void init_decks() { - int32_t nc; - - if ((deckf = fopen(cardfile, "r")) == NULL) { -file_err: - err(1, "%s", cardfile); - } - - /* read number of community chest cards... */ - if (fread(&nc, sizeof(nc), 1, deckf) != 1) - goto file_err; - CC_D.num_cards = be32toh(nc); - /* ... and number of community chest cards. */ - if (fread(&nc, sizeof(nc), 1, deckf) != 1) - goto file_err; - CH_D.num_cards = be32toh(nc); + CC_D.info = cc_cards; + CC_D.num_cards = sizeof(cc_cards) / sizeof(cc_cards[0]); + CH_D.info = ch_cards; + CH_D.num_cards = sizeof(ch_cards) / sizeof(ch_cards[0]); set_up(&CC_D); set_up(&CH_D); } @@ -97,26 +210,24 @@ set_up(dp) int r1, r2; int i; - dp->offsets = (off_t *) calloc(dp->num_cards, sizeof (off_t)); - if (dp->offsets == NULL) + dp->cards = calloc(dp->num_cards, sizeof(dp->cards[0])); + if (dp->cards == NULL) errx(1, "out of memory"); - if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) != - (unsigned) dp->num_cards) { - err(1, "%s", cardfile); - } - /* convert offsets from big-endian byte order */ + for (i = 0; i < dp->num_cards; i++) - BE64TOH(dp->offsets[i]); + dp->cards[i] = i; + dp->top_card = 0; dp->gojf_used = FALSE; + for (i = 0; i < dp->num_cards; i++) { - off_t temp; + int temp; r1 = roll(1, dp->num_cards) - 1; r2 = roll(1, dp->num_cards) - 1; - temp = dp->offsets[r2]; - dp->offsets[r2] = dp->offsets[r1]; - dp->offsets[r1] = temp; + temp = dp->cards[r2]; + dp->cards[r2] = dp->cards[r1]; + dp->cards[r1] = temp; } } @@ -131,15 +242,17 @@ get_card(dp) int num; int i, per_h, per_H, num_h, num_H; OWN *op; + const struct cardinfo *thiscard; do { - fseek(deckf, dp->offsets[dp->top_card], SEEK_SET); + thiscard = &dp->info[dp->top_card]; + type_maj = thiscard->actioncode[0]; dp->top_card = ++(dp->top_card) % dp->num_cards; - type_maj = getc(deckf); } while (dp->gojf_used && type_maj == GOJF); - type_min = getc(deckf); - num = ntohl(getw(deckf)); - printmes(); + type_min = thiscard->actioncode[1]; + num = atoi(thiscard->actioncode+2); + + printmes(thiscard->text); switch (type_maj) { case '+': /* get money */ if (type_min == 'A') { @@ -227,14 +340,14 @@ get_card(dp) * This routine prints out the message on the card */ static void -printmes() +printmes(const char *text) { - char c; + int i; printline(); fflush(stdout); - while ((c = getc(deckf)) != '\0') - putchar(c); + for (i = 0; text[i] != '\0'; i++) + putchar(text[i]); printline(); fflush(stdout); } @@ -270,19 +383,18 @@ ret_card(plr) gojfpos = dp->top_card; do { gojfpos = (gojfpos + 1) % dp->num_cards; - fseek(deckf, dp->offsets[gojfpos], SEEK_SET); - type_maj = getc(deckf); + type_maj = dp->info[gojfpos].actioncode[0]; } while (type_maj != GOJF); - temp = dp->offsets[gojfpos]; + temp = dp->cards[gojfpos]; /* Only one of the next two loops does anything */ for (i = gojfpos - 1; i > last_card; i--) - dp->offsets[i + 1] = dp->offsets[i]; + dp->cards[i + 1] = dp->cards[i]; for (i = gojfpos; i < last_card; i++) - dp->offsets[i] = dp->offsets[i + 1]; + dp->cards[i] = dp->cards[i + 1]; if (gojfpos > last_card) { - dp->offsets[dp->top_card] = temp; + dp->cards[dp->top_card] = temp; dp->top_card++; dp->top_card %= dp->num_cards; } else - dp->offsets[last_card] = temp; + dp->cards[last_card] = temp; } diff --git a/monop/deck.h b/monop/deck.h index 7275d1cc..7e3f1915 100644 --- a/monop/deck.h +++ b/monop/deck.h @@ -1,4 +1,4 @@ -/* $NetBSD: deck.h,v 1.8 2008/02/24 01:57:34 dholland Exp $ */ +/* $NetBSD: deck.h,v 1.9 2008/02/24 02:43:18 dholland Exp $ */ /* * Copyright (c) 1980, 1993 @@ -35,6 +35,8 @@ #define bool char +struct cardinfo; /* private to cards.c */ + #define CC_D deck[0] #define CH_D deck[1] @@ -42,7 +44,8 @@ struct dk_st { /* deck description structure */ int num_cards; /* number of cards in deck */ int top_card; /* number of last card picked */ bool gojf_used; /* set if gojf card out of deck */ - off_t *offsets; /* offsets for start of cards */ + int *cards; /* which cards (indexes info[]) */ + const struct cardinfo *info; /* the static card data */ }; typedef struct dk_st DECK; diff --git a/monop/execute.c b/monop/execute.c index 0f7036a6..e3231c21 100644 --- a/monop/execute.c +++ b/monop/execute.c @@ -1,4 +1,4 @@ -/* $NetBSD: execute.c,v 1.17 2008/02/24 01:57:34 dholland Exp $ */ +/* $NetBSD: execute.c,v 1.18 2008/02/24 02:43:18 dholland Exp $ */ /* * Copyright (c) 1980, 1993 @@ -34,7 +34,7 @@ #if 0 static char sccsid[] = "@(#)execute.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: execute.c,v 1.17 2008/02/24 01:57:34 dholland Exp $"); +__RCSID("$NetBSD: execute.c,v 1.18 2008/02/24 02:43:18 dholland Exp $"); #endif #endif /* not lint */ @@ -297,9 +297,9 @@ save() fprintf(outf, " numcards %d\n", deck[i].num_cards); fprintf(outf, " topcard %d\n", deck[i].top_card); fprintf(outf, " gojf_used %d\n", deck[i].gojf_used); - fprintf(outf, " offsets"); + fprintf(outf, " cards"); for (j = 0; j < deck[i].num_cards; j++) - fprintf(outf, " %ld", (long)(deck[i].offsets[j])); + fprintf(outf, " %d", deck[i].cards[j]); fprintf(outf, "\n"); fprintf(outf, "}\n"); } @@ -647,14 +647,19 @@ restore_deck_attr(const char *attribute, char *txt) return -1; } dp->gojf_used = tmp; - } else if (!strcmp(attribute, "offsets")) { + } else if (!strcmp(attribute, "cards")) { errno = 0; s = txt; for (j = 0; j<dp->num_cards; j++) { - dp->offsets[j] = strtol(s, &s, 10); + tmp = strtol(s, &s, 10); + if (tmp < 0 || tmp >= dp->num_cards) { + printf("cards: out of range value\n"); + return -1; + } + dp->cards[j] = tmp; } if (errno) { - printf("offsets: invalid values\n"); + printf("cards: invalid values\n"); return -1; } } else { diff --git a/monop/initdeck.c b/monop/initdeck.c deleted file mode 100644 index 894ad835..00000000 --- a/monop/initdeck.c +++ /dev/null @@ -1,276 +0,0 @@ -/* $NetBSD: initdeck.c,v 1.16 2008/02/19 08:07:51 dholland Exp $ */ - -/* - * 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 - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 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. 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. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifdef __NetBSD__ -#include <sys/cdefs.h> -#ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ - The Regents of the University of California. All rights reserved.\n"); -#endif /* not lint */ - -#ifndef lint -#if 0 -static char sccsid[] = "@(#)initdeck.c 8.1 (Berkeley) 5/31/93"; -#else -__RCSID("$NetBSD: initdeck.c,v 1.16 2008/02/19 08:07:51 dholland Exp $"); -#endif -#endif /* not lint */ -#endif /* __NetBSD__ */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/types.h> -#include "deck.h" - -#ifndef u_int32_t -#define u_int32_t unsigned int -#endif - -u_int32_t -h2nl(u_int32_t h) -{ - unsigned char c[4]; - u_int32_t rv; - - c[0] = (h >> 24) & 0xff; - c[1] = (h >> 16) & 0xff; - c[2] = (h >> 8) & 0xff; - c[3] = (h >> 0) & 0xff; - memcpy(&rv, c, sizeof rv); - - return (rv); -} - -/* - * This program initializes the card files for monopoly. - * It reads in a data file with Com. Chest cards, followed by - * the Chance card. The two are separated by a line of "%-". - * All other cards are separated by lines of "%%". In the front - * of the file is the data for the decks in the same order. - * This includes the seek pointer for the start of each card. - * All cards start with their execution code, followed by the - * string to print, terminated with a null byte. - */ - -#define TRUE 1 -#define FALSE 0 - -#define bool char - -const char *infile = "cards.inp", /* input file */ - *outfile = "cards.pck"; /* "packed" file */ - -DECK deck[2]; - -FILE *inf, *outf; - -/* initdeck.c */ -int main(int, char *[]); -static void getargs(int, char *[]); -static void fwrite_be_offt(off_t, FILE *); -static void count(void); -static void putem(void); - -int -main(ac, av) - int ac; - char *av[]; -{ - int i, nc; - - /* sanity test */ - if (sizeof(int) != 4) { - fprintf(stderr, "sizeof(int) != 4\n"); - exit(1); - } - - getargs(ac, av); - if ((inf = fopen(infile, "r")) == NULL) { - perror(infile); - exit(1); - } - count(); - /* - * allocate space for pointers. - */ - CC_D.offsets = (off_t *)calloc(CC_D.num_cards + 1, /* sizeof (off_t) */ 8); - CH_D.offsets = (off_t *)calloc(CH_D.num_cards + 1, /* sizeof (off_t) */ 8); - if (CC_D.offsets == NULL || CH_D.offsets == NULL) { - fprintf(stderr, "out of memory\n"); - exit(1); - } - fseek(inf, 0L, SEEK_SET); - if ((outf = fopen(outfile, "w")) == NULL) { - perror(outfile); - exit(1); - } - - /* - * these fields will be overwritten after the offsets are calculated, - * so byte-order doesn't matter yet. - */ - fwrite(&nc, sizeof(nc), 1, outf); - fwrite(&nc, sizeof(nc), 1, outf); - fwrite(CC_D.offsets, /* sizeof (off_t) */ 8, CC_D.num_cards, outf); - fwrite(CH_D.offsets, /* sizeof (off_t) */ 8, CH_D.num_cards, outf); - - /* - * write out the cards themselves (calculating the offsets). - */ - putem(); - - fclose(inf); - fseek(outf, 0, SEEK_SET); - - /* number of community chest cards first... */ - nc = h2nl(CC_D.num_cards); - fwrite(&nc, sizeof(nc), 1, outf); - /* ... then number of chance cards. */ - nc = h2nl(CH_D.num_cards); - fwrite(&nc, sizeof(nc), 1, outf); - - /* dump offsets in big-endian byte order */ - for (i = 0; i < CC_D.num_cards; i++) - fwrite_be_offt(CC_D.offsets[i], outf); - for (i = 0; i < CH_D.num_cards; i++) - fwrite_be_offt(CH_D.offsets[i], outf); - - fflush(outf); - if (ferror(outf)) { - perror(outfile); - exit(1); - } - fclose(outf); - printf("There were %d com. chest and %d chance cards\n", - CC_D.num_cards, CH_D.num_cards); - exit(0); -} - -static void -getargs(ac, av) - int ac; - char *av[]; -{ - if (ac > 1) - infile = av[1]; - if (ac > 2) - outfile = av[2]; -} - -/* - * count the cards - */ -static void -count() -{ - bool newline; - DECK *in_deck; - int c; - - newline = TRUE; - in_deck = &CC_D; - while ((c=getc(inf)) != EOF) - if (newline && c == '%') { - newline = FALSE; - in_deck->num_cards++; - if (getc(inf) == '-') - in_deck = &CH_D; - } - else - newline = (c == '\n'); - in_deck->num_cards++; -} - -/* - * put strings in the file - */ -static void -putem() -{ - bool newline; - DECK *in_deck; - int c; - int num; - - in_deck = &CC_D; - CC_D.num_cards = 1; - CH_D.num_cards = 0; - CC_D.offsets[0] = ftell(outf); - putc(getc(inf), outf); - putc(getc(inf), outf); - for (num = 0; (c=getc(inf)) != '\n'; ) - num = num * 10 + (c - '0'); - putw(h2nl(num), outf); - newline = FALSE; - while ((c=getc(inf)) != EOF) - if (newline && c == '%') { - putc('\0', outf); - newline = FALSE; - if (getc(inf) == '-') - in_deck = &CH_D; - while (getc(inf) != '\n') - continue; - in_deck->offsets[in_deck->num_cards++] = ftell(outf); - if ((c=getc(inf)) == EOF) - break; - putc(c, outf); - putc(c = getc(inf), outf); - for (num = 0; (c=getc(inf)) != EOF && c != '\n'; ) - num = num * 10 + (c - '0'); - putw(h2nl(num), outf); - } - else { - putc(c, outf); - newline = (c == '\n'); - } - putc('\0', outf); -} - -/* - * fwrite_be_offt: - * Write out the off parameter as a 64 bit big endian number - */ - -static void -fwrite_be_offt(off, f) - off_t off; - FILE *f; -{ - int i; - unsigned char c[8]; - - for (i = 7; i >= 0; i--) { - c[i] = off & 0xff; - off >>= 8; - } - fwrite(c, sizeof(c), 1, f); -} diff --git a/monop/pathnames.h b/monop/pathnames.h deleted file mode 100644 index 2b8f9129..00000000 --- a/monop/pathnames.h +++ /dev/null @@ -1,34 +0,0 @@ -/* $NetBSD: pathnames.h,v 1.4 2003/08/07 09:37:29 agc Exp $ */ - -/*- - * Copyright (c) 1990, 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 - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 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. 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. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * @(#)pathnames.h 8.1 (Berkeley) 5/31/93 - */ - -#define _PATH_CARDS "/usr/share/games/cards.pck" |