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_move_cmd.h"
#include "player_rack_cmd.h" #include "player_rack_cmd.h"
#include "game_move_cmd.h" #include "game_move_cmd.h"
#include "game_rack_cmd.h"
#include "mark_played_cmd.h" #include "mark_played_cmd.h"
#include "master_move_cmd.h" #include "master_move_cmd.h"
#include "ai_player.h" #include "ai_player.h"
@ -120,11 +121,14 @@ void Duplicate::start()
// Arbitrary player, since they should all have the same rack // Arbitrary player, since they should all have the same rack
m_currPlayer = 0; m_currPlayer = 0;
// Complete the rack for the player that just played // Complete the racks
try try
{ {
const PlayedRack &newRack = 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 // All the players have the same rack
BOOST_FOREACH(Player *player, m_players) BOOST_FOREACH(Player *player, m_players)
{ {

View file

@ -37,6 +37,7 @@
#include "player_move_cmd.h" #include "player_move_cmd.h"
#include "player_rack_cmd.h" #include "player_rack_cmd.h"
#include "game_move_cmd.h" #include "game_move_cmd.h"
#include "game_rack_cmd.h"
#include "ai_player.h" #include "ai_player.h"
#include "settings.h" #include "settings.h"
#include "turn.h" #include "turn.h"
@ -136,6 +137,10 @@ void FreeGame::start()
accessNavigation().addAndExecute(pCmd); 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(); firstPlayer();
// If the first player is an AI, make it play now // If the first player is an AI, make it play now
@ -167,9 +172,8 @@ int FreeGame::endTurn()
{ {
const PlayedRack &newRack = const PlayedRack &newRack =
helperSetRackRandom(getCurrentPlayer().getCurrentRack(), false, RACK_NEW); helperSetRackRandom(getCurrentPlayer().getCurrentRack(), false, RACK_NEW);
Command *pCmd = new PlayerRackCmd(*m_players[m_currPlayer], Command *pCmd2 = new PlayerRackCmd(*m_players[m_currPlayer], newRack);
newRack); accessNavigation().addAndExecute(pCmd2);
accessNavigation().addAndExecute(pCmd);
} }
catch (EndGameException &e) catch (EndGameException &e)
{ {
@ -182,6 +186,10 @@ int FreeGame::endTurn()
// Next player // Next player
nextPlayer(); nextPlayer();
// Set the game rack to the rack of the current player
Command *pCmd3 = new GameRackCmd(*this, getCurrentPlayer().getCurrentRack());
accessNavigation().addAndExecute(pCmd3);
accessNavigation().newTurn(); accessNavigation().newTurn();
// If this player is an AI, make it play now // 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, GameMoveCmd::GameMoveCmd(Game &ioGame, const Move &iMove,
unsigned int iPlayerId) unsigned int iPlayerId)
: m_game(ioGame), m_move(iMove), : m_game(ioGame), m_move(iMove),
m_moveRack(ioGame.getPlayer(iPlayerId).getHistory().getPreviousTurn().getPlayedRack()), m_moveRack(ioGame.getHistory().getCurrentRack()),
m_playerId(iPlayerId) m_playerId(iPlayerId)
{ {
setAutoExecutable(false); setAutoExecutable(false);
@ -48,7 +48,6 @@ void GameMoveCmd::doExecute()
// History of the game // History of the game
History &history = m_game.accessHistory(); History &history = m_game.accessHistory();
history.setCurrentRack(m_moveRack);
history.playMove(m_playerId, m_move, newRack); history.playMove(m_playerId, m_move, newRack);
// Points // Points

View file

@ -34,7 +34,7 @@ GameRackCmd::GameRackCmd(Game &ioGame, const PlayedRack &iNewRack)
void GameRackCmd::doExecute() void GameRackCmd::doExecute()
{ {
// Get what was the rack for the current turn // 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 // Update the game rack
m_game.accessHistory().setCurrentRack(m_newRack); m_game.accessHistory().setCurrentRack(m_newRack);
} }

View file

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

View file

@ -33,6 +33,7 @@
#include "player.h" #include "player.h"
#include "ai_percent.h" #include "ai_percent.h"
#include "encoding.h" #include "encoding.h"
#include "game_rack_cmd.h"
#include "game_move_cmd.h" #include "game_move_cmd.h"
#include "player_rack_cmd.h" #include "player_rack_cmd.h"
#include "player_move_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); 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(); m_attributes.clear();
for (int i = 0; i < atts.getLength(); ++i) for (int i = 0; i < atts.getLength(); ++i)
@ -284,6 +286,23 @@ void XmlReader::endElement(const string& namespaceURI,
m_game->accessNavigation().newTurn(); 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") else if (tag == "PlayerRack")
{ {
// Build a rack for the correct player // Build a rack for the correct player

View file

@ -32,6 +32,7 @@
#include "ai_percent.h" #include "ai_percent.h"
#include "game_exception.h" #include "game_exception.h"
#include "turn_cmd.h" #include "turn_cmd.h"
#include "game_rack_cmd.h"
#include "game_move_cmd.h" #include "game_move_cmd.h"
#include "player_rack_cmd.h" #include "player_rack_cmd.h"
#include "player_move_cmd.h" #include "player_move_cmd.h"
@ -165,7 +166,14 @@ void XmlWriter::write(const Game &iGame, const string &iFileName)
addIndent(indent); addIndent(indent);
BOOST_FOREACH(const Command *cmd, turn->getCommands()) 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); const PlayerRackCmd *rackCmd = static_cast<const PlayerRackCmd*>(cmd);
unsigned int id = rackCmd->getPlayer().getId() + 1; unsigned int id = rackCmd->getPlayer().getId() + 1;

View file

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