/* This file is part of cardwords
   (c) 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_bonustable.hh

/* The CardWords_Bonustable contains the points that are additionaly added to
   the player's score when he lays a move or trades cards, depending on the
   number of cards involved. */

#ifndef CARDWORDS_BONUSTABLE_HH
#define CARDWORDS_BONUSTABLE_HH

#include "cardwords_iostream.hh"
#include "cardwords_exceptions.hh"

class CardWords_BonusTable {
public:
  // the contructor reads the boni from a file:
  CardWords_BonusTable(const string & filename);
  CardWords_BonusTable(istream & bonus);

  // No boni at all:
  CardWords_BonusTable();
  
  ~CardWords_BonusTable();

  // get the number of cards that a player has usually on the hand:
  size_t get_hand_capacity(void) const;

  // get the bonus for this many cards, or, if the index is negative,
  // get the add-on for trading -index cards:
  int operator [] (int) const;
private:
  size_t hand_capacity;
  int * table;
  int * origin;
};

inline ostream & operator << (ostream & o, const CardWords_BonusTable & b)
{
  int index;
  for (index = -(int)b.get_hand_capacity();
       index <= (int)b.get_hand_capacity();
       ++index) {
    o << index << ':' << b[index] << endl;
  }
  return o << endl;
}

inline 
CardWords_BonusTable::CardWords_BonusTable(const string & filename)
  : hand_capacity(0), table(0), origin(0)
{
  ifstream file(filename.c_str());
  if (file.bad()) {
    throw CardWords_XAccessingFile(gettext("Opening Bonus File"),
                                   filename);
  }
  int index, value, expected_index;
  char c;
  file >> index >> c >> value;
  if (c != ':' || file.bad()) {
    throw CardWords_XBadInput(gettext("Reading bonus file"),
                            gettext("first <int>:<int> pair"));
  }
  hand_capacity = -index;
  table = new int[2 * hand_capacity + 1];
  origin = table + hand_capacity;
  origin[index] = value;
  for (expected_index = index + 1;
       expected_index <= (int)hand_capacity;
       ++expected_index) {
    file >> index >> c >> value;
    if (c != ':' || file.bad() || index != expected_index) {
      delete [] table;
      table = 0;
      throw CardWords_XBadInput(gettext("Reading bonus file"),
                              gettext("valid subsequent <int>:<int> pair"));
    }
    origin[index] = value;
  }
}

inline 
CardWords_BonusTable::CardWords_BonusTable(istream & bonus)
  : hand_capacity(0), table(0), origin(0)
{
  int index, value, expected_index;
  char c;
  bonus >> index >> c >> value;
  if (c != ':' || bonus.bad()) {
    throw CardWords_XBadInput(gettext("Reading bonus definition"),
                            gettext("first <int>:<int> pair"));
  }
  hand_capacity = -index;
  table = new int[2 * hand_capacity + 1];
  origin = table + hand_capacity;
  origin[index] = value;
  for (expected_index = index + 1;
       expected_index <= (int)hand_capacity;
       ++expected_index) {
    bonus >> index >> c >> value;
    if (c != ':' || bonus.bad() || index != expected_index) {
      delete [] table;
      table = 0;
      throw CardWords_XBadInput(gettext("Reading bonus definition"),
                              gettext("valid subsequent <int>:<int> pair"));
    }
    origin[index] = value;
  }
}

inline 
CardWords_BonusTable::CardWords_BonusTable()
  : hand_capacity(0), table(0), origin(0)
{}

inline CardWords_BonusTable::~CardWords_BonusTable()
{
  delete [] table;
}

inline   size_t CardWords_BonusTable::get_hand_capacity(void) const
{
  return hand_capacity;
}
inline   int CardWords_BonusTable::operator[] (int i) const
{
  if (hand_capacity == 0) {
    return 0;
  }
  assert (i >= -(int)hand_capacity);
  assert (i <= (int)hand_capacity);
  return origin[i];
}
#endif

