diff --git a/game/bag.cpp b/game/bag.cpp index 32b50cd..e37f72a 100644 --- a/game/bag.cpp +++ b/game/bag.cpp @@ -46,7 +46,7 @@ unsigned int Bag::in(const Tile &iTile) const { map::const_iterator it = m_tilesMap.find(iTile); if (it != m_tilesMap.end()) - return (*it).second; + return it->second; return 0; } @@ -101,55 +101,34 @@ void Bag::replaceTile(const Tile &iTile) Tile Bag::selectRandom() const { - double max = m_ntiles; - ASSERT(max > 0, "The bag is empty"); - - int n = (int)(max * rand() / (RAND_MAX + 1.0)); - - std::pair p; - BOOST_FOREACH(p, m_tilesMap) - { - if (n < p.second) - return p.first; - n -= p.second; - } - ASSERT(false, "We should not come here"); - return Tile(); + return selectRandomTile(m_ntiles, false, false); } Tile Bag::selectRandomVowel() const { - double max = getNbVowels(); - ASSERT(max > 0, "Not enough vowels in the bag"); - - int n = (int)(max * rand() / (RAND_MAX + 1.0)); - - std::pair p; - BOOST_FOREACH(p, m_tilesMap) - { - if (!p.first.isVowel()) - continue; - if (n < p.second) - return p.first; - n -= p.second; - } - ASSERT(false, "We should not come here"); - return Tile(); + return selectRandomTile(getNbVowels(), true, false); } Tile Bag::selectRandomConsonant() const { - double max = getNbConsonants(); - ASSERT(max > 0, "Not enough consonants in the bag"); + return selectRandomTile(getNbConsonants(), false, true); +} - int n = (int)(max * rand() / (RAND_MAX + 1.0)); +Tile Bag::selectRandomTile(unsigned int total, + bool onlyVowels, bool onlyConsonants) const +{ + ASSERT(total > 0, "Not enough tiles (of the requested kind) in the bag"); + + int n = (int)((double)total * rand() / (RAND_MAX + 1.0)); std::pair p; BOOST_FOREACH(p, m_tilesMap) { - if (!p.first.isConsonant()) + if (onlyVowels && !p.first.isVowel()) + continue; + if (onlyConsonants && !p.first.isConsonant()) continue; if (n < p.second) return p.first; diff --git a/game/bag.h b/game/bag.h index 0ecafca..ea4bfbe 100644 --- a/game/bag.h +++ b/game/bag.h @@ -90,6 +90,10 @@ private: /// Total number of tiles in the bag int m_ntiles; + + /// Helper method, used by the various selectRandom*() methods + Tile selectRandomTile(unsigned int total, + bool onlyVowels, bool onlyConsonants) const; }; #endif diff --git a/game/duplicate.cpp b/game/duplicate.cpp index 56de2c5..9ffdc0b 100644 --- a/game/duplicate.cpp +++ b/game/duplicate.cpp @@ -228,8 +228,8 @@ void Duplicate::endTurn() // Handle solo bonus // First check whether there are enough players in the game for the // bonus to apply - int minNbPlayers = Settings::Instance().getInt("duplicate.solo-players"); - if (getNPlayers() >= (unsigned int)minNbPlayers && + unsigned int minNbPlayers = Settings::Instance().getInt("duplicate.solo-players"); + if (getNPlayers() >= minNbPlayers && bestMove.getType() == Move::VALID_ROUND) { // Find whether other players than imax have the same score diff --git a/game/results.cpp b/game/results.cpp index f663516..81fa2aa 100644 --- a/game/results.cpp +++ b/game/results.cpp @@ -216,7 +216,7 @@ void PercentResults::add(const Round &iRound) if (m_bestScore < iRound.getPoints()) { m_bestScore = iRound.getPoints(); - m_minScore = (int)ceil(m_bestScore * m_percent); + m_minScore = lrint(ceil(m_bestScore * m_percent)); } m_rounds.push_back(iRound); } diff --git a/game/xml_writer.cpp b/game/xml_writer.cpp index 3783885..3bbfa3a 100644 --- a/game/xml_writer.cpp +++ b/game/xml_writer.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include "xml_writer.h" @@ -124,7 +125,7 @@ void XmlWriter::write(const Game &iGame, const string &iFileName) const AIPercent *ai = dynamic_cast(&player); if (ai == NULL) throw SaveGameException("Invalid player type for player " + i); - out << indent << "" << (int)(ai->getPercent() * 100) << "" << endl; + out << indent << "" << lrint(ai->getPercent() * 100) << "" << endl; } removeIndent(indent); out << indent << "" << endl; diff --git a/qt/board_widget.cpp b/qt/board_widget.cpp index df62d39..045b99d 100644 --- a/qt/board_widget.cpp +++ b/qt/board_widget.cpp @@ -19,7 +19,7 @@ *****************************************************************************/ #include // For std::transform -#include +#include #include #include #include @@ -92,7 +92,7 @@ QSize BoardWidget::sizeHint() const void BoardWidget::paintEvent(QPaintEvent *) { const int size = std::min(width(), height()); - const int squareSize = (int)floor((size - 1) / (BOARD_MAX - BOARD_MIN + 2)); + const int squareSize = lrint(floor((size - 1) / (BOARD_MAX - BOARD_MIN + 2))); // The font must grow with the square size QFont letterFont = font(); @@ -223,7 +223,7 @@ void BoardWidget::mousePressEvent(QMouseEvent *iEvent) { // Find the coordinates const int size = std::min(width(), height()); - const int squareSize = (int)floor((size - 1) / (BOARD_MAX - BOARD_MIN + 2)); + const int squareSize = lrint(floor((size - 1) / (BOARD_MAX - BOARD_MIN + 2))); int row = iEvent->y() / squareSize; int col = iEvent->x() / squareSize; // Change the direction if this is exactly the same as the current one @@ -247,7 +247,7 @@ void BoardWidget::mousePressEvent(QMouseEvent *iEvent) { // Find the coordinates const int size = std::min(width(), height()); - const int squareSize = (int)floor((size - 1) / (BOARD_MAX - BOARD_MIN + 2)); + const int squareSize = lrint(floor((size - 1) / (BOARD_MAX - BOARD_MIN + 2))); int row = iEvent->y() / squareSize; int col = iEvent->x() / squareSize; // Change the direction if this is exactly the same as the current one @@ -278,7 +278,7 @@ void BoardWidget::mousePressEvent(QMouseEvent *iEvent) // - a right click toggles between vertical arrow and no arrow // Find the coordinates const int size = std::min(width(), height()); - const int squareSize = (int)floor((size - 1) / (BOARD_MAX - BOARD_MIN + 2)); + const int squareSize = lrint(floor((size - 1) / (BOARD_MAX - BOARD_MIN + 2))); int row = iEvent->y() / squareSize; int col = iEvent->x() / squareSize; if (iEvent->button() == Qt::LeftButton) diff --git a/qt/training_widget.cpp b/qt/training_widget.cpp index 50c8287..1c6205c 100644 --- a/qt/training_widget.cpp +++ b/qt/training_widget.cpp @@ -136,7 +136,7 @@ void TrainingWidget::updateModel() // Consider that there is nothing to do if the number of lines is correct // This avoids problems when the game is updated for a test play if (m_game != NULL && - m_game->trainingGetResults().size() == (unsigned int)m_model->rowCount()) + m_game->trainingGetResults().size() == static_cast(m_model->rowCount())) { return; }