bag.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 1999-2005 Eliot
00003  * Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
00004  *          Olivier Teuliere  <ipkiss@via.ecp.fr>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  *****************************************************************************/
00020 
00021 #include <string>
00022 
00023 #include "tile.h"
00024 #include "bag.h"
00025 #include "debug.h"
00026 
00027 
00028 Bag::Bag()
00029 {
00030     init();
00031 }
00032 
00033 
00034 void Bag::init()
00035 {
00036     m_ntiles = 0;
00037     const list<Tile>& allTiles = Tile::getAllTiles();
00038     list<Tile>::const_iterator it;
00039     for (it = allTiles.begin(); it != allTiles.end(); it++)
00040     {
00041         m_tilesMap[*it] = it->maxNumber();
00042         m_ntiles += it->maxNumber();
00043     }
00044 }
00045 
00046 
00047 unsigned int Bag::in(const Tile &iTile) const
00048 {
00049     map<Tile, int>::const_iterator it = m_tilesMap.find(iTile);
00050     if (it != m_tilesMap.end())
00051         return (*it).second;
00052     return 0;
00053 }
00054 
00055 
00056 unsigned int Bag::nVowels() const
00057 {
00058     map<Tile, int>::const_iterator it;
00059     int v = 0;
00060 
00061     for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
00062     {
00063         if (it->first.isVowel())
00064             v += it->second;
00065     }
00066     return v;
00067 }
00068 
00069 
00070 unsigned int Bag::nConsonants() const
00071 {
00072     map<Tile, int>::const_iterator it;
00073     int c = 0;
00074 
00075     for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
00076     {
00077         if (it->first.isConsonant())
00078             c += it->second;
00079     }
00080     return c;
00081 }
00082 
00083 
00084 void Bag::takeTile(const Tile &iTile)
00085 {
00086     ASSERT(in(iTile),
00087            string("The bag does not contain the letter ") + iTile.toChar());
00088 
00089     m_tilesMap[iTile]--;
00090     m_ntiles--;
00091 }
00092 
00093 
00094 void Bag::replaceTile(const Tile &iTile)
00095 {
00096     ASSERT(in(iTile) < iTile.maxNumber(),
00097            string("Cannot replace tile: ") + iTile.toChar());
00098 
00099     m_tilesMap[iTile]++;
00100     m_ntiles++;
00101 }
00102 
00103 
00104 Tile Bag::selectRandom()
00105 {
00106     map<Tile, int>::const_iterator it;
00107     int n;
00108     double max = m_ntiles;
00109 
00110     n = (int)(max * rand() / (RAND_MAX + 1.0));
00111     for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
00112     {
00113         if (n < it->second)
00114             return it->first;
00115         n -= it->second;
00116     }
00117     ASSERT(false, "We should not come here");
00118     return Tile::dummy();
00119 }
00120 
00121 
00122 void Bag::operator=(const Bag &iOther)
00123 {
00124     m_tilesMap = iOther.m_tilesMap;
00125     m_ntiles = iOther.m_ntiles;
00126 }
00127 
00128 
00129 void Bag::dumpAll() const
00130 {
00131     map<Tile, int>::const_iterator it;
00132     for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
00133     {
00134         if (it->second)
00135             fprintf(stderr, "%c[%i] ", it->first.toChar(), it->second);
00136     }
00137     fprintf(stderr, "\n");
00138 }
00139 
00140 /// Local Variables:
00141 /// mode: c++
00142 /// mode: hs-minor
00143 /// c-basic-offset: 4
00144 /// indent-tabs-mode: nil
00145 /// End:

Generated on Thu Dec 29 02:01:14 2005 for Eliot by  doxygen 1.4.5