/* card.c * * card_t decoding functions. * * Copyright (C) 2002 Andy Goth * Matt Harang * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ #include #include "spite.h" /* Value meaning, there is no card here. */ const card_t card_empty = {0}; /* Returns the card's suit. */ suit_t card_suit(card_t c) { if (!c.exists) return S_NO_CARD; if (!c.visible) return S_UNKNOWN; if (c.real_value == V_JOKER) return S_JOKER; return c.suit; } /* Returns the card's real value, the value stamped on the face. */ value_t card_real_value(card_t c) { if (!c.exists) return S_NO_CARD; if (!c.visible) return S_UNKNOWN; assert(c.real_value >= V_ACE && c.real_value <= V_JOKER); return c.real_value; } /* Returns the card's effective value, the value the card is being played as. */ value_t card_eff_value(card_t c) { if (!c.exists) return S_NO_CARD; if (!c.visible) return S_UNKNOWN; if (card_is_wild(c) && card_in_play(c)) { assert(c.eff_value >= V_ACE && c.eff_value <= V_QUEEN); return c.eff_value; } assert(c.real_value >= V_ACE && c.real_value <= V_JOKER); return c.real_value; } /* Returns true if the card is wild. */ bool card_is_wild(card_t c) { value_t v = card_real_value(c); return v == V_KING || v == V_JOKER; } /* Returns true if the card is on a shared discard pile. */ bool card_in_play(card_t c) { return c.exists && c.visible && c.in_play; } /* Can card 'top' be played on card 'bottom'? */ bool card_is_playable(card_t top, card_t bottom) { value_t t_value = card_real_value(top); value_t b_value = card_eff_value(bottom); assert(card_in_play(bottom)); assert(!card_in_play(top)); assert(b_value != V_QUEEN); /* Wilds can be played on blank spots and on nonwilds */ if (card_is_wild(top)) { if (b_value == V_QUEEN) return 0; if (b_value == V_NO_CARD) return 1; return !card_is_wild(bottom); } /* Ordinary cards can be played only in ascending order (an ace is one * greater than a blank space) */ return t_value == b_value + 1; } /* EOF */