/* 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_cardcontainer2dim.cc

#include "cardwords_cardcontainer2dim.hh"
#include "cardwords_cardcontainer1dim.hh"

// remove a special card from the container:
void CardWords_CardContainer2Dim::remove_card (CardWords_Card *parameter_card)
{
  assert (parameter_card != 0);
  assert (parameter_card->get_container() == this);
  assert (size() > 0);
  
  iterator iter = find(parameter_card);

  assert (iter != end());
  assert ((*this)[iter->second] == parameter_card);

  parameter_card->set_container(0);
  (*this)[iter->second] = 0;
  erase(iter);
}

// add a card to the container:
void CardWords_CardContainer2Dim::add_card (CardWords_Card* to_be_added,
					  CardWords_CardTableLocation loc)
{
  assert (loc.second >= 0 && loc.first >= 0 &&
	  loc.first < width && loc.second < height);
  assert (to_be_added != 0);
  if (to_be_added->get_container() != 0) {
    to_be_added->get_container()->remove_card(to_be_added);
  }
  assert ((*this)[loc] == 0);

  (*this)[loc] = to_be_added;

  to_be_added->set_container(this);
  insert (value_type(to_be_added,loc));
}

// remove all cards and put them into pile:
void
CardWords_CardContainer2Dim::clear(class CardWords_CardContainer1Dim *pile)
{
  while (!empty()) {
    pile->add_card (begin()->first);
  }
}

CardWords_CardContainer2Dim::CardWords_CardContainer2Dim
 (size_t set_width, size_t set_height)
  : width (set_width),
    height (set_height),
    cardPointers (set_width, set_height, static_cast<CardWords_Card *>(0) )
{}


// the destructor automatically deletes all contained cards:
CardWords_CardContainer2Dim::~CardWords_CardContainer2Dim ()
{
  iterator iter;
  for (iter = begin(); iter != end(); ++iter) {
    assert (iter->first != 0);
    delete iter->first;
  }
}
