]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - monop/cards.c
A first stab at supporting the housing shortage rules. From OpenBSD.
[bsdgames-darwin.git] / monop / cards.c
index db31fb5b651c09382244c3454c8560cd0fd038fa..ca27b4634ad4b8c7894f98109ccc32d95c2a2a7d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: cards.c,v 1.13 2003/08/07 09:37:27 agc Exp $   */
+/*     $NetBSD: cards.c,v 1.20 2008/02/23 19:54:06 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.13 2003/08/07 09:37:27 agc Exp $");
+__RCSID("$NetBSD: cards.c,v 1.20 2008/02/23 19:54:06 dholland Exp $");
 #endif
 #endif /* not lint */
 
@@ -57,8 +57,8 @@ static const char     *cardfile       = "cards.pck";
 
 static FILE    *deckf;
 
-static void set_up __P((DECK *));
-static void printmes __P((void));
+static void set_up(DECK *);
+static void printmes(void);
 
 /*
  *     This routine initializes the decks from the data file,
@@ -69,10 +69,9 @@ init_decks()
 {
        int32_t nc;
 
-       if ((deckf=fopen(cardfile, "r")) == NULL) {
+       if ((deckf = fopen(cardfile, "r")) == NULL) {
 file_err:
-               perror(cardfile);
-               exit(1);
+               err(1, "%s", cardfile);
        }
 
        /* read number of community chest cards... */
@@ -101,14 +100,13 @@ set_up(dp)
        if (dp->offsets == NULL)
                errx(1, "out of memory");
        if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
-           dp->num_cards) {
-               perror(cardfile);
-               exit(1);
+           (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->last_card = 0;
+       dp->top_card = 0;
        dp->gojf_used = FALSE;
        for (i = 0; i < dp->num_cards; i++) {
                off_t   temp;
@@ -134,8 +132,8 @@ get_card(dp)
        OWN *op;
 
        do {
-               fseek(deckf, dp->offsets[dp->last_card], SEEK_SET);
-               dp->last_card = ++(dp->last_card) % dp->num_cards;
+               fseek(deckf, dp->offsets[dp->top_card], SEEK_SET);
+               dp->top_card = ++(dp->top_card) % dp->num_cards;
                type_maj = getc(deckf);
        } while (dp->gojf_used && type_maj == GOJF);
        type_min = getc(deckf);
@@ -239,3 +237,51 @@ printmes()
        printline();
        fflush(stdout);
 }
+
+/*
+ *     This routine returns the players get-out-of-jail-free card
+ * to the bottom of a deck.  XXX currently does not return to the correct
+ * deck.
+ */
+void
+ret_card(plr)
+       PLAY *plr;
+{
+       char type_maj;
+       int gojfpos, last_card;
+       int i;
+       DECK *dp;
+       off_t temp;
+
+       plr->num_gojf--;
+       if (CC_D.gojf_used)
+               dp = &CC_D;
+       else
+               dp = &CH_D;
+       dp->gojf_used = FALSE;
+
+       /* Put at bottom of deck (top_card - 1) and remove it from wherever else
+        * it used to be.
+        */
+       last_card = dp->top_card - 1;
+       if (last_card < 0)
+               last_card += dp->num_cards;
+       gojfpos = dp->top_card;
+       do {
+               gojfpos = (gojfpos + 1) % dp->num_cards;
+               fseek(deckf, dp->offsets[gojfpos], SEEK_SET);
+               type_maj = getc(deckf);
+       } while (type_maj != GOJF);
+       temp = dp->offsets[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];
+       for (i = gojfpos; i < last_card; i++)
+               dp->offsets[i] = dp->offsets[i + 1];
+       if (gojfpos > last_card) {
+               dp->offsets[dp->top_card] = temp;
+               dp->top_card++;
+               dp->top_card %= dp->num_cards;
+       } else
+               dp->offsets[last_card] = temp;
+}