eliot-dev
[Top][All Lists]
Advanced

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

[Eliot-dev] eliot game/ai_percent.cpp game/ai_percent.h gam...


From: eliot-dev
Subject: [Eliot-dev] eliot game/ai_percent.cpp game/ai_percent.h gam...
Date: Mon, 28 Jan 2008 19:17:37 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Changes by:     Olivier Teulière <ipkiss>      08/01/28 19:17:37

Modified files:
        game           : ai_percent.cpp ai_percent.h ai_player.h 
                         game.cpp game.h game_factory.cpp game_io.cpp 
                         player.cpp player.h training.cpp training.h 
        qt             : new_game.cpp 
        utils          : eliottxt.cpp 

Log message:
        Players are not created by the Game object anymore but by the user of 
the class.
        This allows more flexibility, in particular to choose the type of 
player or its name.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/game/ai_percent.cpp?cvsroot=eliot&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/eliot/game/ai_percent.h?cvsroot=eliot&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/eliot/game/ai_player.h?cvsroot=eliot&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/eliot/game/game.cpp?cvsroot=eliot&r1=1.37&r2=1.38
http://cvs.savannah.gnu.org/viewcvs/eliot/game/game.h?cvsroot=eliot&r1=1.33&r2=1.34
http://cvs.savannah.gnu.org/viewcvs/eliot/game/game_factory.cpp?cvsroot=eliot&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/eliot/game/game_io.cpp?cvsroot=eliot&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/eliot/game/player.cpp?cvsroot=eliot&r1=1.15&r2=1.16
http://cvs.savannah.gnu.org/viewcvs/eliot/game/player.h?cvsroot=eliot&r1=1.20&r2=1.21
http://cvs.savannah.gnu.org/viewcvs/eliot/game/training.cpp?cvsroot=eliot&r1=1.20&r2=1.21
http://cvs.savannah.gnu.org/viewcvs/eliot/game/training.h?cvsroot=eliot&r1=1.16&r2=1.17
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/new_game.cpp?cvsroot=eliot&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/eliot/utils/eliottxt.cpp?cvsroot=eliot&r1=1.19&r2=1.20

Patches:
Index: game/ai_percent.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/ai_percent.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- game/ai_percent.cpp 19 Jan 2008 19:33:08 -0000      1.7
+++ game/ai_percent.cpp 28 Jan 2008 19:17:33 -0000      1.8
@@ -28,8 +28,8 @@
 #include "ai_percent.h"
 
 
-AIPercent::AIPercent(int iId, float iPercent)
-    : AIPlayer(iId), m_percent(iPercent)
+AIPercent::AIPercent(float iPercent)
+    : m_percent(iPercent)
 {
     // Ensure the decimal value of the percentage is between 0 and 1
     if (m_percent < 0)

Index: game/ai_percent.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/ai_percent.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- game/ai_percent.h   8 Jan 2008 13:52:37 -0000       1.7
+++ game/ai_percent.h   28 Jan 2008 19:17:33 -0000      1.8
@@ -39,7 +39,7 @@
 {
 public:
     /// Constructor, taking the percentage (0.0 <= iPercent <= 1.0)
-    AIPercent(int iId, float iPercent);
+    AIPercent(float iPercent);
     virtual ~AIPercent() {}
 
     /**

Index: game/ai_player.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/ai_player.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- game/ai_player.h    8 Jan 2008 13:52:37 -0000       1.8
+++ game/ai_player.h    28 Jan 2008 19:17:33 -0000      1.9
@@ -77,7 +77,7 @@
 
 protected:
     /// This class is a pure interface, forbid any direct instanciation
-    AIPlayer(unsigned int iId): Player(iId) {}
+    AIPlayer() {}
 };
 
 #endif

Index: game/game.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/game.cpp,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -b -r1.37 -r1.38
--- game/game.cpp       26 Jan 2008 10:10:51 -0000      1.37
+++ game/game.cpp       28 Jan 2008 19:17:34 -0000      1.38
@@ -26,7 +26,6 @@
 #include "pldrack.h"
 #include "results.h"
 #include "player.h"
-#include "ai_percent.h"
 #include "game.h"
 #include "game_factory.h"
 #include "turn.h"
@@ -521,17 +520,13 @@
 }
 
 
-void Game::addHumanPlayer()
+void Game::addPlayer(Player *iPlayer)
 {
-    // The ID of the player is its position in the m_players vector
-    m_players.push_back(new HumanPlayer(getNPlayers()));
-}
-
+    ASSERT(iPlayer != NULL, "Invalid player pointer in addPlayer()");
 
-void Game::addAIPlayer()
-{
-    // TODO: allow other percentages, and even other types of AI
-    m_players.push_back(new AIPercent(getNPlayers(), 1));
+    // The ID of the player is its position in the m_players vector
+    iPlayer->setId(getNPlayers());
+    m_players.push_back(iPlayer);
 }
 
 

Index: game/game.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/game.h,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -b -r1.33 -r1.34
--- game/game.h 15 Jan 2008 14:56:38 -0000      1.33
+++ game/game.h 28 Jan 2008 19:17:34 -0000      1.34
@@ -110,11 +110,14 @@
     const Player& getCurrentPlayer() const { return getPlayer(currPlayer()); };
     unsigned int getNPlayers() const { return m_players.size(); }
     unsigned int getNHumanPlayers() const;
-    virtual void addHumanPlayer();
-    // TODO: Ability to specify which kind of AI player is wanted
-    virtual void addAIPlayer();
     unsigned int currPlayer() const { return m_currPlayer; }
 
+    /**
+     * Add a player to the game.
+     * The Game object takes ownership of the given player
+     */
+    virtual void addPlayer(Player *iPlayer);
+
     /***************
      * Game handling
      ***************/

Index: game/game_factory.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/game_factory.cpp,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- game/game_factory.cpp       19 Jan 2008 19:33:08 -0000      1.10
+++ game/game_factory.cpp       28 Jan 2008 19:17:34 -0000      1.11
@@ -39,6 +39,7 @@
 #include "freegame.h"
 #include "duplicate.h"
 #include "player.h"
+#include "ai_percent.h"
 #include "dic.h"
 #include "encoding.h"
 
@@ -207,11 +208,13 @@
     for (unsigned int i = 0; i < m_players.size(); ++i)
     {
         // Human?
+        Player *player;
         if (m_players[i].first)
-            game->addHumanPlayer();
+            player = new HumanPlayer;
         else
-            game->addAIPlayer();
-        const_cast<Player*>(&game->getPlayer(i))->setName(m_players[i].second);
+            player = new AIPercent(1);
+        player->setName(m_players[i].second);
+        game->addPlayer(player);
     }
 
     // 6) Set the variant

Index: game/game_io.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/game_io.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- game/game_io.cpp    11 Jan 2008 10:09:27 -0000      1.7
+++ game/game_io.cpp    28 Jan 2008 19:17:35 -0000      1.8
@@ -31,6 +31,7 @@
 #include "round.h"
 #include "turn.h"
 #include "player.h"
+#include "ai_percent.h"
 #include "game.h"
 #include "game_factory.h"
 #include "training.h"
@@ -261,7 +262,7 @@
                 if (string(type) == "Human")
                 {
                     debug("   add Human player\n");
-                    pGame->addHumanPlayer();
+                    pGame->addPlayer(new HumanPlayer);
                 }
                 else if (string(type) == "Computer")
                 {
@@ -273,7 +274,7 @@
                     else
                     {
                         debug("   add Computer player\n");
-                        pGame->addAIPlayer();
+                        pGame->addPlayer(new AIPercent(1));
                     }
                 }
                 else

Index: game/player.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/player.cpp,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- game/player.cpp     8 Jan 2008 13:52:39 -0000       1.15
+++ game/player.cpp     28 Jan 2008 19:17:35 -0000      1.16
@@ -33,8 +33,8 @@
 #include "debug.h"
 
 
-Player::Player(unsigned int iId)
-    : m_id(iId), m_score(0)
+Player::Player()
+    : m_id(0), m_score(0)
 {
 }
 

Index: game/player.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/player.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- game/player.h       19 Jan 2008 19:33:08 -0000      1.20
+++ game/player.h       28 Jan 2008 19:17:35 -0000      1.21
@@ -39,7 +39,7 @@
 class Player
 {
 public:
-    explicit Player(unsigned int iId);
+    explicit Player();
     virtual ~Player() {}
 
     // Pseudo RTTI
@@ -50,6 +50,9 @@
     /// Set the name of the player
     void setName(const wstring &iName) { m_name = iName; }
 
+    /// Set the ID
+    void setId(unsigned int iId) { m_id = iId; }
+
     /**************************
      * General getters
      **************************/
@@ -106,7 +109,7 @@
 class HumanPlayer: public Player
 {
 public:
-    HumanPlayer(int iId): Player(iId) {}
+    HumanPlayer(): Player() {}
     virtual ~HumanPlayer() {}
 
     // Pseudo RTTI

Index: game/training.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/training.cpp,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- game/training.cpp   19 Jan 2008 19:33:08 -0000      1.20
+++ game/training.cpp   28 Jan 2008 19:17:35 -0000      1.21
@@ -45,7 +45,7 @@
     : Game(iDic)
 {
     // Training mode implicitly uses 1 human player
-    Game::addHumanPlayer();
+    Game::addPlayer(new HumanPlayer);
     m_players[0]->setName(convertToWc(_("Training")));
 }
 
@@ -175,17 +175,11 @@
 }
 
 
-void Training::addHumanPlayer()
+void Training::addPlayer(Player *iPlayer)
 {
-    // We are not supposed to be here...
-    ASSERT(false, "Trying to add a human player in Training mode");
-}
-
-
-void Training::addAIPlayer()
-{
-    // We are not supposed to be here...
-    ASSERT(false, "Trying to add a AI player in Training mode");
+    // Override the default behaviour to do nothing
+    // except releasing memory
+    delete iPlayer;
 }
 
 

Index: game/training.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/training.h,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- game/training.h     8 Jan 2008 13:52:40 -0000       1.16
+++ game/training.h     28 Jan 2008 19:17:36 -0000      1.17
@@ -64,11 +64,10 @@
     int setRack(set_rack_mode iMode, bool iCheck, const wstring &iLetters);
 
     /*************************
-     * Override the default behaviour of these methods, because in training
+     * Override the default behaviour of addPlayer(), because in training
      * mode we only want a human player
      *************************/
-    virtual void addHumanPlayer();
-    virtual void addAIPlayer();
+    virtual void addPlayer(Player *iPlayer);
 
     /*************************
      * Functions to access the current search results

Index: qt/new_game.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/new_game.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- qt/new_game.cpp     26 Jan 2008 16:58:47 -0000      1.2
+++ qt/new_game.cpp     28 Jan 2008 19:17:36 -0000      1.3
@@ -30,6 +30,7 @@
 #include "freegame.h"
 #include "duplicate.h"
 #include "player.h"
+#include "ai_percent.h"
 
 
 const QString NewGame::kHUMAN = _q("Human");
@@ -111,14 +112,13 @@
             allNames.insert(name);
 
             QString type = m_model->data(m_model->index(num, 1)).toString();
+            Player *player;
             if (type == kHUMAN)
-                game->addHumanPlayer();
+                player = new HumanPlayer;
             else
-                game->addAIPlayer();
-            // FIXME: ugly const_cast, because of the awkward Game API
-            // (we should have addPlayer(Player &) instead)
-            const Player *player = &game->getPlayer(game->getNPlayers() - 1);
-            const_cast<Player *>(player)->setName(qtw(name));
+                player = new AIPercent(1);
+            player->setName(qtw(name));
+            game->addPlayer(player);
         }
     }
     else

Index: utils/eliottxt.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/utils/eliottxt.cpp,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -b -r1.19 -r1.20
--- utils/eliottxt.cpp  8 Jan 2008 13:52:42 -0000       1.19
+++ utils/eliottxt.cpp  28 Jan 2008 19:17:37 -0000      1.20
@@ -41,6 +41,8 @@
 #include "training.h"
 #include "duplicate.h"
 #include "freegame.h"
+#include "player.h"
+#include "ai_percent.h"
 #include "encoding.h"
 
 
@@ -959,7 +961,7 @@
                     }
                     Duplicate *game = 
GameFactory::Instance()->createDuplicate(iDic);
                     for (i = 0; i < _wtoi(token); i++)
-                        game->addHumanPlayer();
+                        game->addPlayer(new HumanPlayer);
                     token = next_token_digit(NULL, delim, &state);
                     if (token == NULL)
                     {
@@ -967,7 +969,7 @@
                         break;
                     }
                     for (i = 0; i < _wtoi(token); i++)
-                        game->addAIPlayer();
+                        game->addPlayer(new AIPercent(1));
                     game->start();
                     loop_duplicate(*game);
                     GameFactory::Instance()->releaseGame(*game);
@@ -985,7 +987,7 @@
                     }
                     FreeGame *game = 
GameFactory::Instance()->createFreeGame(iDic);
                     for (i = 0; i < _wtoi(token); i++)
-                        game->addHumanPlayer();
+                        game->addPlayer(new HumanPlayer);
                     token = next_token_digit(NULL, delim, &state);
                     if (token == NULL)
                     {
@@ -993,7 +995,7 @@
                         break;
                     }
                     for (i = 0; i < _wtoi(token); i++)
-                        game->addAIPlayer();
+                        game->addPlayer(new AIPercent(1));
                     game->start();
                     loop_freegame(*game);
                     GameFactory::Instance()->releaseGame(*game);
@@ -1003,8 +1005,8 @@
                 {
                     // New duplicate game
                     Duplicate *game = 
GameFactory::Instance()->createDuplicate(iDic);
-                    game->addHumanPlayer();
-                    game->addAIPlayer();
+                    game->addPlayer(new HumanPlayer);
+                    game->addPlayer(new AIPercent(1));
                     game->start();
                     loop_duplicate(*game);
                     GameFactory::Instance()->releaseGame(*game);
@@ -1014,8 +1016,8 @@
                 {
                     // New free game
                     FreeGame *game = 
GameFactory::Instance()->createFreeGame(iDic);
-                    game->addHumanPlayer();
-                    game->addAIPlayer();
+                    game->addPlayer(new HumanPlayer);
+                    game->addPlayer(new AIPercent(1));
                     game->start();
                     loop_freegame(*game);
                     GameFactory::Instance()->releaseGame(*game);




reply via email to

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