- Removed various C-style casts

- Factorized code in the Bag class
This commit is contained in:
Olivier Teulière 2010-03-06 16:54:20 +00:00
parent 5642bfa191
commit b7fff8b4a1
7 changed files with 29 additions and 45 deletions

View file

@ -46,7 +46,7 @@ unsigned int Bag::in(const Tile &iTile) const
{
map<Tile, int>::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<Tile, int> 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<Tile, int> 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<Tile, int> 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;

View file

@ -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

View file

@ -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

View file

@ -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);
}

View file

@ -20,6 +20,7 @@
#include <vector>
#include <fstream>
#include <cmath>
#include <boost/foreach.hpp>
#include "xml_writer.h"
@ -124,7 +125,7 @@ void XmlWriter::write(const Game &iGame, const string &iFileName)
const AIPercent *ai = dynamic_cast<const AIPercent *>(&player);
if (ai == NULL)
throw SaveGameException("Invalid player type for player " + i);
out << indent << "<Level>" << (int)(ai->getPercent() * 100) << "</Level>" << endl;
out << indent << "<Level>" << lrint(ai->getPercent() * 100) << "</Level>" << endl;
}
removeIndent(indent);
out << indent << "</Player>" << endl;

View file

@ -19,7 +19,7 @@
*****************************************************************************/
#include <algorithm> // For std::transform
#include <math.h>
#include <cmath>
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
#include <QtGui/QMouseEvent>
@ -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)

View file

@ -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<unsigned int>(m_model->rowCount()))
{
return;
}