/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2004  by Diether Knof and Borg Enders
 *
 *   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 can find this license in the file 'gpl.txt'.
 *
 *   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
 *
 *  Contact:
 *    Diether Knof dknof@gmx.de
 *    Borg Enders  borg@borgsoft.de
 *
 *********************************************************************/


#include "constants.h"
#include "hand.h"

/**
 **
 ** -> result
 **
 ** @param	value	value to count
 **
 ** @return	number of cards with the value 'value'
 **
 ** @author	Borg Enders
 ** @author	Diether Knof
 **
 ** @version	0.6.7
 **
 **/
unsigned
Hand::numberof(Card::Value const& value) const
{
  unsigned number = 0;

  for (vector<Card>::const_iterator c = this->cards().begin();
       c != this->cards().end();
       ++c)
    if (c->value() == value)
      number += 1;

  return number;
} // unsigned Hand::numberof(Card::Value const& value)

/**
 **
 ** -> result
 **
 ** @param	tcolor	trump-color to count
 **
 ** @return	number of cards with the trump-color 'tcolor'
 **
 ** @author	Borg Enders
 ** @author	Diether Knof
 **
 ** @version	0.6.7
 **
 **/
unsigned
Hand::numberof(Card::TColor const& tcolor) const
{
  unsigned number = 0;

  for (vector<Card>::const_iterator c = this->cards().begin();
       c != this->cards().end();
       ++c)
    if (c->tcolor() == tcolor)
      number += 1;

  return number;
} // unsigned Hand::numberof(Card::TColor const& tcolor)

/**
 **
 ** -> result
 **
 ** @param	card	card to count
 **
 ** @return	number of cards 'card'
 **
 ** @author	Borg Enders
 ** @author	Diether Knof
 **
 ** @version	0.6.7
 **
 **/
unsigned
Hand::numberof(Card const& card) const
{
  unsigned number = 0;

  for (vector<Card>::const_iterator c = this->cards().begin();
       c != this->cards().end();
       ++c)
    if (*c == card)
      number += 1;

  return number;
} // unsigned Hand::numberof(Card const& card) const

/**
 **
 ** -> result
 ** Note: 'Hand::numberof(Card)' does not cover the case (TRUMP, ACE)
 **
 ** @param	tcolor	trump-color of the card
 ** @param	value	value of the card
 **
 ** @return	number of cards with trump-color 'tcolor' and value 'value'
 **
 ** @author	Borg Enders
 ** @author	Diether Knof
 **
 ** @version	0.6.7
 **
 **/
unsigned
Hand::numberof(Card::TColor const& tcolor, Card::Value const& value) const
{
  unsigned number = 0;

  for (vector<Card>::const_iterator c = this->cards().begin();
       c != this->cards().end();
       ++c)
    if ( (c->tcolor() == tcolor)
	 && (c->value() == value) )
      number += 1;

  return number;
} // unsigned Hand::numberof(Card::TColor, Card::Value)

/**
 **
 ** -> result
 ** Note: 'Hand::numberof(Card)' does not cover the case (TRUMP, ACE)
 **
 ** @param	color	color to count
 ** @param	gt	game type
 **
 ** @return	number of cards with color 'color' given the gametype 'gt'
 **
 ** @author	Borg Enders
 ** @author	Diether Knof
 **
 ** @version	0.6.7
 **
 **/
unsigned
Hand::numberof(Card::Color const& color, GameType const& gt) const
{
  unsigned number = 0;

  for (vector<Card>::const_iterator c = this->cards().begin();
       c != this->cards().end();
       ++c)
    if ((c->color() == color)
	&& !c->istrump(gt))
      number += 1;

  return number;
} // unsigned Hand::numberof(Card::Color const& color, GameType const& gt) const

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	number of trumps on the hand
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.6
 **
 **/
unsigned
Hand::numberoftrumps() const
{
  return this->numberof(Card::TRUMP);
} // unsigned Hand::numberoftrumps() const

/**
 **
 ** -> result
 **
 ** @param	tcolor	card tcolor
 ** @param	value	card value
 **
 ** @return	number of 'Card(tcolor, value)' on the hand with all cards
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.6
 **
 **/
unsigned
Hand::numberofall(Card::TColor const& tcolor, Card::Value const& value) const
{
  DEBUG_CALLING(INFO_HAND && INFO_VALUE,
		"Hand::numberof(tcolor, value)");

  unsigned res=0;
  for (unsigned i = 0; i < this->cardsnumber_all(); i++)
    if ((this->card_all(i).tcolor() == tcolor)
	&& (this->card_all(i).value() == value))
      ++res;

  DEBUG_RETURNING(res,
		  INFO_HAND && INFO_VALUE,
		  "Hand::numberofall(tcolor, value)");
} // unsigned Hand::numberofall(Card::TColor const& tcolor, Card::Value const& value) const

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	number of trump aces on the hand with all cards
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.6
 **
 **/
unsigned
Hand::numberofalltrumpaces() const
{
  return this->numberofall(Card::TRUMP, Card::ACE);
} // unsigned Hand::numberofalltrumpaces() const

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	number of trump kings on the hand
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.4
 **
 **/
unsigned
Hand::numberoftrumpkings() const
{
  return this->numberof(Card::TRUMP, Card::KING);
} // unsigned Hand::numberoftrumpkings() const

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	number of nines on the hand
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.4
 **
 **/
unsigned
Hand::numberofnines() const
{
  return this->numberof(Card::NINE);
} // unsigned Hand::numbernines()

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	number of kings on the hand
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.4
 **
 **/
unsigned
Hand::numberofkings() const
{
  return this->numberof(Card::KING);
} // unsigned Hand::numberkings()

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	number of club queens on the hand
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.4
 **
 **/
unsigned
Hand::numberofclubqueens() const
{
  return this->numberof(Card::CLUB_QUEEN);
} // unsigned Hand::numberofclubqueens()

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	number of trump aces on the hand
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.4
 **
 **/
unsigned
Hand::numberoftrumpaces() const
{
  return this->numberof(Card::TRUMP, Card::ACE);
} // unsigned Hand::numberoftrumpaces() const

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	number of trump nines on the hand
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.4
 **
 **/
unsigned
Hand::numberoftrumpnines() const
{
  return this->numberof(Card::TRUMP, Card::NINE);
} // unsigned Hand::numberoftrumpnines() const

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	whether there is a trump still on the hand
 **
 ** @author	Borg Enders
 ** @author	Diether Knof
 **
 ** @version	0.6.7
 **
 **/
bool
Hand::hastrump() const
{
  for (vector<Card>::const_iterator c = this->cards().begin();
       c != this->cards().end();
       ++c)
    if (c->istrump())
      return true;

  return false;
} // bool Hand::hastrump()

/**
 **
 ** -> result
 **
 ** @param	tcolor	trump-color to search for
 **
 ** @return	whether there is a card with trump-color 'tcolor' on the hand
 **
 ** @author	Borg Enders
 ** @author	Diether Knof
 **
 ** @version	0.6.7
 **
 **/
bool
Hand::existstcolor(Card::TColor const& tcolor) const
{
  for (vector<Card>::const_iterator c = this->cards().begin();
       c != this->cards().end();
       ++c)
    if (c->tcolor() == tcolor)
      return true;

  return false;
} // bool Hand::existstcolor(Card::TColor const& tcolor)
