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
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-11-22 14:09:28 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2006-01-01 20:47:26 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
#include <algorithm>
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "pldrack.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "rack.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
INIT_LOGGER(game, PlayedRack);
|
|
|
|
|
|
|
|
|
2006-01-01 20:47:26 +01:00
|
|
|
PlayedRack::PlayedRack()
|
2008-01-08 14:52:32 +01:00
|
|
|
: m_reject(false)
|
2006-01-01 20:47:26 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
void PlayedRack::addOld(const Tile &t)
|
|
|
|
{
|
|
|
|
m_oldTiles.push_back(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayedRack::addNew(const Tile &t)
|
|
|
|
{
|
|
|
|
m_newTiles.push_back(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayedRack::getOldTiles(vector<Tile> &oTiles) const
|
|
|
|
{
|
|
|
|
oTiles.clear();
|
2008-01-08 14:52:32 +01:00
|
|
|
oTiles = m_oldTiles;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayedRack::getNewTiles(vector<Tile> &oTiles) const
|
|
|
|
{
|
|
|
|
oTiles.clear();
|
2008-01-08 14:52:32 +01:00
|
|
|
oTiles = m_newTiles;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayedRack::getAllTiles(vector<Tile> &oTiles) const
|
|
|
|
{
|
|
|
|
oTiles.clear();
|
2008-01-08 14:52:32 +01:00
|
|
|
oTiles = m_oldTiles;
|
|
|
|
oTiles.insert(oTiles.end(), m_newTiles.begin(), m_newTiles.end());
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayedRack::reset()
|
|
|
|
{
|
|
|
|
m_oldTiles.clear();
|
|
|
|
m_newTiles.clear();
|
2008-01-08 14:52:32 +01:00
|
|
|
m_reject = false;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayedRack::resetNew()
|
|
|
|
{
|
|
|
|
m_newTiles.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-26 21:10:41 +01:00
|
|
|
Rack PlayedRack::getOld() const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-01-26 21:10:41 +01:00
|
|
|
Rack rack;
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, m_oldTiles)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-01-26 21:10:41 +01:00
|
|
|
rack.add(tile);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2012-01-26 21:10:41 +01:00
|
|
|
return rack;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-26 21:10:41 +01:00
|
|
|
Rack PlayedRack::getNew() const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-01-26 21:10:41 +01:00
|
|
|
Rack rack;
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, m_newTiles)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-01-26 21:10:41 +01:00
|
|
|
rack.add(tile);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2012-01-26 21:10:41 +01:00
|
|
|
return rack;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-26 21:10:41 +01:00
|
|
|
Rack PlayedRack::getRack() const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-01-26 21:10:41 +01:00
|
|
|
Rack rack = getOld();
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, m_newTiles)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-01-26 21:10:41 +01:00
|
|
|
rack.add(tile);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2012-01-26 21:10:41 +01:00
|
|
|
return rack;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayedRack::setOld(const Rack &iRack)
|
|
|
|
{
|
|
|
|
m_oldTiles.clear();
|
2008-01-08 14:52:32 +01:00
|
|
|
iRack.getTiles(m_oldTiles);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayedRack::setNew(const Rack &iRack)
|
|
|
|
{
|
|
|
|
m_newTiles.clear();
|
2008-01-08 14:52:32 +01:00
|
|
|
iRack.getTiles(m_newTiles);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
void PlayedRack::setManual(const wstring& iLetters)
|
2006-01-01 20:47:26 +01:00
|
|
|
{
|
|
|
|
reset();
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// An empty rack is OK
|
|
|
|
if (iLetters.empty())
|
|
|
|
return;
|
2006-01-01 20:47:26 +01:00
|
|
|
|
2008-01-26 11:10:50 +01:00
|
|
|
// Handle the reject sign
|
|
|
|
unsigned int begin;
|
|
|
|
if (iLetters[0] == L'-')
|
|
|
|
{
|
|
|
|
setReject();
|
|
|
|
begin = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
begin = 0;
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
unsigned int i;
|
2008-01-26 11:10:50 +01:00
|
|
|
for (i = begin; i < iLetters.size() && iLetters[i] != L'+'; i++)
|
2006-01-01 20:47:26 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
addOld(Tile(iLetters[i]));
|
2006-01-01 20:47:26 +01:00
|
|
|
}
|
|
|
|
|
2006-01-22 13:23:52 +01:00
|
|
|
if (i < iLetters.size() && iLetters[i] == L'+')
|
2006-01-01 20:47:26 +01:00
|
|
|
{
|
|
|
|
for (i++; i < iLetters.size(); i++)
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
addNew(Tile(iLetters[i]));
|
2006-01-01 20:47:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
bool PlayedRack::checkRack(unsigned int cMin, unsigned int vMin) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
unsigned int v = 0;
|
|
|
|
unsigned int c = 0;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, m_oldTiles)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
if (tile.isVowel()) v++;
|
|
|
|
if (tile.isConsonant()) c++;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, m_newTiles)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
if (tile.isVowel()) v++;
|
|
|
|
if (tile.isConsonant()) c++;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2006-01-01 20:47:26 +01:00
|
|
|
return (v >= vMin) && (c >= cMin);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void PlayedRack::shuffleNew()
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
std::random_shuffle(m_newTiles.begin(), m_newTiles.end());
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2005-03-29 08:54:08 +02:00
|
|
|
|
2008-09-14 19:56:18 +02:00
|
|
|
void PlayedRack::shuffle()
|
|
|
|
{
|
|
|
|
m_newTiles.insert(m_newTiles.end(),
|
|
|
|
m_oldTiles.begin(), m_oldTiles.end());
|
|
|
|
m_oldTiles.clear();
|
|
|
|
shuffleNew();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-22 13:23:52 +01:00
|
|
|
wstring PlayedRack::toString(display_mode mode) const
|
2005-03-29 08:54:08 +02:00
|
|
|
{
|
2006-01-22 13:23:52 +01:00
|
|
|
wstring s;
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
if (mode >= RACK_EXTRA && m_reject)
|
2006-01-01 20:47:26 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
s += L"-";
|
2006-01-01 20:47:26 +01:00
|
|
|
}
|
2005-11-05 16:48:59 +01:00
|
|
|
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, m_oldTiles)
|
2006-01-01 20:47:26 +01:00
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
s += tile.getDisplayStr();
|
2006-01-01 20:47:26 +01:00
|
|
|
}
|
2005-11-05 16:48:59 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
if (mode > RACK_SIMPLE && getNbOld() > 0 && getNbNew() > 0)
|
2006-01-01 20:47:26 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
s += L"+";
|
2006-01-01 20:47:26 +01:00
|
|
|
}
|
2005-11-05 16:48:59 +01:00
|
|
|
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, m_newTiles)
|
2006-01-01 20:47:26 +01:00
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
s += tile.getDisplayStr();
|
2006-01-01 20:47:26 +01:00
|
|
|
}
|
2005-11-05 16:48:59 +01:00
|
|
|
|
|
|
|
return s;
|
2005-03-29 08:54:08 +02:00
|
|
|
}
|
2006-01-01 20:47:26 +01:00
|
|
|
|