eliot-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Eliot-dev] eliot game/bag.cpp game/bag.h game/duplicate.cp...


From: Olivier Teulière
Subject: [Eliot-dev] eliot game/bag.cpp game/bag.h game/duplicate.cp...
Date: Sat, 06 Mar 2010 16:54:21 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Changes by:     Olivier Teulière <ipkiss>       10/03/06 16:54:21

Modified files:
        game           : bag.cpp bag.h duplicate.cpp results.cpp 
                         xml_writer.cpp 
        qt             : board_widget.cpp training_widget.cpp 

Log message:
         - Removed various C-style casts
         - Factorized code in the Bag class

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/game/bag.cpp?cvsroot=eliot&r1=1.13&r2=1.14
http://cvs.savannah.gnu.org/viewcvs/eliot/game/bag.h?cvsroot=eliot&r1=1.13&r2=1.14
http://cvs.savannah.gnu.org/viewcvs/eliot/game/duplicate.cpp?cvsroot=eliot&r1=1.33&r2=1.34
http://cvs.savannah.gnu.org/viewcvs/eliot/game/results.cpp?cvsroot=eliot&r1=1.20&r2=1.21
http://cvs.savannah.gnu.org/viewcvs/eliot/game/xml_writer.cpp?cvsroot=eliot&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/board_widget.cpp?cvsroot=eliot&r1=1.15&r2=1.16
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/training_widget.cpp?cvsroot=eliot&r1=1.14&r2=1.15

Patches:
Index: game/bag.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/bag.cpp,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- game/bag.cpp        29 Nov 2009 16:01:32 -0000      1.13
+++ game/bag.cpp        6 Mar 2010 16:54:20 -0000       1.14
@@ -46,7 +46,7 @@
 {
     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 @@
 
 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;

Index: game/bag.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/bag.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- game/bag.h  29 Nov 2009 16:01:32 -0000      1.13
+++ game/bag.h  6 Mar 2010 16:54:20 -0000       1.14
@@ -90,6 +90,10 @@
 
     /// 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

Index: game/duplicate.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/duplicate.cpp,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -b -r1.33 -r1.34
--- game/duplicate.cpp  29 Nov 2009 16:01:32 -0000      1.33
+++ game/duplicate.cpp  6 Mar 2010 16:54:20 -0000       1.34
@@ -228,8 +228,8 @@
     // 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

Index: game/results.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/results.cpp,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- game/results.cpp    20 Feb 2010 13:31:12 -0000      1.20
+++ game/results.cpp    6 Mar 2010 16:54:20 -0000       1.21
@@ -216,7 +216,7 @@
     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);
 }

Index: game/xml_writer.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/xml_writer.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- game/xml_writer.cpp 29 Nov 2009 16:01:33 -0000      1.1
+++ game/xml_writer.cpp 6 Mar 2010 16:54:20 -0000       1.2
@@ -20,6 +20,7 @@
 
 #include <vector>
 #include <fstream>
+#include <cmath>
 #include <boost/foreach.hpp>
 
 #include "xml_writer.h"
@@ -124,7 +125,7 @@
             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;

Index: qt/board_widget.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/board_widget.cpp,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- qt/board_widget.cpp 23 Jun 2009 12:56:40 -0000      1.15
+++ qt/board_widget.cpp 6 Mar 2010 16:54:20 -0000       1.16
@@ -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 @@
 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 @@
     {
         // 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 @@
     {
         // 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 @@
     //  - 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)

Index: qt/training_widget.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/training_widget.cpp,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- qt/training_widget.cpp      23 Jan 2010 23:34:23 -0000      1.14
+++ qt/training_widget.cpp      6 Mar 2010 16:54:20 -0000       1.15
@@ -136,7 +136,7 @@
     // 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;
     }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]