From 0be2769dfa65d2494d374eeea4883c4d37ad4c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sat, 18 Feb 2012 23:52:07 +0100 Subject: [PATCH] New utility methods to get the lowercase (joker) or uppercase (no joker) variant of a tile --- dic/tile.cpp | 17 +++++++++++++++++ dic/tile.h | 5 +++++ game/board.cpp | 14 ++++++++------ game/game.cpp | 2 +- game/game_move_cmd.cpp | 3 +-- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/dic/tile.cpp b/dic/tile.cpp index 28b4c35..c30c74d 100644 --- a/dic/tile.cpp +++ b/dic/tile.cpp @@ -27,6 +27,7 @@ #include "header.h" #include "encoding.h" #include "dic_exception.h" +#include "debug.h" INIT_LOGGER(dic, Tile); @@ -149,6 +150,22 @@ unsigned int Tile::toCode() const } +Tile Tile::toLower() const +{ + ASSERT(iswalpha(m_char), + "toLower() should be called on alphabetical tiles"); + return Tile(towlower(m_char)); +} + + +Tile Tile::toUpper() const +{ + ASSERT(iswalpha(m_char), + "toUpper() should be called on alphabetical tiles"); + return Tile(towupper(m_char)); +} + + bool Tile::operator<(const Tile &iOther) const { if (m_joker) diff --git a/dic/tile.h b/dic/tile.h index 093ef70..7730204 100644 --- a/dic/tile.h +++ b/dic/tile.h @@ -64,6 +64,11 @@ public: wstring getDisplayStr() const; vector getInputStr() const; + /// Return a copy of this tile, as a (non pure) joker + Tile toLower() const; + /// Return a copy of this tile, without joker information + Tile toUpper() const; + static const Tile &Joker() { return m_TheJoker; } bool operator <(const Tile &iOther) const; diff --git a/game/board.cpp b/game/board.cpp index 2afccbf..32c6daf 100644 --- a/game/board.cpp +++ b/game/board.cpp @@ -417,10 +417,11 @@ void Board::testRound(const Round &iRound) { if (isVacant(row, col + i)) { - Tile t = iRound.getTile(i); + const Tile &t = iRound.getTile(i); if (iRound.isJoker(i)) - t = Tile(towlower(t.toChar())); - m_testsRow[row][col + i] = t; + m_testsRow[row][col + i] = t.toLower(); + else + m_testsRow[row][col + i] = t; } } } @@ -430,10 +431,11 @@ void Board::testRound(const Round &iRound) { if (isVacant(row + i, col)) { - Tile t = iRound.getTile(i); + const Tile &t = iRound.getTile(i); if (iRound.isJoker(i)) - t = Tile(towlower(t.toChar())); - m_testsRow[row + i][col] = t; + m_testsRow[row + i][col] = t.toLower(); + else + m_testsRow[row + i][col] = t; } } } diff --git a/game/game.cpp b/game/game.cpp index 3ad2ec4..0d16419 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -352,7 +352,7 @@ PlayedRack Game::helperSetRackRandom(const PlayedRack &iPld, if (bestRound.isJoker(i) && bestRound.isPlayedFromRack(i)) { const Tile &jokerTile = bestRound.getTile(i); - Tile replacingTile(towupper(jokerTile.toChar())); + const Tile &replacingTile = jokerTile.toUpper(); LOG_DEBUG("helperSetRackRandom(): replacing Joker with " << lfw(replacingTile.toChar())); diff --git a/game/game_move_cmd.cpp b/game/game_move_cmd.cpp index bb3a76c..de46172 100644 --- a/game/game_move_cmd.cpp +++ b/game/game_move_cmd.cpp @@ -113,8 +113,7 @@ void GameMoveCmd::playRound() if (m_round.isPlayedFromRack(i) && m_round.isJoker(i)) { // Is the represented letter still available in the bag? - // XXX: this way to get the represented letter sucks... - Tile t(towupper(m_round.getTile(i).toChar())); + const Tile &t = m_round.getTile(i).toUpper(); if (bag.in(t)) { bag.replaceTile(Tile::Joker());