From 8e7c61e2d89dead63bb110f2432d923a1ceed88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sun, 23 Nov 2008 17:07:42 +0000 Subject: [PATCH] New command to handle changes of the current player. This fixes history navigation in free game mode --- game/freegame.cpp | 2 +- game/freegame.h | 1 - game/game.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++---- game/game.h | 24 +++++++++++++++++++ 4 files changed, 81 insertions(+), 6 deletions(-) diff --git a/game/freegame.cpp b/game/freegame.cpp index 7abcc45..037bde5 100644 --- a/game/freegame.cpp +++ b/game/freegame.cpp @@ -126,7 +126,7 @@ void FreeGame::start() accessNavigation().addAndExecute(pCmd); } - m_currPlayer = 0; + firstPlayer(); accessNavigation().newTurn(); diff --git a/game/freegame.h b/game/freegame.h index 6527aee..bd1fe9f 100644 --- a/game/freegame.h +++ b/game/freegame.h @@ -22,7 +22,6 @@ #define _FREEGAME_H_ #include "game.h" -#include "tile.h" class Player; diff --git a/game/game.cpp b/game/game.cpp index b1ca3ed..c3c4b62 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -20,6 +20,7 @@ *****************************************************************************/ #include +#include #if ENABLE_NLS # include @@ -458,14 +459,29 @@ void Game::addPlayer(Player *iPlayer) } +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 @@ void Game::nextPlayer() { 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 @@ int Game::checkPlayedWord(const wstring &iCoord, 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(); +} + diff --git a/game/game.h b/game/game.h index e2bfc74..7e5c242 100644 --- a/game/game.h +++ b/game/game.h @@ -29,6 +29,7 @@ #include "board.h" #include "history.h" #include "navigation.h" +#include "command.h" class Dictionary; class Player; @@ -237,6 +238,28 @@ private: 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 @@ protected: */ int helperSetRackManual(unsigned int p, bool iCheck, const wstring &iLetters); + void firstPlayer(); void prevPlayer(); void nextPlayer();