2005-02-05 12:14:56 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Copyright (C) 1999-2005 Eliot
|
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
|
|
|
|
* Olivier Teuliere <ipkiss@via.ecp.fr>
|
|
|
|
*
|
|
|
|
* 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
|
2005-10-23 16:53:42 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-02-05 12:14:56 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2005-03-27 23:45:04 +02:00
|
|
|
#include <string>
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "tile.h"
|
|
|
|
#include "bag.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
|
|
|
Bag::Bag()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Bag::init()
|
|
|
|
{
|
|
|
|
m_ntiles = 0;
|
|
|
|
const list<Tile>& allTiles = Tile::getAllTiles();
|
|
|
|
list<Tile>::const_iterator it;
|
|
|
|
for (it = allTiles.begin(); it != allTiles.end(); it++)
|
|
|
|
{
|
|
|
|
m_tilesMap[*it] = it->maxNumber();
|
|
|
|
m_ntiles += it->maxNumber();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-09 16:58:24 +02:00
|
|
|
unsigned int Bag::in(const Tile &iTile) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
map<Tile, int>::const_iterator it = m_tilesMap.find(iTile);
|
|
|
|
if (it != m_tilesMap.end())
|
|
|
|
return (*it).second;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-09 16:58:24 +02:00
|
|
|
unsigned int Bag::nVowels() const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
map<Tile, int>::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-09 16:58:24 +02:00
|
|
|
unsigned int Bag::nConsonants() const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
map<Tile, int>::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-27 23:45:04 +02:00
|
|
|
void Bag::takeTile(const Tile &iTile)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-03-27 23:45:04 +02:00
|
|
|
ASSERT(in(iTile),
|
2006-01-22 13:23:52 +01:00
|
|
|
(wstring(L"The bag does not contain the letter ") + iTile.toChar()).c_str());
|
2005-03-27 23:45:04 +02:00
|
|
|
|
|
|
|
m_tilesMap[iTile]--;
|
|
|
|
m_ntiles--;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-27 23:45:04 +02:00
|
|
|
void Bag::replaceTile(const Tile &iTile)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-03-27 23:45:04 +02:00
|
|
|
ASSERT(in(iTile) < iTile.maxNumber(),
|
2006-01-22 13:23:52 +01:00
|
|
|
(wstring(L"Cannot replace tile: ") + iTile.toChar()).c_str());
|
2005-03-27 23:45:04 +02:00
|
|
|
|
|
|
|
m_tilesMap[iTile]++;
|
|
|
|
m_ntiles++;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Tile Bag::selectRandom()
|
|
|
|
{
|
|
|
|
map<Tile, int>::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;
|
|
|
|
}
|
2005-03-27 23:45:04 +02:00
|
|
|
ASSERT(false, "We should not come here");
|
2005-02-05 12:14:56 +01:00
|
|
|
return Tile::dummy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Bag::operator=(const Bag &iOther)
|
|
|
|
{
|
|
|
|
m_tilesMap = iOther.m_tilesMap;
|
|
|
|
m_ntiles = iOther.m_ntiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Bag::dumpAll() const
|
|
|
|
{
|
|
|
|
map<Tile, int>::const_iterator it;
|
|
|
|
for (it = m_tilesMap.begin(); it != m_tilesMap.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second)
|
2006-12-23 14:50:43 +01:00
|
|
|
fprintf(stderr, "%lc[%i] ", it->first.toChar(), it->second);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2006-01-01 20:49:35 +01:00
|
|
|
|
|
|
|
/// Local Variables:
|
|
|
|
/// mode: c++
|
|
|
|
/// mode: hs-minor
|
|
|
|
/// c-basic-offset: 4
|
|
|
|
/// indent-tabs-mode: nil
|
|
|
|
/// End:
|