/***************************************************************************** * Copyright (C) 1999-2005 Eliot * Authors: Antoine Fraboulet * Olivier Teuliere * * $Id: bag.cpp,v 1.1 2005/02/05 11:14:56 ipkiss Exp $ * * 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 *****************************************************************************/ #include "tile.h" #include "bag.h" #include "debug.h" Bag::Bag() { init(); } void Bag::init() { m_ntiles = 0; const list& allTiles = Tile::getAllTiles(); list::const_iterator it; for (it = allTiles.begin(); it != allTiles.end(); it++) { m_tilesMap[*it] = it->maxNumber(); m_ntiles += it->maxNumber(); } } int Bag::in(const Tile &iTile) const { map::const_iterator it = m_tilesMap.find(iTile); if (it != m_tilesMap.end()) return (*it).second; return 0; } int Bag::nVowels() const { map::const_iterator it; int v = 0; for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++) { if (it->first.isVowel()) v += it->second; } return v; } int Bag::nConsonants() const { map::const_iterator it; int c = 0; for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++) { if (it->first.isConsonant()) c += it->second; } return c; } int Bag::takeTile(const Tile &iTile) { if (in(iTile)) { m_tilesMap[iTile]--; m_ntiles--; } else { return 1; } return 0; } int Bag::replaceTile(const Tile &iTile) { if (in(iTile) < iTile.maxNumber()) { m_tilesMap[iTile]++; m_ntiles++; } else { return 1; } return 0; } Tile Bag::selectRandom() { map::const_iterator it; int n; double max = m_ntiles; n = (int)(max * rand() / (RAND_MAX + 1.0)); for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++) { if (n < it->second) return it->first; n -= it->second; } PDEBUG(1, "BAG: we should not come here\n"); return Tile::dummy(); } void Bag::operator=(const Bag &iOther) { m_tilesMap = iOther.m_tilesMap; m_ntiles = iOther.m_ntiles; } void Bag::dumpAll() const { map::const_iterator it; for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++) { if (it->second) fprintf(stderr, "%c[%i] ", it->first.toChar(), it->second); } fprintf(stderr, "\n"); }