Use the GameMoveCmd command

This commit is contained in:
Olivier Teulière 2012-02-25 23:16:42 +01:00
parent df806594ae
commit 4077a25fa0
8 changed files with 64 additions and 20 deletions

View file

@ -42,6 +42,7 @@
#include "player_move_cmd.h"
#include "player_rack_cmd.h"
#include "game_move_cmd.h"
#include "game_rack_cmd.h"
#include "mark_played_cmd.h"
#include "master_move_cmd.h"
#include "ai_player.h"
@ -120,11 +121,14 @@ void Duplicate::start()
// Arbitrary player, since they should all have the same rack
m_currPlayer = 0;
// Complete the rack for the player that just played
// Complete the racks
try
{
const PlayedRack &newRack =
helperSetRackRandom(getCurrentPlayer().getCurrentRack(), true, RACK_NEW);
helperSetRackRandom(getHistory().getCurrentRack(), true, RACK_NEW);
// Set the game rack
Command *pCmd = new GameRackCmd(*this, newRack);
accessNavigation().addAndExecute(pCmd);
// All the players have the same rack
BOOST_FOREACH(Player *player, m_players)
{

View file

@ -37,6 +37,7 @@
#include "player_move_cmd.h"
#include "player_rack_cmd.h"
#include "game_move_cmd.h"
#include "game_rack_cmd.h"
#include "ai_player.h"
#include "settings.h"
#include "turn.h"
@ -136,6 +137,10 @@ void FreeGame::start()
accessNavigation().addAndExecute(pCmd);
}
// Set the game rack to the rack of the current player
Command *pCmd = new GameRackCmd(*this, getPlayer(0).getCurrentRack());
accessNavigation().addAndExecute(pCmd);
firstPlayer();
// If the first player is an AI, make it play now
@ -167,9 +172,8 @@ int FreeGame::endTurn()
{
const PlayedRack &newRack =
helperSetRackRandom(getCurrentPlayer().getCurrentRack(), false, RACK_NEW);
Command *pCmd = new PlayerRackCmd(*m_players[m_currPlayer],
newRack);
accessNavigation().addAndExecute(pCmd);
Command *pCmd2 = new PlayerRackCmd(*m_players[m_currPlayer], newRack);
accessNavigation().addAndExecute(pCmd2);
}
catch (EndGameException &e)
{
@ -182,6 +186,10 @@ int FreeGame::endTurn()
// Next player
nextPlayer();
// Set the game rack to the rack of the current player
Command *pCmd3 = new GameRackCmd(*this, getCurrentPlayer().getCurrentRack());
accessNavigation().addAndExecute(pCmd3);
accessNavigation().newTurn();
// If this player is an AI, make it play now

View file

@ -34,7 +34,7 @@ INIT_LOGGER(game, GameMoveCmd);
GameMoveCmd::GameMoveCmd(Game &ioGame, const Move &iMove,
unsigned int iPlayerId)
: m_game(ioGame), m_move(iMove),
m_moveRack(ioGame.getPlayer(iPlayerId).getHistory().getPreviousTurn().getPlayedRack()),
m_moveRack(ioGame.getHistory().getCurrentRack()),
m_playerId(iPlayerId)
{
setAutoExecutable(false);
@ -48,7 +48,6 @@ void GameMoveCmd::doExecute()
// History of the game
History &history = m_game.accessHistory();
history.setCurrentRack(m_moveRack);
history.playMove(m_playerId, m_move, newRack);
// Points

View file

@ -34,7 +34,7 @@ GameRackCmd::GameRackCmd(Game &ioGame, const PlayedRack &iNewRack)
void GameRackCmd::doExecute()
{
// Get what was the rack for the current turn
m_oldRack = m_game.accessHistory().getCurrentRack();
m_oldRack = m_game.getHistory().getCurrentRack();
// Update the game rack
m_game.accessHistory().setCurrentRack(m_newRack);
}

View file

@ -42,6 +42,7 @@
#include "player_move_cmd.h"
#include "player_rack_cmd.h"
#include "game_move_cmd.h"
#include "game_rack_cmd.h"
#include "encoding.h"
#include "debug.h"
@ -60,10 +61,13 @@ void Training::setRackRandom(bool iCheck, set_rack_mode mode)
{
m_results.clear();
const PlayedRack &newRack =
helperSetRackRandom(getCurrentPlayer().getCurrentRack(), iCheck, mode);
Command *pCmd = new PlayerRackCmd(*m_players[m_currPlayer], newRack);
pCmd->setHumanIndependent(false);
accessNavigation().addAndExecute(pCmd);
helperSetRackRandom(getHistory().getCurrentRack(), iCheck, mode);
Command *pCmd1 = new GameRackCmd(*this, newRack);
pCmd1->setHumanIndependent(false);
accessNavigation().addAndExecute(pCmd1);
Command *pCmd2 = new PlayerRackCmd(*m_players[m_currPlayer], newRack);
pCmd2->setHumanIndependent(false);
accessNavigation().addAndExecute(pCmd2);
}
@ -77,9 +81,12 @@ void Training::setRackManual(bool iCheck, const wstring &iLetters)
std::transform(upperLetters.begin(), upperLetters.end(),
upperLetters.begin(), towupper);
const PlayedRack &newRack = helperSetRackManual(iCheck, upperLetters);
Command *pCmd = new PlayerRackCmd(*m_players[m_currPlayer], newRack);
pCmd->setHumanIndependent(false);
accessNavigation().addAndExecute(pCmd);
Command *pCmd1 = new GameRackCmd(*this, newRack);
pCmd1->setHumanIndependent(false);
accessNavigation().addAndExecute(pCmd1);
Command *pCmd2 = new PlayerRackCmd(*m_players[m_currPlayer], newRack);
pCmd2->setHumanIndependent(false);
accessNavigation().addAndExecute(pCmd2);
// Clear the results if everything went well
m_results.clear();
}
@ -150,8 +157,7 @@ void Training::endTurn()
void Training::search()
{
// Search for the current player
const Rack &rack =
m_players[m_currPlayer]->getCurrentRack().getRack();
const Rack &rack = getHistory().getCurrentRack().getRack();
int limit = Settings::Instance().getInt("training.search-limit");
m_results.setLimit(limit);
m_results.search(getDic(), getBoard(), rack, getHistory().beforeFirstRound());

View file

@ -33,6 +33,7 @@
#include "player.h"
#include "ai_percent.h"
#include "encoding.h"
#include "game_rack_cmd.h"
#include "game_move_cmd.h"
#include "player_rack_cmd.h"
#include "player_move_cmd.h"
@ -185,7 +186,8 @@ void XmlReader::startElement(const string& namespaceURI,
m_attributes[atts.getLocalName(i)] = atts.getValue(i);
}
}
else if (tag == "PlayerRack" || tag == "PlayerMove" || tag == "GameMove")
else if (tag == "GameRack" || tag == "PlayerRack" ||
tag == "PlayerMove" || tag == "GameMove")
{
m_attributes.clear();
for (int i = 0; i < atts.getLength(); ++i)
@ -284,6 +286,23 @@ void XmlReader::endElement(const string& namespaceURI,
m_game->accessNavigation().newTurn();
}
else if (tag == "GameRack")
{
// Build a rack for the correct player
const wstring &rackStr = m_dic.convertFromInput(fromUtf8(m_data));
PlayedRack pldrack;
if (!m_dic.validateLetters(rackStr, L"-+"))
{
throw LoadGameException("Rack invalid for the current dictionary: " + m_data);
}
pldrack.setManual(rackStr);
LOG_DEBUG("loaded rack: " << lfw(pldrack.toString()));
GameRackCmd *cmd = new GameRackCmd(*m_game, pldrack);
m_game->accessNavigation().addAndExecute(cmd);
LOG_DEBUG("rack: " << lfw(pldrack.toString()));
}
else if (tag == "PlayerRack")
{
// Build a rack for the correct player

View file

@ -32,6 +32,7 @@
#include "ai_percent.h"
#include "game_exception.h"
#include "turn_cmd.h"
#include "game_rack_cmd.h"
#include "game_move_cmd.h"
#include "player_rack_cmd.h"
#include "player_move_cmd.h"
@ -165,7 +166,14 @@ void XmlWriter::write(const Game &iGame, const string &iFileName)
addIndent(indent);
BOOST_FOREACH(const Command *cmd, turn->getCommands())
{
if (dynamic_cast<const PlayerRackCmd*>(cmd))
if (dynamic_cast<const GameRackCmd*>(cmd))
{
const GameRackCmd *rackCmd = static_cast<const GameRackCmd*>(cmd);
out << indent << "<GameRack>"
<< toUtf8(rackCmd->getRack().toString())
<< "</GameRack>" << endl;
}
else if (dynamic_cast<const PlayerRackCmd*>(cmd))
{
const PlayerRackCmd *rackCmd = static_cast<const PlayerRackCmd*>(cmd);
unsigned int id = rackCmd->getPlayer().getId() + 1;

View file

@ -255,7 +255,7 @@ void MainWindow::refresh()
emit turnChanged(currTurn, isLastTurn);
}
#ifdef DEBUG
//m_game->printTurns();
m_game->printTurns();
#endif
}
}