eliot-dev
[Top][All Lists]
Advanced

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

[Eliot-dev] eliot game/Makefile.am game/debug.h game/duplic...


From: Olivier Teulière
Subject: [Eliot-dev] eliot game/Makefile.am game/debug.h game/duplic...
Date: Sun, 23 Nov 2008 16:55:29 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Changes by:     Olivier Teulière <ipkiss>      08/11/23 16:55:29

Modified files:
        game           : Makefile.am debug.h duplicate.cpp freegame.cpp 
                         game.cpp game.h training.cpp turn_cmd.cpp 
                         turn_cmd.h 
        qt             : main_window.cpp main_window.h 
        qt/ui          : main_window.ui 
        utils          : eliottxt.cpp 
Added files:
        game           : navigation.cpp navigation.h 

Log message:
         - Fixed compilation in debug mode
         - Centralized navigation in the game history in the Navigation class
         - The Qt interface now has (very) basic controls to navigate in the 
history.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/game/Makefile.am?cvsroot=eliot&r1=1.20&r2=1.21
http://cvs.savannah.gnu.org/viewcvs/eliot/game/debug.h?cvsroot=eliot&r1=1.14&r2=1.15
http://cvs.savannah.gnu.org/viewcvs/eliot/game/duplicate.cpp?cvsroot=eliot&r1=1.22&r2=1.23
http://cvs.savannah.gnu.org/viewcvs/eliot/game/freegame.cpp?cvsroot=eliot&r1=1.24&r2=1.25
http://cvs.savannah.gnu.org/viewcvs/eliot/game/game.cpp?cvsroot=eliot&r1=1.44&r2=1.45
http://cvs.savannah.gnu.org/viewcvs/eliot/game/game.h?cvsroot=eliot&r1=1.39&r2=1.40
http://cvs.savannah.gnu.org/viewcvs/eliot/game/training.cpp?cvsroot=eliot&r1=1.25&r2=1.26
http://cvs.savannah.gnu.org/viewcvs/eliot/game/turn_cmd.cpp?cvsroot=eliot&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/eliot/game/turn_cmd.h?cvsroot=eliot&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/eliot/game/navigation.cpp?cvsroot=eliot&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/eliot/game/navigation.h?cvsroot=eliot&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/main_window.cpp?cvsroot=eliot&r1=1.17&r2=1.18
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/main_window.h?cvsroot=eliot&r1=1.11&r2=1.12
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/ui/main_window.ui?cvsroot=eliot&r1=1.11&r2=1.12
http://cvs.savannah.gnu.org/viewcvs/eliot/utils/eliottxt.cpp?cvsroot=eliot&r1=1.27&r2=1.28

Patches:
Index: game/Makefile.am
===================================================================
RCS file: /cvsroot/eliot/eliot/game/Makefile.am,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- game/Makefile.am    23 Nov 2008 08:18:03 -0000      1.20
+++ game/Makefile.am    23 Nov 2008 16:55:28 -0000      1.21
@@ -43,6 +43,7 @@
     board_cross.cpp \
     board_search.cpp \
     settings.cpp settings.h \
+    navigation.cpp navigation.h \
     game.cpp game.h \
     game_move_cmd.h game_move_cmd.cpp \
     turn_cmd.cpp turn_cmd.h \

Index: game/debug.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/debug.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- game/debug.h        22 Nov 2008 13:09:30 -0000      1.14
+++ game/debug.h        23 Nov 2008 16:55:28 -0000      1.15
@@ -28,6 +28,11 @@
 
 #ifdef DEBUG
 #   include <iostream>
+#   include <cstdlib>
+
+using std::cerr;
+using std::endl;
+
 // Assertion macro: if the condition is not verified, print a message on stderr
 // and stops execution, otherwise do nothing.
 #   define ASSERT(cond, msg) \

Index: game/duplicate.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/duplicate.cpp,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -b -r1.22 -r1.23
--- game/duplicate.cpp  23 Nov 2008 08:18:05 -0000      1.22
+++ game/duplicate.cpp  23 Nov 2008 16:55:28 -0000      1.23
@@ -33,7 +33,6 @@
 #include "game_move_cmd.h"
 #include "ai_player.h"
 #include "settings.h"
-#include "turn_cmd.h"
 #include "debug.h"
 
 
@@ -111,7 +110,7 @@
         for (unsigned int i = 0; i < getNPlayers(); i++)
         {
             Command *pCmd = new PlayerRackCmd(*m_players[i], newRack);
-            m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+            accessNavigation().addAndExecute(pCmd);
             // Nobody has played yet in this round
             m_hasPlayed[i] = false;
         }
@@ -161,7 +160,7 @@
 void Duplicate::recordPlayerMove(const Move &iMove, Player &ioPlayer)
 {
     Command *pCmd = new PlayerMoveCmd(ioPlayer, iMove);
-    m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+    accessNavigation().addAndExecute(pCmd);
 
     m_hasPlayed[ioPlayer.getId()] = true;
 }
@@ -216,7 +215,7 @@
     // Play the best word on the board
     Command *pCmd = new GameMoveCmd(*this, bestMove,
                                     getPlayer(imax).getLastRack(), imax);
-    m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+    accessNavigation().addAndExecute(pCmd);
 
     // Leave the same reliquate to all players
     // This is required by the start() method which will be called to
@@ -227,11 +226,11 @@
         if (i != imax)
         {
             Command *pCmd = new PlayerRackCmd(*m_players[i], pld);
-            m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+            accessNavigation().addAndExecute(pCmd);
         }
     }
 
-    newTurn();
+    accessNavigation().newTurn();
 
     // Start next turn...
     start();

Index: game/freegame.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/freegame.cpp,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -b -r1.24 -r1.25
--- game/freegame.cpp   23 Nov 2008 08:18:06 -0000      1.24
+++ game/freegame.cpp   23 Nov 2008 16:55:28 -0000      1.25
@@ -39,7 +39,6 @@
 #include "ai_player.h"
 #include "settings.h"
 #include "turn.h"
-#include "turn_cmd.h"
 
 #include "debug.h"
 
@@ -110,7 +109,7 @@
 void FreeGame::recordPlayerMove(const Move &iMove, Player &ioPlayer)
 {
     Command *pCmd = new PlayerMoveCmd(ioPlayer, iMove);
-    m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+    accessNavigation().addAndExecute(pCmd);
 }
 
 
@@ -124,7 +123,7 @@
         const PlayedRack &newRack =
             helperSetRackRandom(getPlayer(i).getCurrentRack(), false, 
RACK_NEW);
         Command *pCmd = new PlayerRackCmd(*m_players[i], newRack);
-        m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+        accessNavigation().addAndExecute(pCmd);
     }
 
     m_currPlayer = 0;
@@ -146,7 +145,7 @@
     Command *pCmd = new GameMoveCmd(*this, move,
                                     getCurrentPlayer().getLastRack(),
                                     m_currPlayer);
-    m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+    accessNavigation().addAndExecute(pCmd);
 
     // Complete the rack for the player that just played
     if (move.getType() == Move::VALID_ROUND ||
@@ -158,7 +157,7 @@
                 helperSetRackRandom(getCurrentPlayer().getCurrentRack(), 
false, RACK_NEW);
             Command *pCmd = new PlayerRackCmd(*m_players[m_currPlayer],
                                               newRack);
-            m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+            accessNavigation().addAndExecute(pCmd);
         }
         catch (EndGameException &e)
         {
@@ -171,7 +170,7 @@
     // Next player
     nextPlayer();
 
-    newTurn();
+    accessNavigation().newTurn();
 
     // If this player is an AI, make it play now
     if (!getCurrentPlayer().isHuman())

Index: game/game.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/game.cpp,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -b -r1.44 -r1.45
--- game/game.cpp       23 Nov 2008 08:18:06 -0000      1.44
+++ game/game.cpp       23 Nov 2008 16:55:28 -0000      1.45
@@ -55,8 +55,6 @@
     m_points = 0;
     m_currPlayer = 0;
     m_finished = false;
-    m_currTurn = -1;
-    newTurn();
 }
 
 
@@ -66,10 +64,6 @@
     {
         delete p;
     }
-    BOOST_FOREACH(Command *c, m_turnCommands)
-    {
-        delete c;
-    }
 }
 
 
@@ -85,6 +79,12 @@
     if (m_history.getSize() < n)
         throw GameException("Cannot go back that far");
 
+    for (unsigned int i = 0; i < n+1; ++i)
+    {
+        m_navigation.prevTurn();
+    }
+    m_navigation.clearFuture();
+#if 0
     for (unsigned int i = 0; i < n; i++)
     {
         prevPlayer();
@@ -113,6 +113,7 @@
         m_players[m_currPlayer]->removeLastTurn();
         m_history.removeLastTurn();
     }
+#endif
     return 0;
 }
 
@@ -198,7 +199,6 @@
 PlayedRack Game::helperSetRackRandom(const PlayedRack &iPld,
                                      bool iCheck, set_rack_mode mode) const
 {
-    ASSERT(p < getNPlayers(), "Wrong player number");
     // FIXME: RACK_MANUAL shouldn't be in the enum
     ASSERT(mode != RACK_MANUAL, "Invalid rack mode");
 
@@ -618,51 +618,3 @@
     return 0;
 }
 
-/*********************************************************
- *********************************************************/
-
-void Game::prevTurn()
-{
-    if (m_currTurn > 0)
-    {
-        --m_currTurn;
-        m_turnCommands[m_currTurn]->undo();
-    }
-}
-
-
-void Game::nextTurn()
-{
-    if (m_currTurn + 1 < m_turnCommands.size())
-    {
-        m_turnCommands[m_currTurn]->execute();
-        ++m_currTurn;
-    }
-}
-
-
-void Game::firstTurn()
-{
-    while (m_currTurn > 0)
-    {
-        prevTurn();
-    }
-}
-
-
-void Game::lastTurn()
-{
-    while (m_currTurn + 1 < m_turnCommands.size())
-    {
-        nextTurn();
-    }
-}
-
-
-void Game::newTurn()
-{
-    lastTurn();
-    m_turnCommands.push_back(new TurnCmd);
-    ++m_currTurn;
-}
-

Index: game/game.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/game.h,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -b -r1.39 -r1.40
--- game/game.h 23 Nov 2008 08:18:06 -0000      1.39
+++ game/game.h 23 Nov 2008 16:55:28 -0000      1.40
@@ -28,6 +28,7 @@
 #include "bag.h"
 #include "board.h"
 #include "history.h"
+#include "navigation.h"
 
 class Dictionary;
 class Player;
@@ -35,7 +36,6 @@
 class Round;
 class Rack;
 class Turn;
-class TurnCmd;
 
 using namespace std;
 
@@ -222,10 +222,8 @@
 
     void addPoints(int iPoints) { m_points += iPoints; }
 
-    void prevTurn();
-    void nextTurn();
-    void firstTurn();
-    void lastTurn();
+    const Navigation & getNavigation() const { return m_navigation; }
+    Navigation & accessNavigation() { return m_navigation; }
 
 private:
     /// Variant
@@ -239,8 +237,9 @@
      */
     History m_history;
 
-    int m_points;
+    Navigation m_navigation;
 
+    int m_points;
 
 // TODO: check what should be private and what should be protected
 protected:
@@ -255,17 +254,12 @@
     /// Bag
     Bag m_bag;
 
-    vector<TurnCmd *> m_turnCommands;
-    unsigned int m_currTurn;
-
     bool m_finished;
 
     /*********************************************************
      * Helper functions
      *********************************************************/
 
-    void newTurn();
-
     /**
      * Complete the given rack randomly.
      *

Index: game/training.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/training.cpp,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -b -r1.25 -r1.26
--- game/training.cpp   23 Nov 2008 08:18:10 -0000      1.25
+++ game/training.cpp   23 Nov 2008 16:55:29 -0000      1.26
@@ -40,7 +40,6 @@
 #include "game_move_cmd.h"
 #include "training.h"
 #include "encoding.h"
-#include "turn_cmd.h"
 
 #include "debug.h"
 
@@ -60,7 +59,7 @@
     const PlayedRack &newRack =
         helperSetRackRandom(getCurrentPlayer().getCurrentRack(), iCheck, mode);
     Command *pCmd = new PlayerRackCmd(*m_players[m_currPlayer], newRack);
-    m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+    accessNavigation().addAndExecute(pCmd);
 }
 
 
@@ -131,7 +130,7 @@
     // (called in this class in endTurn()).
     // See the big comment in game.cpp, line 96
     Command *pCmd = new PlayerMoveCmd(ioPlayer, iMove);
-    m_turnCommands[m_currTurn]->addAndExecute(pCmd);
+    accessNavigation().addAndExecute(pCmd);
 }
 
 
@@ -154,8 +153,8 @@
     Command *pCmd = new GameMoveCmd(*this, move,
                                     getCurrentPlayer().getLastRack(),
                                     m_currPlayer);
-    m_turnCommands[m_currTurn]->addAndExecute(pCmd);
-    newTurn();
+    accessNavigation().addAndExecute(pCmd);
+    accessNavigation().newTurn();
 }
 
 

Index: game/turn_cmd.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/turn_cmd.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- game/turn_cmd.cpp   23 Nov 2008 08:18:10 -0000      1.1
+++ game/turn_cmd.cpp   23 Nov 2008 16:55:29 -0000      1.2
@@ -24,6 +24,13 @@
 #include "player.h"
 
 
+TurnCmd::TurnCmd()
+{
+    // Fake execution
+    execute();
+}
+
+
 TurnCmd::~TurnCmd()
 {
     BOOST_FOREACH(Command *cmd, m_commands)

Index: game/turn_cmd.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/turn_cmd.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- game/turn_cmd.h     23 Nov 2008 08:18:11 -0000      1.1
+++ game/turn_cmd.h     23 Nov 2008 16:55:29 -0000      1.2
@@ -35,6 +35,7 @@
 class TurnCmd: public Command
 {
     public:
+        TurnCmd();
         virtual ~TurnCmd();
 
         /**
@@ -43,6 +44,8 @@
          */
         void addAndExecute(Command *iCmd);
 
+        bool isEmpty() const { return m_commands.empty(); }
+
     protected:
         virtual void doExecute();
         virtual void doUndo();

Index: qt/main_window.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/main_window.cpp,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -b -r1.17 -r1.18
--- qt/main_window.cpp  22 Nov 2008 14:40:27 -0000      1.17
+++ qt/main_window.cpp  23 Nov 2008 16:55:29 -0000      1.18
@@ -86,6 +86,9 @@
 
     QObject::connect(this, SIGNAL(gameChanged(const Game*)),
                      this, SLOT(updateForGame(const Game*)));
+    QObject::connect(this, SIGNAL(gameUpdated()),
+                     this, SLOT(refresh()));
+    refresh();
 
     // Status bar
     statusBar()->addWidget(new QLabel, 1);
@@ -150,6 +153,15 @@
     emit gameChangedNonConst(NULL);
     emit gameChanged(NULL);
 
+    QObject::connect(m_ui.buttonFirst, SIGNAL(clicked()),
+                     this, SLOT(onGameFirst()));
+    QObject::connect(m_ui.buttonPrev, SIGNAL(clicked()),
+                     this, SLOT(onGamePrev()));
+    QObject::connect(m_ui.buttonNext, SIGNAL(clicked()),
+                     this, SLOT(onGameNext()));
+    QObject::connect(m_ui.buttonLast, SIGNAL(clicked()),
+                     this, SLOT(onGameLast()));
+
     // Load dictionary
     QSettings qs(ORGANIZATION, PACKAGE_NAME);
     QString dicPath = qs.value(PrefsDialog::kINTF_DIC_PATH, "").toString();
@@ -197,6 +209,31 @@
 }
 
 
+void MainWindow::refresh()
+{
+    if (m_game == NULL)
+    {
+        m_ui.buttonFirst->setEnabled(false);
+        m_ui.buttonPrev->setEnabled(false);
+        m_ui.buttonNext->setEnabled(false);
+        m_ui.buttonLast->setEnabled(false);
+        // XXX: tmp
+        m_ui.labelTurnNb->setText("");
+    }
+    else
+    {
+        bool isFirstTurn = m_game->getNavigation().isFirstTurn();
+        bool isLastTurn = m_game->getNavigation().isLastTurn();
+        m_ui.buttonFirst->setEnabled(!isFirstTurn);
+        m_ui.buttonPrev->setEnabled(!isFirstTurn);
+        m_ui.buttonNext->setEnabled(!isLastTurn);
+        m_ui.buttonLast->setEnabled(!isLastTurn);
+        // XXX: tmp
+        m_ui.labelTurnNb->setText(QString("Turn: 
%1").arg(m_game->getNavigation().getCurrTurn()));
+    }
+}
+
+
 void MainWindow::updateForGame(const Game *iGame)
 {
     if (iGame == NULL)
@@ -715,3 +752,43 @@
     aboutBox->exec();
 }
 
+
+void MainWindow::onGameFirst()
+{
+    if (m_game == NULL)
+        return;
+
+    m_game->accessNavigation().firstTurn();
+    emit gameUpdated();
+}
+
+
+void MainWindow::onGamePrev()
+{
+    if (m_game == NULL)
+        return;
+
+    m_game->accessNavigation().prevTurn();
+    emit gameUpdated();
+}
+
+
+void MainWindow::onGameNext()
+{
+    if (m_game == NULL)
+        return;
+
+    m_game->accessNavigation().nextTurn();
+    emit gameUpdated();
+}
+
+
+void MainWindow::onGameLast()
+{
+    if (m_game == NULL)
+        return;
+
+    m_game->accessNavigation().lastTurn();
+    emit gameUpdated();
+}
+

Index: qt/main_window.h
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/main_window.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- qt/main_window.h    6 Sep 2008 20:06:58 -0000       1.11
+++ qt/main_window.h    23 Nov 2008 16:55:29 -0000      1.12
@@ -73,6 +73,14 @@
     void onWindowsDicTools();
     void onHelpAbout();
 
+    void onGameFirst();
+    void onGamePrev();
+    void onGameNext();
+    void onGameLast();
+
+    /** Perform some updates when the game is updated */
+    void refresh();
+
     /**
      * Perform several updates when the game changes (title bar, status bar,
      * grey out some menu items, ...)

Index: qt/ui/main_window.ui
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/ui/main_window.ui,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- qt/ui/main_window.ui        6 Sep 2008 20:06:59 -0000       1.11
+++ qt/ui/main_window.ui        23 Nov 2008 16:55:29 -0000      1.12
@@ -17,16 +17,60 @@
     <normaloff>:/images/eliot.xpm</normaloff>:/images/eliot.xpm</iconset>
   </property>
   <widget class="QWidget" name="centralwidget" >
-   <property name="geometry" >
-    <rect>
-     <x>0</x>
-     <y>26</y>
-     <width>747</width>
-     <height>568</height>
-    </rect>
-   </property>
    <layout class="QVBoxLayout" >
     <item>
+     <layout class="QHBoxLayout" name="horizontalLayout" >
+      <item>
+       <widget class="QPushButton" name="buttonFirst" >
+        <property name="text" >
+         <string>First</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="buttonPrev" >
+        <property name="text" >
+         <string>Prev</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="buttonNext" >
+        <property name="text" >
+         <string>Next</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="buttonLast" >
+        <property name="text" >
+         <string>Last</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QLabel" name="labelTurnNb" >
+        <property name="text" >
+         <string>Turn: </string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <spacer name="horizontalSpacer" >
+        <property name="orientation" >
+         <enum>Qt::Horizontal</enum>
+        </property>
+        <property name="sizeHint" stdset="0" >
+         <size>
+          <width>40</width>
+          <height>20</height>
+         </size>
+        </property>
+       </spacer>
+      </item>
+     </layout>
+    </item>
+    <item>
      <layout class="QHBoxLayout" >
       <item>
        <widget class="QGroupBox" name="groupBoxTest" >
@@ -72,20 +116,11 @@
      <x>0</x>
      <y>0</y>
      <width>747</width>
-     <height>26</height>
-    </rect>
-   </property>
-  </widget>
-  <widget class="QStatusBar" name="statusbar" >
-   <property name="geometry" >
-    <rect>
-     <x>0</x>
-     <y>594</y>
-     <width>747</width>
-     <height>23</height>
+     <height>25</height>
     </rect>
    </property>
   </widget>
+  <widget class="QStatusBar" name="statusbar" />
   <action name="action_WindowsBag" >
    <property name="checkable" >
     <bool>true</bool>
@@ -146,4 +181,5 @@
  <resources>
   <include location="../eliot.qrc" />
  </resources>
+ <connections/>
 </ui>

Index: utils/eliottxt.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/utils/eliottxt.cpp,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -b -r1.27 -r1.28
--- utils/eliottxt.cpp  23 Nov 2008 08:33:18 -0000      1.27
+++ utils/eliottxt.cpp  23 Nov 2008 16:55:29 -0000      1.28
@@ -576,16 +576,16 @@
                             switch (token[0])
                             {
                                 case L'p':
-                                    iGame.prevTurn();
+                                    iGame.accessNavigation().prevTurn();
                                     break;
                                 case L'n':
-                                    iGame.nextTurn();
+                                    iGame.accessNavigation().nextTurn();
                                     break;
                                 case L'f':
-                                    iGame.firstTurn();
+                                    iGame.accessNavigation().firstTurn();
                                     break;
                                 case L'l':
-                                    iGame.lastTurn();
+                                    iGame.accessNavigation().lastTurn();
                                     break;
                             }
                         }
@@ -705,16 +705,16 @@
                             switch (token[0])
                             {
                                 case L'p':
-                                    iGame.prevTurn();
+                                    iGame.accessNavigation().prevTurn();
                                     break;
                                 case L'n':
-                                    iGame.nextTurn();
+                                    iGame.accessNavigation().nextTurn();
                                     break;
                                 case L'f':
-                                    iGame.firstTurn();
+                                    iGame.accessNavigation().firstTurn();
                                     break;
                                 case L'l':
-                                    iGame.lastTurn();
+                                    iGame.accessNavigation().lastTurn();
                                     break;
                             }
                         }
@@ -842,16 +842,16 @@
                             switch (token[0])
                             {
                                 case L'p':
-                                    iGame.prevTurn();
+                                    iGame.accessNavigation().prevTurn();
                                     break;
                                 case L'n':
-                                    iGame.nextTurn();
+                                    iGame.accessNavigation().nextTurn();
                                     break;
                                 case L'f':
-                                    iGame.firstTurn();
+                                    iGame.accessNavigation().firstTurn();
                                     break;
                                 case L'l':
-                                    iGame.lastTurn();
+                                    iGame.accessNavigation().lastTurn();
                                     break;
                             }
                         }
@@ -1120,9 +1120,3 @@
     return 0;
 }
 
-/// Local Variables:
-/// mode: c++
-/// mode: hs-minor
-/// c-basic-offset: 4
-/// indent-tabs-mode: nil
-/// End:

Index: game/navigation.cpp
===================================================================
RCS file: game/navigation.cpp
diff -N game/navigation.cpp
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ game/navigation.cpp 23 Nov 2008 16:55:28 -0000      1.1
@@ -0,0 +1,118 @@
+/*******************************************************************
+ * Eliot
+ * Copyright (C) 2008 Olivier Teulière
+ * Authors: Olivier Teulière <ipkiss @@ gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *****************************************************************************/
+
+#include <boost/foreach.hpp>
+
+#include "navigation.h"
+#include "turn_cmd.h"
+#include "debug.h"
+
+
+Navigation::Navigation()
+    : m_currTurn(0)
+{
+    newTurn();
+}
+
+
+Navigation::~Navigation()
+{
+    BOOST_FOREACH(Command *c, m_turnCommands)
+    {
+        delete c;
+    }
+}
+
+
+void Navigation::newTurn()
+{
+    lastTurn();
+    m_turnCommands.push_back(new TurnCmd);
+    ++m_currTurn;
+}
+
+
+void Navigation::addAndExecute(Command *iCmd)
+{
+    ASSERT(isLastTurn(), "Trying to add a command to an old turn!");
+    ASSERT(m_currTurn >= 1, "Bug in the turns vector");
+    ASSERT(m_currTurn - 1 < m_turnCommands.size(), "Bug in the turns vector");
+    m_turnCommands[m_currTurn - 1]->addAndExecute(iCmd);
+}
+
+
+void Navigation::prevTurn()
+{
+    if (m_currTurn > 0)
+    {
+        --m_currTurn;
+        m_turnCommands[m_currTurn]->undo();
+    }
+}
+
+
+void Navigation::nextTurn()
+{
+    if (m_currTurn < m_turnCommands.size())
+    {
+        m_turnCommands[m_currTurn]->execute();
+        ++m_currTurn;
+    }
+}
+
+
+void Navigation::firstTurn()
+{
+    while (m_currTurn > 0)
+    {
+        prevTurn();
+    }
+}
+
+
+void Navigation::lastTurn()
+{
+    while (m_currTurn < m_turnCommands.size())
+    {
+        nextTurn();
+    }
+}
+
+
+void Navigation::clearFuture()
+{
+    // When there is no future, don't do anything
+    if (isLastTurn())
+        return;
+
+    for (unsigned int i = m_currTurn; i < m_turnCommands.size(); ++i)
+    {
+        delete m_turnCommands[i];
+    }
+    while (m_turnCommands.size() > m_currTurn)
+    {
+        m_turnCommands.pop_back();
+    }
+    newTurn();
+    // Sanity check
+    ASSERT(isLastTurn(),
+           "After removing the next turns, we should be at the last turn");
+}
+

Index: game/navigation.h
===================================================================
RCS file: game/navigation.h
diff -N game/navigation.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ game/navigation.h   23 Nov 2008 16:55:29 -0000      1.1
@@ -0,0 +1,62 @@
+/*******************************************************************
+ * Eliot
+ * Copyright (C) 2008 Olivier Teulière
+ * Authors: Olivier Teulière <ipkiss @@ gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *****************************************************************************/
+
+#ifndef _NAVIGATION_H
+#define _NAVIGATION_H
+
+#include <vector>
+
+class TurnCmd;
+class Command;
+
+using namespace std;
+
+
+class Navigation
+{
+    public:
+        Navigation();
+        ~Navigation();
+
+        void newTurn();
+        void addAndExecute(Command *iCmd);
+
+        unsigned int getCurrTurn() const { return m_currTurn; }
+        bool isFirstTurn() const { return m_currTurn == 0; }
+        bool isLastTurn() const { return m_currTurn == m_turnCommands.size(); }
+
+        void firstTurn();
+        void prevTurn();
+        void nextTurn();
+        void lastTurn();
+        /**
+         * Get rid of the future turns of the game, the current turn
+         * becoming the last one.
+         */
+        void clearFuture();
+
+    private:
+        vector<TurnCmd *> m_turnCommands;
+        unsigned int m_currTurn;
+
+};
+
+#endif
+




reply via email to

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