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) 1999-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
|
|
|
*****************************************************************************/
|
|
|
|
|
2005-12-26 16:52:48 +01:00
|
|
|
#include <string>
|
2008-11-23 21:13:02 +01:00
|
|
|
#include <sstream>
|
2006-01-22 13:23:52 +01:00
|
|
|
#include <wctype.h>
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "tile.h"
|
|
|
|
#include "round.h"
|
2006-01-22 13:23:52 +01:00
|
|
|
#include "encoding.h"
|
2012-02-19 12:26:44 +01:00
|
|
|
#include "debug.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
INIT_LOGGER(game, Round);
|
|
|
|
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
Round::Round()
|
2012-02-18 23:13:02 +01:00
|
|
|
: m_coord(1, 1, Coord::HORIZONTAL), m_points(0), m_bonus(false)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Round::setWord(const vector<Tile> &iTiles)
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
m_word = iTiles;
|
|
|
|
// XXX: always from rack?
|
2012-02-19 10:31:05 +01:00
|
|
|
m_rackOrigin = vector<bool>(iTiles.size(), true);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-19 12:26:44 +01:00
|
|
|
void Round::setTile(unsigned int iIndex, const Tile &iTile)
|
|
|
|
{
|
|
|
|
ASSERT(iIndex < m_word.size(), "Invalid index");
|
|
|
|
m_word[iIndex] = iTile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void Round::setFromRack(unsigned int iIndex)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-02-19 12:26:44 +01:00
|
|
|
ASSERT(iIndex < m_word.size(), "Invalid index");
|
2012-02-19 10:31:05 +01:00
|
|
|
m_rackOrigin[iIndex] = true;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void Round::setFromBoard(unsigned int iIndex)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-02-19 12:26:44 +01:00
|
|
|
ASSERT(iIndex < m_word.size(), "Invalid index");
|
2012-02-19 10:31:05 +01:00
|
|
|
m_rackOrigin[iIndex] = false;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
bool Round::isJoker(unsigned int iIndex) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-02-19 12:26:44 +01:00
|
|
|
ASSERT(iIndex < m_word.size(), "Invalid index");
|
2012-02-19 10:31:05 +01:00
|
|
|
return m_word[iIndex].isJoker();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
const Tile& Round::getTile(unsigned int iIndex) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-02-19 12:26:44 +01:00
|
|
|
ASSERT(iIndex < m_word.size(), "Invalid index");
|
2005-02-05 12:14:56 +01:00
|
|
|
return m_word[iIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
bool Round::isPlayedFromRack(unsigned int iIndex) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-02-19 12:26:44 +01:00
|
|
|
ASSERT(iIndex < m_word.size(), "Invalid index");
|
2012-02-19 10:31:05 +01:00
|
|
|
return m_rackOrigin[iIndex];
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-18 23:13:02 +01:00
|
|
|
void Round::addRightFromBoard(const Tile &iTile)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-02-19 10:31:05 +01:00
|
|
|
// The call to toUpper() is necessary to avoid that a joker
|
|
|
|
// on the board appears as a joker in the Round
|
|
|
|
m_word.push_back(iTile.toUpper());
|
|
|
|
m_rackOrigin.push_back(false);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-18 23:13:02 +01:00
|
|
|
void Round::addRightFromRack(const Tile &iTile, bool iJoker)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
if (iJoker)
|
2012-02-19 10:31:05 +01:00
|
|
|
m_word.push_back(iTile.toLower());
|
|
|
|
else
|
|
|
|
m_word.push_back(iTile);
|
|
|
|
m_rackOrigin.push_back(true);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-18 23:13:02 +01:00
|
|
|
void Round::removeRight()
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-02-19 12:26:44 +01:00
|
|
|
ASSERT(!m_word.empty() && !m_rackOrigin.empty(),
|
|
|
|
"Trying to remove tiles that were never added");
|
2005-02-05 12:14:56 +01:00
|
|
|
m_word.pop_back();
|
2012-02-19 10:31:05 +01:00
|
|
|
m_rackOrigin.pop_back();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2012-02-18 23:13:02 +01:00
|
|
|
|
2006-01-22 13:23:52 +01:00
|
|
|
wstring Round::getWord() const
|
2005-12-26 16:52:48 +01:00
|
|
|
{
|
2006-01-22 13:23:52 +01:00
|
|
|
wstring s;
|
2005-12-26 16:52:48 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
for (unsigned int i = 0; i < getWordLen(); i++)
|
2005-12-26 16:52:48 +01:00
|
|
|
{
|
2009-06-23 14:41:53 +02:00
|
|
|
wstring chr = getTile(i).getDisplayStr();
|
2006-01-22 13:23:52 +01:00
|
|
|
if (isJoker(i))
|
2012-12-23 18:15:55 +01:00
|
|
|
chr = toLower(chr);
|
2009-06-23 14:41:53 +02:00
|
|
|
s += chr;
|
2005-12-26 16:52:48 +01:00
|
|
|
}
|
2006-01-22 13:23:52 +01:00
|
|
|
return s;
|
2005-12-26 16:52:48 +01:00
|
|
|
}
|
|
|
|
|
2012-02-18 23:13:02 +01:00
|
|
|
|
2012-03-04 22:58:53 +01:00
|
|
|
unsigned Round::countJokersFromRack() const
|
|
|
|
{
|
|
|
|
unsigned count = 0;
|
|
|
|
for (unsigned int i = 0; i < getWordLen(); i++)
|
|
|
|
{
|
|
|
|
if (isJoker(i) && isPlayedFromRack(i))
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-22 13:23:52 +01:00
|
|
|
wstring Round::toString() const
|
2005-12-26 16:52:48 +01:00
|
|
|
{
|
2008-11-23 21:13:02 +01:00
|
|
|
wostringstream oss;
|
|
|
|
if (!getWord().empty())
|
2005-12-26 16:52:48 +01:00
|
|
|
{
|
2008-11-23 21:13:02 +01:00
|
|
|
oss << getWord() << L' ' << (getBonus() ? L'*' : L' ')
|
|
|
|
<< L' ' << getPoints() << L' ' << getCoord().toString();
|
2005-12-26 16:52:48 +01:00
|
|
|
}
|
2008-11-23 21:13:02 +01:00
|
|
|
return oss.str();
|
2005-12-26 16:52:48 +01:00
|
|
|
}
|
|
|
|
|
2012-12-30 16:13:31 +01:00
|
|
|
|
|
|
|
bool Round::operator==(const Round &iOther) const
|
|
|
|
{
|
|
|
|
return m_word == iOther.m_word
|
|
|
|
&& m_rackOrigin == iOther.m_rackOrigin
|
|
|
|
&& m_coord == iOther.m_coord
|
|
|
|
&& m_points == iOther.m_points
|
|
|
|
&& m_bonus == iOther.m_bonus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|