Simplify slightly the Round API

This commit is contained in:
Olivier Teulière 2012-02-18 23:13:02 +01:00
parent 0be2769dfa
commit b855c5f64e
4 changed files with 18 additions and 45 deletions

View file

@ -63,11 +63,10 @@ void BoardSearch::search(Rack &iRack, Results &oResults, Coord::Direction iDir)
vector<Tile> rackTiles; vector<Tile> rackTiles;
iRack.getTiles(rackTiles); iRack.getTiles(rackTiles);
vector<Tile>::const_iterator it; vector<Tile>::const_iterator it;
Round partialWord;
for (int row = 1; row <= BOARD_DIM; row++) for (int row = 1; row <= BOARD_DIM; row++)
{ {
partialWord.init(); Round partialWord;
partialWord.accessCoord().setDir(iDir); partialWord.accessCoord().setDir(iDir);
partialWord.accessCoord().setRow(row); partialWord.accessCoord().setRow(row);
int lastanchor = 0; int lastanchor = 0;
@ -150,7 +149,7 @@ void BoardSearch::leftPart(Rack &iRack, Round &ioPartialWord,
leftPart(iRack, ioPartialWord, oResults, leftPart(iRack, ioPartialWord, oResults,
succ, iRow, iAnchor, iLimit - 1); succ, iRow, iAnchor, iLimit - 1);
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1); ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
ioPartialWord.removeRightToRack(l, false); ioPartialWord.removeRight();
iRack.add(l); iRack.add(l);
} }
if (hasJokerInRack) if (hasJokerInRack)
@ -161,7 +160,7 @@ void BoardSearch::leftPart(Rack &iRack, Round &ioPartialWord,
leftPart(iRack, ioPartialWord, oResults, leftPart(iRack, ioPartialWord, oResults,
succ, iRow, iAnchor, iLimit - 1); succ, iRow, iAnchor, iLimit - 1);
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1); ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
ioPartialWord.removeRightToRack(l, true); ioPartialWord.removeRight();
iRack.add(Tile::Joker()); iRack.add(Tile::Joker());
} }
} }
@ -198,7 +197,7 @@ void BoardSearch::extendRight(Rack &iRack, Round &ioPartialWord,
ioPartialWord.addRightFromRack(l, false); ioPartialWord.addRightFromRack(l, false);
extendRight(iRack, ioPartialWord, oResults, extendRight(iRack, ioPartialWord, oResults,
succ, iRow, iCol + 1, iAnchor); succ, iRow, iCol + 1, iAnchor);
ioPartialWord.removeRightToRack(l, false); ioPartialWord.removeRight();
iRack.add(l); iRack.add(l);
} }
if (hasJokerInRack) if (hasJokerInRack)
@ -207,7 +206,7 @@ void BoardSearch::extendRight(Rack &iRack, Round &ioPartialWord,
ioPartialWord.addRightFromRack(l, true); ioPartialWord.addRightFromRack(l, true);
extendRight(iRack, ioPartialWord, oResults, extendRight(iRack, ioPartialWord, oResults,
succ, iRow, iCol + 1, iAnchor); succ, iRow, iCol + 1, iAnchor);
ioPartialWord.removeRightToRack(l, true); ioPartialWord.removeRight();
iRack.add(Tile::Joker()); iRack.add(Tile::Joker());
} }
} }
@ -224,7 +223,7 @@ void BoardSearch::extendRight(Rack &iRack, Round &ioPartialWord,
ioPartialWord.addRightFromBoard(l); ioPartialWord.addRightFromBoard(l);
extendRight(iRack, ioPartialWord, extendRight(iRack, ioPartialWord,
oResults, succ, iRow, iCol + 1, iAnchor); oResults, succ, iRow, iCol + 1, iAnchor);
ioPartialWord.removeRightToBoard(l); ioPartialWord.removeRight();
// The letter will be present only once in the dictionary, // The letter will be present only once in the dictionary,
// so we can stop looping // so we can stop looping
break; break;

View file

@ -513,7 +513,7 @@ int Game::checkPlayedWord(const wstring &iCoord,
return 1; return 1;
// Init the round with the given coordinates // Init the round with the given coordinates
oRound.init(); oRound = Round();
oRound.accessCoord().setFromString(iCoord); oRound.accessCoord().setFromString(iCoord);
if (!oRound.getCoord().isValid()) if (!oRound.getCoord().isValid())
{ {

View file

@ -35,24 +35,10 @@ INIT_LOGGER(game, Round);
#define FROMRACK 0x2 #define FROMRACK 0x2
#define JOKER 0x4 #define JOKER 0x4
#define __UNUSED__ __attribute__((unused))
Round::Round() Round::Round()
: m_coord(1, 1, Coord::HORIZONTAL), m_points(0), m_bonus(false)
{ {
init();
}
void Round::init()
{
m_word.clear();
m_tileOrigin.clear();
m_coord.setRow(1);
m_coord.setCol(1);
m_coord.setDir(Coord::HORIZONTAL);
m_points = 0;
m_bonus = false;
} }
@ -105,24 +91,16 @@ bool Round::isPlayedFromRack(unsigned int iIndex) const
} }
void Round::addRightFromBoard(Tile c) void Round::addRightFromBoard(const Tile &iTile)
{ {
m_word.push_back(c); m_word.push_back(iTile);
m_tileOrigin.push_back(FROMBOARD); m_tileOrigin.push_back(FROMBOARD);
} }
void Round::removeRightToBoard(Tile __UNUSED__ c) void Round::addRightFromRack(const Tile &iTile, bool iJoker)
{ {
// c is unused. m_word.push_back(iTile);
m_word.pop_back();
m_tileOrigin.pop_back();
}
void Round::addRightFromRack(Tile c, bool iJoker)
{
m_word.push_back(c);
char origin = FROMRACK; char origin = FROMRACK;
if (iJoker) if (iJoker)
{ {
@ -132,13 +110,13 @@ void Round::addRightFromRack(Tile c, bool iJoker)
} }
void Round::removeRightToRack(Tile __UNUSED__ c, bool __UNUSED__ iJoker) void Round::removeRight()
{ {
// c is unused.
m_word.pop_back(); m_word.pop_back();
m_tileOrigin.pop_back(); m_tileOrigin.pop_back();
} }
wstring Round::getWord() const wstring Round::getWord() const
{ {
wstring s; wstring s;
@ -153,6 +131,7 @@ wstring Round::getWord() const
return s; return s;
} }
wstring Round::toString() const wstring Round::toString() const
{ {
wostringstream oss; wostringstream oss;

View file

@ -41,19 +41,14 @@ class Round
DEFINE_LOGGER(); DEFINE_LOGGER();
public: public:
/*************************
*
*************************/
Round(); Round();
void init();
/************************* /*************************
* *
*************************/ *************************/
void addRightFromBoard(Tile); void addRightFromBoard(const Tile &iTile);
void removeRightToBoard(Tile); void addRightFromRack(const Tile &iTile, bool iJoker);
void addRightFromRack(Tile, bool); void removeRight();
void removeRightToRack(Tile, bool);
/************************* /*************************
* General setters * General setters