-/* $NetBSD: cards.c,v 1.11 1999/12/30 01:40:08 simonb Exp $ */
+/* $NetBSD: cards.c,v 1.20 2008/02/23 19:54:06 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
* 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.
*
#if 0
static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: cards.c,v 1.11 1999/12/30 01:40:08 simonb Exp $");
+__RCSID("$NetBSD: cards.c,v 1.20 2008/02/23 19:54:06 dholland Exp $");
#endif
#endif /* not lint */
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,
{
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... */
int r1, r2;
int i;
- dp->offsets = (off_t *) calloc(sizeof (off_t), dp->num_cards);
+ dp->offsets = (off_t *) calloc(dp->num_cards, sizeof (off_t));
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;
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);
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;
+}