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

This commit is contained in:
Olivier Teulière 2008-11-23 17:07:42 +00:00
parent e8cd922186
commit 8e7c61e2d8
4 changed files with 81 additions and 6 deletions

View file

@ -126,7 +126,7 @@ void FreeGame::start()
accessNavigation().addAndExecute(pCmd);
}
m_currPlayer = 0;
firstPlayer();
accessNavigation().newTurn();

View file

@ -22,7 +22,6 @@
#define _FREEGAME_H_
#include "game.h"
#include "tile.h"
class Player;

View file

@ -20,6 +20,7 @@
*****************************************************************************/
#include <boost/foreach.hpp>
#include <sstream>
#if ENABLE_NLS
# include <libintl.h>
@ -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();
}

View file

@ -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();