/* This file is part of cardwords
   (c) 1998 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   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
*/

// cardwords_cardbase.hh

/* The format used for input and output of the classes
   CardWords_Card and CardWords_CardDescription:

   [x #]

   [ and ] are literal brackets
   x       is a Machine_Char that can be converted to a CardWords_Char
   .       This is the face value of the card. For wildcards, this
   .       must be something will be converted to the same as
   .       CardWords_Char::blanko.
   Space   is either a literal space or a Machine_Char that can be
   .       converted to a CardWords_Char. Use Space for non-wildcards
   .       (though the same as x is also possible) and a desired
   .       meaning for wildcards.
   #       an int in decimal, the points of that card.
*/

#ifndef CARDWORDS_CARDBASE_HH
#define CARDWORDS_CARDBASE_HH

#include "cardwords_char.hh"

class CardWords_CardBase {
public:

  // the test if this card is a wildcard:
  virtual
  bool
  is_wildcard(void) const = 0;

  // Get the char that is written on this card:
  virtual 
  const CardWords_Char &
  get_faceValue(void) const = 0;

  // Get the char that is represented by this card;
  // can be different from the above for wildcards (and only for them):
  const CardWords_Char &
  get_meaning(void) const;

  // Get the score for this card:
  virtual
  int
  get_points(void) const = 0;

  // Change the meaning of this card. Only allowed for wildcards:
  void set_meaning (CardWords_Char);

  CardWords_CardBase( CardWords_Char means);
  CardWords_CardBase( char means);
  
  // read the card definition from a stream:
  CardWords_CardBase( istream & );

  bool operator == (const CardWords_CardBase &) const;
  // for sorting:
  bool operator < (const CardWords_CardBase & other) const;
  bool operator > (const CardWords_CardBase & other) const;
private:
  // bool wildcard and CardWords_Char faceValue are only defined in the
  // derived classes, because they will be (assignable in
  // CardWords_CardDescription) and (a constant in CardWords_Card).
  CardWords_Char meaning; // The char that is represented by this card;
  // can be different from the faceValue for wildcards (and only for them).
};

class ostream & operator << (class ostream &, const CardWords_CardBase &);

inline
class ostream & operator << (class ostream & ostream,
                             const CardWords_CardBase *card)
{
  return ostream << *card;
}

// Get the char that is represented by this card;
// can be different from faceValue for wildcards (and only for them):
inline
const CardWords_Char &
CardWords_CardBase::get_meaning(void) const
{
  if (is_wildcard() == true || get_faceValue() == CardWords_Char::blanko)
    return meaning;
  return get_faceValue();
}

// Change the meaning of this card. Only allowed for wildcards:
inline
void CardWords_CardBase::set_meaning (CardWords_Char newMeaning)
{
  if (is_wildcard() == true) {
    meaning = newMeaning;
  }
}

inline bool
CardWords_CardBase::operator == (const CardWords_CardBase & other) const
{
  // This == operator's intention is to compare an instance of class
  // CardWords_CardDescription and an instance of class CardWords_Card.
  // From the players this cardwords server will get only descriptions
  // (CardWords_CardDescription) which of their cards they intend to lay
  // on the table. The server has to find the matching card on their
  // handes. For this purpose, the meaning of wildcards is completely
  // unimportant. It is therefor not counted here.
  return (get_points() == other.get_points() &&
          is_wildcard() == other.is_wildcard() &&
          get_faceValue() == other.get_faceValue());
}

// The < operator is intended for sorting purposes:
inline bool
CardWords_CardBase::operator < (const CardWords_CardBase & other) const
{
  if (get_faceValue() < other.get_faceValue()) return true;
  if (other.get_faceValue() < get_faceValue()) return false;
  if (get_points() < other.get_points()) return true;
  return false;
}

inline bool
CardWords_CardBase::operator > (const CardWords_CardBase & other) const
{
  if (get_faceValue() < other.get_faceValue()) return false;
  if (other.get_faceValue() < get_faceValue()) return true;
  if (get_points() > other.get_points()) return true;
  return false;
}

inline
CardWords_CardBase::CardWords_CardBase (CardWords_Char means)
  : meaning   (means)
{}

inline
CardWords_CardBase::CardWords_CardBase (char means)
  : meaning   (means)
{}

#endif
