2005-02-05 12:14:56 +01:00
|
|
|
/*****************************************************************************
|
2008-01-08 14:52:32 +01:00
|
|
|
* Eliot
|
2012-10-07 16:25:41 +02:00
|
|
|
* Copyright (C) 2002-2012 Antoine Fraboulet & Olivier Teulière
|
2008-01-08 14:52:32 +01:00
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
|
|
|
|
* Olivier Teulière <ipkiss @@ gmail.com>
|
2005-02-05 12:14:56 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "rack.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "dic.h"
|
2006-01-22 13:23:52 +01:00
|
|
|
#include "encoding.h"
|
|
|
|
#include "debug.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2006-01-22 13:23:52 +01:00
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
INIT_LOGGER(game, Rack);
|
|
|
|
|
|
|
|
|
2006-01-22 13:23:52 +01:00
|
|
|
Rack::Rack()
|
2013-01-17 17:52:42 +01:00
|
|
|
: m_tiles(Dictionary::GetDic().getTileNumber() + 1, 0), m_nbTiles(0)
|
2006-01-22 13:23:52 +01:00
|
|
|
{
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
void Rack::remove(const Tile &t)
|
|
|
|
{
|
2013-01-17 17:55:00 +01:00
|
|
|
ASSERT(count(t),
|
2011-07-30 21:48:05 +02:00
|
|
|
"The rack does not contain the letter " + lfw(t.getDisplayStr()));
|
2006-01-22 13:23:52 +01:00
|
|
|
m_tiles[t.toCode()]--;
|
2013-01-17 17:52:42 +01:00
|
|
|
m_nbTiles--;
|
2006-01-22 13:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Rack::clear()
|
|
|
|
{
|
2013-01-17 17:52:42 +01:00
|
|
|
for (unsigned i = 0; i < m_tiles.size(); i++)
|
2006-01-22 13:23:52 +01:00
|
|
|
{
|
|
|
|
m_tiles[i] = 0;
|
|
|
|
}
|
2013-01-17 17:52:42 +01:00
|
|
|
m_nbTiles = 0;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void Rack::getTiles(vector<Tile> &oTiles) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2013-01-17 17:52:42 +01:00
|
|
|
oTiles.reserve(m_nbTiles);
|
|
|
|
for (unsigned i = 1; i < m_tiles.size(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
// 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-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-26 23:56:40 +01:00
|
|
|
|
2008-01-11 11:09:26 +01:00
|
|
|
wstring Rack::toString() const
|
2005-12-26 22:08:08 +01:00
|
|
|
{
|
2006-01-22 13:23:52 +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
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
// Append m_tiles[i] copies of the char
|
2009-06-23 14:41:53 +02:00
|
|
|
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)
|
2009-06-23 14:41:53 +02:00
|
|
|
{
|
|
|
|
rs += chr;
|
|
|
|
}
|
2005-12-26 22:08:08 +01:00
|
|
|
}
|
2006-01-22 13:23:52 +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
|
|
|
}
|
|
|
|
|
|
|
|
|