eliot-dev
[Top][All Lists]
Advanced

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

[Eliot-dev] eliot/game freegame.cpp freegame.h game.cpp game.h


From: Olivier Teulière
Subject: [Eliot-dev] eliot/game freegame.cpp freegame.h game.cpp game.h
Date: Sun, 23 Nov 2008 17:07:42 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Changes by:     Olivier Teulière <ipkiss>      08/11/23 17:07:42

Modified files:
        game           : freegame.cpp freegame.h game.cpp game.h 

Log message:
        New command to handle changes of the current player. This fixes history 
navigation in free game mode

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/game/freegame.cpp?cvsroot=eliot&r1=1.28&r2=1.29
http://cvs.savannah.gnu.org/viewcvs/eliot/game/freegame.h?cvsroot=eliot&r1=1.15&r2=1.16
http://cvs.savannah.gnu.org/viewcvs/eliot/game/game.cpp?cvsroot=eliot&r1=1.46&r2=1.47
http://cvs.savannah.gnu.org/viewcvs/eliot/game/game.h?cvsroot=eliot&r1=1.43&r2=1.44

Patches:
Index: freegame.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/freegame.cpp,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -b -r1.28 -r1.29
--- freegame.cpp        23 Nov 2008 17:05:18 -0000      1.28
+++ freegame.cpp        23 Nov 2008 17:07:42 -0000      1.29
@@ -126,7 +126,7 @@
         accessNavigation().addAndExecute(pCmd);
     }
 
-    m_currPlayer = 0;
+    firstPlayer();
 
     accessNavigation().newTurn();
 

Index: freegame.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/freegame.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- freegame.h  23 Nov 2008 17:04:40 -0000      1.15
+++ freegame.h  23 Nov 2008 17:07:42 -0000      1.16
@@ -22,7 +22,6 @@
 #define _FREEGAME_H_
 
 #include "game.h"
-#include "tile.h"
 
 class Player;
 

Index: game.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/game.cpp,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -b -r1.46 -r1.47
--- game.cpp    23 Nov 2008 16:58:31 -0000      1.46
+++ game.cpp    23 Nov 2008 17:07:42 -0000      1.47
@@ -20,6 +20,7 @@
  *****************************************************************************/
 
 #include <boost/foreach.hpp>
+#include <sstream>
 
 #if ENABLE_NLS
 #   include <libintl.h>
@@ -458,14 +459,29 @@
 }
 
 
+void Game::firstPlayer()
+{
+    ASSERT(getNPlayers() != 0, "Expected at least one player");
+    // Make sure there is something to do
+    if (m_currPlayer == 0)
+        return;
+
+    Command *pCmd = new CurrentPlayerCmd(*this, 0);
+    accessNavigation().addAndExecute(pCmd);
+}
+
+
 void Game::prevPlayer()
 {
     ASSERT(getNPlayers() != 0, "Expected at least one player");
 
+    unsigned int newPlayerId;
     if (m_currPlayer == 0)
-        m_currPlayer = getNPlayers() - 1;
+        newPlayerId = getNPlayers() - 1;
     else
-        m_currPlayer--;
+        newPlayerId = m_currPlayer - 1;
+    Command *pCmd = new CurrentPlayerCmd(*this, newPlayerId);
+    accessNavigation().addAndExecute(pCmd);
 }
 
 
@@ -473,10 +489,13 @@
 {
     ASSERT(getNPlayers() != 0, "Expected at least one player");
 
+    unsigned int newPlayerId;
     if (m_currPlayer == getNPlayers() - 1)
-        m_currPlayer = 0;
+        newPlayerId = 0;
     else
-        m_currPlayer++;
+        newPlayerId = m_currPlayer + 1;
+    Command *pCmd = new CurrentPlayerCmd(*this, newPlayerId);
+    accessNavigation().addAndExecute(pCmd);
 }
 
 
@@ -552,3 +571,36 @@
     return 0;
 }
 
+
+Game::CurrentPlayerCmd::CurrentPlayerCmd(Game &ioGame,
+                             unsigned int iPlayerId)
+    : m_game(ioGame), m_newPlayerId(iPlayerId), m_oldPlayerId(0)
+{
+}
+
+
+void Game::CurrentPlayerCmd::doExecute()
+{
+    m_oldPlayerId = m_game.currPlayer();
+    m_game.setCurrentPlayer(m_newPlayerId);
+}
+
+
+void Game::CurrentPlayerCmd::doUndo()
+{
+    m_game.setCurrentPlayer(m_oldPlayerId);
+}
+
+
+wstring Game::CurrentPlayerCmd::toString() const
+{
+    wostringstream oss;
+    oss << L"CurrentPlayerCmd (new player: " << m_newPlayerId;
+    if (isExecuted())
+    {
+        oss << L"  old player: " << m_oldPlayerId;
+    }
+    oss << L")";
+    return oss.str();
+}
+

Index: game.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/game.h,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -b -r1.43 -r1.44
--- game.h      23 Nov 2008 17:06:44 -0000      1.43
+++ game.h      23 Nov 2008 17:07:42 -0000      1.44
@@ -29,6 +29,7 @@
 #include "board.h"
 #include "history.h"
 #include "navigation.h"
+#include "command.h"
 
 class Dictionary;
 class Player;
@@ -237,6 +238,28 @@
 
     int m_points;
 
+    /// Change the player who is supposed to play
+    void setCurrentPlayer(unsigned int iPlayerId) { m_currPlayer = iPlayerId; }
+
+    /// Command used to keep track of the current player changes
+    class CurrentPlayerCmd: public Command
+    {
+        public:
+            CurrentPlayerCmd(Game &ioGame,
+                             unsigned int iPlayerId);
+
+            virtual wstring toString() const;
+
+        protected:
+            virtual void doExecute();
+            virtual void doUndo();
+
+        private:
+            Game &m_game;
+            unsigned int m_newPlayerId;
+            unsigned int m_oldPlayerId;
+    };
+
 // TODO: check what should be private and what should be protected
 protected:
     /// All the players, indexed by their ID
@@ -289,6 +312,7 @@
      */
     int helperSetRackManual(unsigned int p, bool iCheck, const wstring 
&iLetters);
 
+    void firstPlayer();
     void prevPlayer();
     void nextPlayer();
 




reply via email to

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