/**********************************************************************
 *
 *   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 "sorted_hand.h"

#include "hand.h"

#include "../misc/setting.h"

/**
 **
 ** constructor
 **
 ** @param	hand	the corresponding hand
 **
 ** @return	-
 **
 ** @version	0.6.4
 **
 ** @author	Diether Knof
 **
 **/
SortedHand::SortedHand(Hand& hand) :
hand_(&hand),
  to_sorted_pos_(hand.cardsnumber_all()),
from_sorted_pos_(hand.cardsnumber_all()),
  mixed(::setting(Setting::CARDS_ORDER).mixed())
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_INIT,
		"SortedHand::SortedHand(hand)");

  for (unsigned i = 0; i < this->cardsnumber_all(); ++i)
    this->to_sorted_pos_.push_back(i);

  this->set_sorting();

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_INIT,
		  "SortedHand::SortedHand(hand)");
} // SortedHand::SortedHand(Hand& hand)

/**
 **
 ** constructor
 **
 ** @param	sorted_hand	the sorted hand to copy
 ** @param	hand		the corresponding hand
 **
 ** @return	-
 **
 ** @version	0.6.4
 **
 ** @author	Diether Knof
 **
 **/
SortedHand::SortedHand(SortedHand const& sorted_hand, Hand& hand) :
hand_(&hand),
  to_sorted_pos_(sorted_hand.to_sorted_pos_),
  from_sorted_pos_(sorted_hand.from_sorted_pos_),
  mixed(sorted_hand.mixed)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_INIT,
		"SortedHand::SortedHand(sorted_hand, hand)");

  DEBUG_ASSERTION((sorted_hand.hand() == hand),
		  "SortedHand::SortedHand(sorted_hand, hand):\n"
		  "  sorted_hand.hand() != hand");

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_INIT,
		  "SortedHand::SortedHand(sorted_hand, hand)");
} // SortedHand::SortedHand(SortedHand const& sorted_hand, Hand& hand) :

/**
 **
 ** setting equal
 **
 ** @param	sorted_hand	the sorted hand to copy
 **
 ** @return	-
 **
 ** @version	0.6.4
 **
 ** @author	Diether Knof
 **
 **/
SortedHand&
SortedHand::operator=(SortedHand const& sorted_hand)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_INIT,
		"SortedHand::operator=(sorted_hand)");

  this->hand_ = sorted_hand.hand_;
  this->to_sorted_pos_ = sorted_hand.to_sorted_pos_;
  this->from_sorted_pos_ = sorted_hand.from_sorted_pos_;
  this->mixed = sorted_hand.mixed;

  DEBUG_RETURNING(*this,
		  INFO_SORTED_HAND && INFO_INIT,
		  "SortedHand::operator=(sorted_hand)");
} // SortedHand& SortedHand::operator=(SortedHand const& sorted_hand) :

/**
 **
 ** destructor
 **
 ** @param	-
 **
 ** @return	-
 **
 ** @version	0.6.2
 **
 ** @author	Diether Knof
 **
 **/
SortedHand::~SortedHand()
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_INIT,
		"SortedHand::~SortedHand()");

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_INIT,
		  "SortedHand::~SortedHand()");
} // SortedHand::~SortedHand()

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	the corresponding hand
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.2
 **
 **/
Hand const&
SortedHand::hand() const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::hand()");

  DEBUG_RETURNING(*(this->hand_),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::hand()");
} // Hand const& SortedHand::hand() const

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	the corresponding hand
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
Hand&
SortedHand::hand()
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::hand()");

  DEBUG_RETURNING(*(this->hand_),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::hand()");
} // Hand& SortedHand::hand()

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	the number of (unplayed) cards
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
unsigned
SortedHand::cardsnumber() const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::cardsnumber()");

  DEBUG_RETURNING(this->hand().cardsnumber(),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::cardsnumber()");
} // unsigned SortedHand::cardsnumber() const

/**
 **
 ** -> result
 **
 ** @param	-
 **
 ** @return	the number of (all) cards
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
unsigned
SortedHand::cardsnumber_all() const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::cardsnumber_all()");

  DEBUG_RETURNING(this->hand().cardsnumber_all(),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::cardsnumber_all()");
} // unsigned SortedHand::cardsnumber_all() const

/**
 **
 ** -> result
 **
 ** @param	i	number of card
 **
 ** @return	card i of the hand (with remaining cards)
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.2
 **
 **/
Card const&
SortedHand::card(unsigned const i) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::card(i)");     

  DEBUG_RETURNING(this->hand().card(this->from_sorted_pos(i)),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::card(i)");
} // Card const& SortedHand::card(unsigned const i) const

/**
 **
 ** -> result
 **
 ** @param	i	cardnumber
 **
 ** @return	the card number 'i'
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.2
 **
 **/
Card const&
SortedHand::card_all(unsigned const i) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::card_all(i)");

  DEBUG_ASSERTION((i < this->cardsnumber_all()),
		  "SortedHand::card_all(i):\n"
		  "  illegal value " << i
		  << " (maximal value is: "
		  << this->cardsnumber_all() - 1 << ")");

  DEBUG_RETURNING(this->hand().card_all(this->from_sorted_pos_all(i)),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::card_all(i)");
} // Card const& SortedHand::card(unsigned const i) const

/**
 **
 ** -> result
 **
 ** @param	i	cardnumber
 **
 ** @return	whether the card 'i' has been played
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.2
 **
 **/
bool
SortedHand::played(unsigned const i) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::played(i)");

  DEBUG_ASSERTION((i < this->cardsnumber_all()),
		  "SortedHand::played(i):\n"
		  "  illegal value " << i
		  << " (maximal value is: "
		  << this->cardsnumber_all() - 1 << ")");

  DEBUG_RETURNING(this->hand().played(this->from_sorted_pos_all(i)),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::card_all(i)");
} // bool SortedHand::played(unsigned const i) const

/**
 **
 ** -> result
 **
 ** @param	i	number of card
 **
 ** @return	in which trick the card has been played
 **		(UINT_MAX, if not played, yet)
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.2
 **
 **/
unsigned
SortedHand::played_trick(unsigned const i) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::played_trick(unsigned const i)");

  DEBUG_ASSERTION((i < this->cardsnumber_all()),
		  "SortedHand::played_trick(i):\n"
		  "  illegal value " << i
		  << " (maximal value is: "
		  << this->cardsnumber_all() - 1 << ")");

  DEBUG_RETURNING(this->hand().played_trick(this->from_sorted_pos_all(i)),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::card_all(i)");
} // unsigned SortedHand::played_trick(i)

/**
 **
 ** -> result
 **
 ** @param	i	number of card
 **
 ** @return	real position of the card (in the played cards)
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
unsigned
SortedHand::from_sorted_pos(unsigned const i) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::from_sorted_pos(i)");

  DEBUG_ASSERTION((i < this->cardsnumber()),
		  "SortedHand::from_sorted_pos(i):\n"
		  "  illegal value " << i
		  << " (maximal value is: "
		  << this->cardsnumber() - 1 << ")");

  DEBUG_RETURNING(this->hand().pos_all_to_pos(this->from_sorted_pos_all(this->pos_to_pos_all(i))),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::from_sorted_pos(i)");
} // unsigned SortedHand::from_sorted_pos(unsigned const i) const

/**
 **
 ** -> result
 **
 ** @param	i	number of card
 **
 ** @return	real position of the card (in all cards)
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.2
 **
 **/
unsigned
SortedHand::from_sorted_pos_all(unsigned const i) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::from_sorted_pos_all(i)");

  DEBUG_ASSERTION((i < this->cardsnumber_all()),
		  "SortedHand::from_sorted_pos_all(i):\n"
		  "  illegal value " << i
		  << " (maximal value is: "
		  << this->cardsnumber_all() - 1 << ")");

  DEBUG_RETURNING(this->from_sorted_pos_[i],
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::from_sorted_pos_all(i)");
} // unsigned SortedHand::from_sorted_pos_all(unsigned const i) const

/**
 **
 ** -> result
 **
 ** @param	i	number of card
 **
 ** @return	translated position of the card (real pos is 'i')
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
unsigned
SortedHand::to_sorted_pos(unsigned const i) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::to_sorted_pos(i)");

  DEBUG_ASSERTION((i < this->cardsnumber()),
		  "SortedHand::to_sorted_pos(i):\n"
		  "  illegal value " << i
		  << " (maximal value is: "
		  << this->cardsnumber() - 1 << ")");

  DEBUG_RETURNING(this->hand().pos_all_to_pos(this->to_sorted_pos_all(this->pos_to_pos_all(i))),
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::to_sorted_pos(i)");
} // unsigned SortedHand::to_sorted_pos(unsigned const i) const

/**
 **
 ** -> result
 **
 ** @param	i	number of card
 **
 ** @return	translated position of the card (real pos is 'i')
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
unsigned
SortedHand::to_sorted_pos_all(unsigned const i) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::to_sorted_pos_all(i)");

  DEBUG_ASSERTION((i < this->cardsnumber_all()),
		  "SortedHand::to_sorted_pos_all(i):\n"
		  "  illegal value " << i
		  << " (maximal value is: "
		  << this->cardsnumber_all() - 1 << ")");

  DEBUG_RETURNING(this->to_sorted_pos_[i],
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::to_sorted_pos_all(i)");
} // unsigned SortedHand::to_sorted_pos_all(unsigned const i) const

/**
 **
 ** -> result
 ** The pos of the argument is the pos in the unplayed cards.
 ** It is searched, what position in all cards 'pos' stands for.
 **
 ** @param	pos	position in the unplayed card
 **
 ** @return	position the played cards
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
unsigned
SortedHand::pos_to_pos_all(unsigned pos) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::pos_to_pos_all(pos)");

  unsigned n;
  // translate the position
  for (n = 0, pos += 1; pos > 0; ++n)
    if (!this->hand().played(this->from_sorted_pos_all(n)))
      pos -= 1;

  DEBUG_RETURNING(n - 1,
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::pos_to_pos_all(pos)");
} // unsigned SortedHand::pos_to_pos_all(unsigned pos)

/**
 **
 ** -> result
 ** The pos of the argument is the pos in all cards.
 ** It is searched, what position in the remaining cards 'pos' stands for.
 **
 ** @param	pos	position in all card
 **
 ** @return	position in the unplayed cards
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
unsigned
SortedHand::pos_all_to_pos(unsigned pos) const
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::pos_all_to_pos(pos)");

  unsigned n;
  // translate the position
  for (n = 0, pos += 1; pos > 0; pos--)
    if (!this->hand().played(this->from_sorted_pos_all(pos - 1)))
      n++;

  DEBUG_RETURNING(n - 1,
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::pos_all_to_pos(pos)");
} // unsigned SortedHand::pos_all_to_pos(unsigned pos)

/**
 **
 ** -> result
 ** The pos of the argument is the pos in all cards.
 ** It is searched, what position in the remaining cards 'pos' stands for.
 **
 ** @param	pos	position of the card to request (in all cards)
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
void
SortedHand::request_position_all(unsigned const pos_all)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::request_position_all(pos_all)");

  this->hand().request_position_all(this->from_sorted_pos_all(pos_all));

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::request_position_all(pos_all)");
} // void SortedHand::request_position_all(unsigned const pos_all)

/**
 **
 ** marks card 'c' as played
 **
 ** @param	c	card to play
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
void
SortedHand::playcard(Card const& c)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_GAMEPLAY,
		"SortedHand::playcard(Card const& c)");

  this->hand().playcard(c);

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_GAMEPLAY,
		  "SortedHand::playcard(Card const& c)");
} // void SortedHand::playcard(Card const& c)

/**
 **
 ** marks card at 'pos' as played
 **
 ** @param	pos	position of the card to play
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
void
SortedHand::playcard(unsigned const pos)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_GAMEPLAY,
		"SortedHand::playcard(pos)");

  this->hand().playcard(this->from_sorted_pos(pos));

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_GAMEPLAY,
		  "SortedHand::playcard(pos)");
} // void SortedHand::playcard(unsigned const pos)

/**
 **
 ** marks a played 'card' as not played
 **
 ** @param	c card to unmark
 **
 ** @return	-
 **
 ** @version	0.5
 **
 ** @author	Diether Knof
 **
 **/
void
SortedHand::unplaycard(Card const& c)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_GAMEPLAY,
		"SortedHand::unplaycard(c)");

  this->hand().unplaycard(c);

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_GAMEPLAY,
		  "SortedHand::unplaycard(c)");
} // void SortedHand::unplaycard(Card const& c)

/**
 **
 ** adds the cards from 'hand' to the hand
 **
 ** @param	hand	hand to be added
 **
 ** @return	-
 **
 ** @version	0.6.4
 **
 ** @author	Diether Knof
 **
 **/
void
SortedHand::add(Hand const& hand)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_GAMEPLAY,
		"SortedHand::add(hand)");

  // here we know, that the cards are added at the end of the hand
  for (unsigned c = 0; c < hand.cardsnumber_all(); c++) {
    this->to_sorted_pos_.push_back(c + this->cardsnumber());
    this->from_sorted_pos_.push_back(c + this->cardsnumber());
  } // for (c < hand.cardsnumber_all())
  this->hand().add(hand);
  
  if (!this->mixed)
    this->sort();

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_GAMEPLAY,
		  "SortedHand::add(hand)");
} // void SortedHand::add(Hand const& hand)

/**
 **
 ** removes 'card' from the hand
 **
 ** @param	card	card to remove
 **
 ** @return	position of the removed card
 **
 ** @version	0.6.4
 **
 ** @author	Diether Knof
 **
 **/
unsigned
SortedHand::remove(Card const& card)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_GAMEPLAY,
		"SortedHand::remove(Card)");

  // the position in the hand of the removed card
  unsigned const pos_all = this->hand().getpos_all(card);
  // the position in the sorted hand of the removed card
  unsigned const sorted_pos_all = this->to_sorted_pos_all(pos_all);

  this->hand().remove(card);

  // update the sorting vectors
  //vector<unsigned> to_sorted_pos_;
  //vector<unsigned> from_sorted_pos_;

  this->from_sorted_pos_.erase(this->from_sorted_pos_.begin()
			       + sorted_pos_all);
  for (vector<unsigned>::iterator p = this->from_sorted_pos_.begin();
       p != this->from_sorted_pos_.end();
       ++p)
    if (*p > pos_all)
      *p -= 1;

  this->to_sorted_pos_.erase(this->to_sorted_pos_.begin() + pos_all);
  for (vector<unsigned>::iterator p = this->to_sorted_pos_.begin();
       p != this->to_sorted_pos_.end();
       ++p)
    if (*p > sorted_pos_all)
      *p -= 1;

  DEBUG_RETURNING(sorted_pos_all,
		  INFO_SORTED_HAND && INFO_GAMEPLAY,
		  "SortedHand::remove(Card)");
} // unsigned SortedHand::remove(Card const& card)

/**
 **
 ** removes the cards 'cards' from the hand
 **
 ** @param	cards	cards to be removeed
 **
 ** @return	-
 **
 ** @version	0.6.4
 **
 ** @author	Diether Knof
 **
 **/
void
SortedHand::remove(vector<Card> const& cards)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_GAMEPLAY,
		"SortedHand::remove(cards)");

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

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_GAMEPLAY,
		  "SortedHand::remove(cards)");
} // void SortedHand::remove(vector<Card> const& cards)

/**
 **
 ** the hand has been changed - update the sorting
 **
 ** @param	-
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
void
SortedHand::set_sorting()
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_VALUE,
		"SortedHand::set_sorting()");


  if (this->mixed)
    this->mix();
  else
    this->sort();

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_VALUE,
		  "SortedHand::set_sorting()");
} // void SortedHand::set_sorting()

/**
 **
 ** sorts the cards (as set in the settings)
 **
 ** @param	-
 **
 ** @return	whether the order has changed
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
bool
SortedHand::sort()
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_OTHER_FUNCTION,
		"SortedHand::sort()");

  this->from_sorted_pos_.clear();
  for (unsigned i = 0; i < this->cardsnumber_all(); ++i)
    this->from_sorted_pos_.push_back(i);

  bool changed = false;
  if (::setting(Setting::CARDS_ORDER).sorted()) {
    // a bad bubble sort, but we have at max 12 cards

    for (unsigned c1 = 0; c1 < this->cardsnumber_all(); ++c1)
      for (unsigned c2 = c1 + 1; c2 < this->cardsnumber_all(); ++c2)
	if (Card::relative_position(this->hand().card_all(this->from_sorted_pos_[c1]),
				    this->hand().card_all(this->from_sorted_pos_[c2]))
	    > 0) {
	  std::swap(this->from_sorted_pos_[c1], this->from_sorted_pos_[c2]);
	  changed = true;
	}

    this->set_positions();
  } // bubble sort over all cards

  this->mixed = false;

  DEBUG_RETURNING(changed,
		  INFO_SORTED_HAND && INFO_OTHER_FUNCTION,
		  "SortedHand::sort()");
} // changed SortedHand::sort()

/**
 **
 ** unsort the cards (make them the original order)
 **
 ** @param	-
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
void
SortedHand::unsort()
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_OTHER_FUNCTION,
		"SortedHand::unsort()");

  this->from_sorted_pos_.clear();
  for (unsigned i = 0; i < this->cardsnumber_all(); ++i)
    this->from_sorted_pos_.push_back(i);

  this->set_positions();

  this->mixed = true;

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_OTHER_FUNCTION,
		  "SortedHand::unsort()");
} // void SortedHand::unsort()

/**
 **
 ** mix the (order of the) cards
 **
 ** @param	-
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
void
SortedHand::mix()
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_OTHER_FUNCTION,
		"SortedHand::mix()");

  if (!this->mixed) {
    this->unsort();
  } else { // if !(!this->mixed)
    this->from_sorted_pos_.clear();
    for (unsigned i = 0; i < this->cardsnumber_all(); ++i)
      this->from_sorted_pos_.push_back(i);
    if (::setting(Setting::CARDS_ORDER).mixed())
      for (unsigned c = 0; c < this->cardsnumber_all(); c++)
	swap(this->from_sorted_pos_[c], this->from_sorted_pos_[RAND(c)]);

    this->set_positions();
  } // if !(!this->mixed)

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_OTHER_FUNCTION,
		  "SortedHand::mix()");
} // void SortedHand::mix()

/**
 **
 ** calculate the position of the cards from the hand
 **
 ** @param	-
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.4
 **
 **/
void
SortedHand::set_positions()
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_OTHER_FUNCTION,
		"SortedHand::set_positions()");

  this->to_sorted_pos_.resize(this->from_sorted_pos_.size());

  for (unsigned c = 0; c < this->cardsnumber_all(); c++)
    this->to_sorted_pos_[this->from_sorted_pos_[c]] = c;

  DEBUG_RETURNING(VOID,
		  INFO_SORTED_HAND && INFO_OTHER_FUNCTION,
		  "SortedHand::set_positions()");
} // void SortedHand::set_positions()

/**
 **
 ** writes the (sorted) hand in 'ostr'
 **
 ** @param	ostr		stream to write to
 ** @param	sorted_hand	hand to write
 **
 ** @return	output stream
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.2
 **
 **/
ostream&
operator<<(ostream& ostr, SortedHand const& sorted_hand)
{
  DEBUG_CALLING(INFO_SORTED_HAND && INFO_IO,
		"operator<<(ostr, hand)");

  for (unsigned i = 0; i < sorted_hand.cardsnumber_all(); i++) {
    ostr << hex << i << dec << "  " << sorted_hand.card_all(i);
    if (sorted_hand.played(i))
      ostr << "\t(played " << setw(2) << sorted_hand.played_trick(i) << ")";
    ostr << "\n";
  } // for (i < hand.cardsnumber_all())

  DEBUG_RETURNING(ostr,
		  INFO_SORTED_HAND && INFO_IO,
		  "operator<<(ostr, hand)");
} // ostream& operator<<(ostream& ostr, SortedHand const& sorted_hand)
