[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    kdegames/kreversi [POSSIBLY UNSAFE]
From:       Inge Wallin <inge () lysator ! liu ! se>
Date:       2004-10-03 11:23:03
Message-ID: 20041003112303.A478D9910 () office ! kde ! org
[Download RAW message or body]

CVS commit by ingwa: 

Simplify saving of the game: Use the possibilities of the improved
Game object, and don't reload the game immediately after saving it.


  M +7 -0      ChangeLog   1.43
  M +13 -0     Game.cpp   1.11
  M +5 -4      Game.h   1.11
  M +15 -17    kreversi.cpp   1.31 [POSSIBLY UNSAFE: printf]


--- kdegames/kreversi/ChangeLog  #1.42:1.43
@@ -1,2 +1,9 @@
+2004-10-03  Inge Wallin  <inge@lysator.liu.se>
+
+        Simplify saving of the game
+        * Game.{h,cpp} (move(uint)): New method.
+        * kreversi.cpp (saveGame): Use the new method, and don't call
+        loadGame to restore the Game object.
+
 2004-09-29  Inge Wallin  <inge@lysator.liu.se>
 

--- kdegames/kreversi/Game.cpp  #1.10:1.11
@@ -98,4 +98,6 @@
 
 
+#include <assert.h>
+
 #include "Game.h"
 
@@ -151,4 +154,14 @@ Move Game::lastMove() const 
 
 
+Move
+Game::move(uint moveNo) const
+{
+  assert(moveNo < m_moveNumber);
+
+  return m_moves[moveNo];
+}
+
+
+
 // Return true if the move is legal in the current position.
 //

--- kdegames/kreversi/Game.h  #1.10:1.11
@@ -116,4 +116,5 @@ public:
   uint   score(Color color) const;
   Move   lastMove() const;
+  Move   move(uint moveNo)     const;
 
   bool   moveIsLegal(Move &move) const;

--- kdegames/kreversi/kreversi.cpp  #1.30:1.31
@@ -632,29 +632,27 @@ void KReversi::saveGame(KConfig *config)
 
   // Write the data to the config file.
-  config->writeEntry("NumberOfMoves", moveNumber());
   config->writeEntry("State", state());
   config->writeEntry("Strength", strength());
+  config->writeEntry("Competitive",   (int) m_competitiveGame);
+  config->writeEntry("HumanColor",    (int) m_humanColor);
 
   // Write the moves of the game to the config object.  This object
   // saves itself all at once so we don't have to write the moves
   // to the file ourselves.
-  for (uint i = moveNumber(); i > 0; i--) {
-    Move  move = m_game->lastMove();
-    m_game->takeBackMove();
+  config->writeEntry("NumberOfMoves", moveNumber());
+  for (uint i = 0; i < moveNumber(); i++) {
+    Move  move = m_game->move(i);
 
-    QString s, idx;
-    s.sprintf("%d %d %d", move.x(), move.y(), (int)move.color());
-    idx.sprintf("Move_%d", i);
-    config->writeEntry(idx, s);
+    QString  moveString;
+    QString  idx;
+
+    moveString.sprintf("%d %d %d", move.x(), move.y(), (int) move.color());
+    idx.sprintf("Move_%d", i + 1);
+    config->writeEntry(idx, moveString);
   }
 
-  // Save whose turn it is and if the game is competitive.
-  config->writeEntry("Competitive", (int) m_competitiveGame);
-  config->writeEntry("HumanColor",  (int) m_humanColor);
+  // Actually write the data to file.
   config->sync();
 
-  // All moves must be redone.
-  loadGame(config, TRUE);
-
   // Continue with the move if applicable.
   slotContinue(); 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic