eliot/game/rack.cpp

90 lines
2.3 KiB
C++
Raw Permalink Normal View History

/*****************************************************************************
* Eliot
* Copyright (C) 2002-2012 Antoine Fraboulet & Olivier Teulière
* Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
* Olivier Teulière <ipkiss @@ gmail.com>
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
#include "rack.h"
#include "dic.h"
#include "encoding.h"
#include "debug.h"
2012-02-18 22:26:52 +01:00
INIT_LOGGER(game, Rack);
Rack::Rack()
2013-01-17 17:52:42 +01:00
: m_tiles(Dictionary::GetDic().getTileNumber() + 1, 0), m_nbTiles(0)
{
}
void Rack::remove(const Tile &t)
{
2013-01-17 17:55:00 +01:00
ASSERT(count(t),
"The rack does not contain the letter " + lfw(t.getDisplayStr()));
m_tiles[t.toCode()]--;
2013-01-17 17:52:42 +01:00
m_nbTiles--;
}
void Rack::clear()
{
2013-01-17 17:52:42 +01:00
for (unsigned i = 0; i < m_tiles.size(); i++)
{
m_tiles[i] = 0;
}
2013-01-17 17:52:42 +01:00
m_nbTiles = 0;
}
void Rack::getTiles(vector<Tile> &oTiles) const
{
2013-01-17 17:52:42 +01:00
oTiles.reserve(m_nbTiles);
for (unsigned i = 1; i < m_tiles.size(); i++)
{
// Add m_tiles[i] copies of the tile at the end of the vector
oTiles.insert(oTiles.end(), m_tiles[i], Dictionary::GetDic().getTileFromCode(i));
}
}
2005-12-26 23:56:40 +01:00
wstring Rack::toString() const
2005-12-26 22:08:08 +01:00
{
wstring rs;
2013-01-17 17:52:42 +01:00
for (unsigned i = 1; i < m_tiles.size(); i++)
2005-12-26 22:08:08 +01:00
{
// Append m_tiles[i] copies of the char
const wstring &chr = Dictionary::GetDic().getTileFromCode(i).getDisplayStr();
2013-01-17 17:52:42 +01:00
for (unsigned j = 0; j < m_tiles[i]; ++j)
{
rs += chr;
}
2005-12-26 22:08:08 +01:00
}
return rs;
2005-12-26 22:08:08 +01:00
}
2006-01-01 20:47:03 +01:00
2013-01-09 18:42:17 +01:00
bool Rack::operator==(const Rack &iOther) const
{
return m_tiles == iOther.m_tiles
2013-01-17 17:52:42 +01:00
&& m_nbTiles == iOther.m_nbTiles;
2013-01-09 18:42:17 +01:00
}