eliot/game/round.cpp

146 lines
3.2 KiB
C++
Raw Normal View History

/*****************************************************************************
* Eliot
* Copyright (C) 1999-2007 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
*****************************************************************************/
2005-12-26 16:52:48 +01:00
#include <string>
2008-11-23 21:13:02 +01:00
#include <sstream>
#include <algorithm> // For std::transform
#include <wctype.h>
#include "tile.h"
#include "round.h"
#include "encoding.h"
2012-02-18 22:26:52 +01:00
INIT_LOGGER(game, Round);
#define FROMBOARD 0x1
#define FROMRACK 0x2
#define JOKER 0x4
Round::Round()
2012-02-18 23:13:02 +01:00
: m_coord(1, 1, Coord::HORIZONTAL), m_points(0), m_bonus(false)
{
}
void Round::setWord(const vector<Tile> &iTiles)
{
m_word = iTiles;
// XXX: always from rack?
m_tileOrigin = vector<char>(iTiles.size(), FROMRACK);
}
void Round::setFromRack(unsigned int iIndex)
{
m_tileOrigin[iIndex] &= ~FROMBOARD;
m_tileOrigin[iIndex] |= FROMRACK;
}
void Round::setFromBoard(unsigned int iIndex)
{
m_tileOrigin[iIndex] &= ~FROMRACK;
m_tileOrigin[iIndex] |= FROMBOARD;
}
void Round::setJoker(unsigned int iIndex, bool value)
{
if (value)
m_tileOrigin[iIndex] |= JOKER;
else
m_tileOrigin[iIndex] &= ~JOKER;
}
bool Round::isJoker(unsigned int iIndex) const
{
return m_tileOrigin[iIndex] & JOKER;
}
const Tile& Round::getTile(unsigned int iIndex) const
{
return m_word[iIndex];
}
bool Round::isPlayedFromRack(unsigned int iIndex) const
{
return m_tileOrigin[iIndex] & FROMRACK;
}
2012-02-18 23:13:02 +01:00
void Round::addRightFromBoard(const Tile &iTile)
{
2012-02-18 23:13:02 +01:00
m_word.push_back(iTile);
m_tileOrigin.push_back(FROMBOARD);
}
2012-02-18 23:13:02 +01:00
void Round::addRightFromRack(const Tile &iTile, bool iJoker)
{
2012-02-18 23:13:02 +01:00
m_word.push_back(iTile);
char origin = FROMRACK;
if (iJoker)
{
origin |= JOKER;
}
m_tileOrigin.push_back(origin);
}
2012-02-18 23:13:02 +01:00
void Round::removeRight()
{
m_word.pop_back();
m_tileOrigin.pop_back();
}
2012-02-18 23:13:02 +01:00
wstring Round::getWord() const
2005-12-26 16:52:48 +01:00
{
wstring s;
2005-12-26 16:52:48 +01:00
for (unsigned int i = 0; i < getWordLen(); i++)
2005-12-26 16:52:48 +01:00
{
wstring chr = getTile(i).getDisplayStr();
if (isJoker(i))
std::transform(chr.begin(), chr.end(), chr.begin(), towlower);
s += chr;
2005-12-26 16:52:48 +01:00
}
return s;
2005-12-26 16:52:48 +01:00
}
2012-02-18 23:13:02 +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
}