From 4077a25fa0e21dc15b5a7711bbcf74069b6f4349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sat, 25 Feb 2012 23:16:42 +0100 Subject: [PATCH] Use the GameMoveCmd command --- game/duplicate.cpp | 8 ++++++-- game/freegame.cpp | 14 +++++++++++--- game/game_move_cmd.cpp | 3 +-- game/game_rack_cmd.cpp | 2 +- game/training.cpp | 24 +++++++++++++++--------- game/xml_reader.cpp | 21 ++++++++++++++++++++- game/xml_writer.cpp | 10 +++++++++- qt/main_window.cpp | 2 +- 8 files changed, 64 insertions(+), 20 deletions(-) diff --git a/game/duplicate.cpp b/game/duplicate.cpp index 287d693..b650711 100644 --- a/game/duplicate.cpp +++ b/game/duplicate.cpp @@ -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) { diff --git a/game/freegame.cpp b/game/freegame.cpp index f94337f..f51ffc8 100644 --- a/game/freegame.cpp +++ b/game/freegame.cpp @@ -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 diff --git a/game/game_move_cmd.cpp b/game/game_move_cmd.cpp index c61ea98..96a335d 100644 --- a/game/game_move_cmd.cpp +++ b/game/game_move_cmd.cpp @@ -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 diff --git a/game/game_rack_cmd.cpp b/game/game_rack_cmd.cpp index 3b8912e..1abc315 100644 --- a/game/game_rack_cmd.cpp +++ b/game/game_rack_cmd.cpp @@ -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); } diff --git a/game/training.cpp b/game/training.cpp index fb3a785..7154c04 100644 --- a/game/training.cpp +++ b/game/training.cpp @@ -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()); diff --git a/game/xml_reader.cpp b/game/xml_reader.cpp index ab8cf2f..14983fb 100644 --- a/game/xml_reader.cpp +++ b/game/xml_reader.cpp @@ -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 diff --git a/game/xml_writer.cpp b/game/xml_writer.cpp index 68b7858..f7514f8 100644 --- a/game/xml_writer.cpp +++ b/game/xml_writer.cpp @@ -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(cmd)) + if (dynamic_cast(cmd)) + { + const GameRackCmd *rackCmd = static_cast(cmd); + out << indent << "" + << toUtf8(rackCmd->getRack().toString()) + << "" << endl; + } + else if (dynamic_cast(cmd)) { const PlayerRackCmd *rackCmd = static_cast(cmd); unsigned int id = rackCmd->getPlayer().getId() + 1; diff --git a/qt/main_window.cpp b/qt/main_window.cpp index 9cf234f..9a4d725 100644 --- a/qt/main_window.cpp +++ b/qt/main_window.cpp @@ -255,7 +255,7 @@ void MainWindow::refresh() emit turnChanged(currTurn, isLastTurn); } #ifdef DEBUG - //m_game->printTurns(); + m_game->printTurns(); #endif } }