Arbitration: improved the refresh of search results

This commit is contained in:
Olivier Teulière 2012-03-17 23:45:47 +01:00
parent 3a8ac20a0a
commit f3b2442cc0
6 changed files with 37 additions and 50 deletions

View file

@ -27,6 +27,7 @@
#include "player.h" #include "player.h"
#include "turn_cmd.h" #include "turn_cmd.h"
#include "game_rack_cmd.h" #include "game_rack_cmd.h"
#include "results.h"
#include "settings.h" #include "settings.h"
#include "encoding.h" #include "encoding.h"
#include "debug.h" #include "debug.h"
@ -36,7 +37,7 @@ INIT_LOGGER(game, Arbitration);
Arbitration::Arbitration(const GameParams &iParams) Arbitration::Arbitration(const GameParams &iParams)
: Duplicate(iParams), m_results(1000) : Duplicate(iParams)
{ {
} }
@ -48,9 +49,6 @@ void Arbitration::setRackRandom()
const PlayedRack &newRack = const PlayedRack &newRack =
helperSetRackRandom(getHistory().getCurrentRack(), true, RACK_NEW); helperSetRackRandom(getHistory().getCurrentRack(), true, RACK_NEW);
setGameAndPlayersRack(newRack); setGameAndPlayersRack(newRack);
// Clear the results if everything went well
m_results.clear();
} }
@ -67,20 +65,17 @@ void Arbitration::setRackManual(const wstring &iLetters)
upperLetters.begin(), towupper); upperLetters.begin(), towupper);
const PlayedRack &newRack = helperSetRackManual(false, upperLetters); const PlayedRack &newRack = helperSetRackManual(false, upperLetters);
setGameAndPlayersRack(newRack); setGameAndPlayersRack(newRack);
// Clear the results if everything went well
m_results.clear();
} }
void Arbitration::search() void Arbitration::search(LimitResults &oResults)
{ {
const Rack &rack = getHistory().getCurrentRack().getRack(); const Rack &rack = getHistory().getCurrentRack().getRack();
LOG_DEBUG("Performing search for rack " + lfw(rack.toString())); LOG_DEBUG("Performing search for rack " + lfw(rack.toString()));
int limit = Settings::Instance().getInt("arbitration.search-limit"); int limit = Settings::Instance().getInt("arbitration.search-limit");
m_results.setLimit(limit); oResults.setLimit(limit);
m_results.search(getDic(), getBoard(), rack, getHistory().beforeFirstRound()); oResults.search(getDic(), getBoard(), rack, getHistory().beforeFirstRound());
LOG_DEBUG("Found " << m_results.size() << " results"); LOG_DEBUG("Found " << oResults.size() << " results");
} }
@ -114,8 +109,6 @@ void Arbitration::assignMove(unsigned int iPlayerId, const Move &iMove)
void Arbitration::finalizeTurn() void Arbitration::finalizeTurn()
{ {
m_results.clear();
// Assign a default empty move to the human players which have // Assign a default empty move to the human players which have
// not played yet, to be able to end the turn. // not played yet, to be able to end the turn.
BOOST_FOREACH(Player *player, m_players) BOOST_FOREACH(Player *player, m_players)

View file

@ -22,10 +22,11 @@
#define ARBITRATION_H_ #define ARBITRATION_H_
#include "duplicate.h" #include "duplicate.h"
#include "results.h"
#include "logging.h" #include "logging.h"
class LimitResults;
/** /**
* This class simply extends the Duplicate game, * This class simply extends the Duplicate game,
* to specialize it for arbitration purposes. * to specialize it for arbitration purposes.
@ -49,8 +50,7 @@ public:
*/ */
void setRackManual(const wstring &iLetters); void setRackManual(const wstring &iLetters);
void search(); void search(LimitResults &oResults);
const Results& getResults() const { return m_results; }
Move checkWord(const wstring &iWord, const wstring &iCoords) const; Move checkWord(const wstring &iWord, const wstring &iCoords) const;
@ -64,9 +64,6 @@ private:
/// Undo the current rack, and subsequent commands /// Undo the current rack, and subsequent commands
void undoCurrentRack(); void undoCurrentRack();
/// Search results, with all the possible rounds up to a predefined limit
LimitResults m_results;
}; };
#endif /* ARBITRATION_H_ */ #endif /* ARBITRATION_H_ */

View file

@ -249,17 +249,12 @@ void PublicGame::arbitrationSetRackManual(const wstring &iLetters)
getTypedGame<Arbitration>(m_game).setRackManual(iLetters); getTypedGame<Arbitration>(m_game).setRackManual(iLetters);
} }
void PublicGame::arbitrationSearch() void PublicGame::arbitrationSearch(LimitResults &oResults)
{ {
return getTypedGame<Arbitration>(m_game).search(); return getTypedGame<Arbitration>(m_game).search(oResults);
} }
const Results & PublicGame::arbitrationGetResults() const
{
return getTypedGame<Arbitration>(m_game).getResults();
}
Move PublicGame::arbitrationCheckWord(const wstring &iWord, Move PublicGame::arbitrationCheckWord(const wstring &iWord,
const wstring &iCoords) const const wstring &iCoords) const
{ {

View file

@ -33,6 +33,7 @@ class Player;
class Navigation; class Navigation;
class Round; class Round;
class Results; class Results;
class LimitResults;
class Move; class Move;
using namespace std; using namespace std;
@ -256,8 +257,7 @@ public:
*/ */
void arbitrationSetRackManual(const wstring &iLetters); void arbitrationSetRackManual(const wstring &iLetters);
void arbitrationSearch(); void arbitrationSearch(LimitResults &oResults);
const Results& arbitrationGetResults() const;
Move arbitrationCheckWord(const wstring &iWord, Move arbitrationCheckWord(const wstring &iWord,
const wstring &iCoords) const; const wstring &iCoords) const;

View file

@ -58,7 +58,7 @@ INIT_LOGGER(qt, ArbitrationWidget);
ArbitrationWidget::ArbitrationWidget(QWidget *parent, ArbitrationWidget::ArbitrationWidget(QWidget *parent,
PublicGame *iGame, CoordModel &iCoordModel) PublicGame *iGame, CoordModel &iCoordModel)
: QWidget(parent), m_game(iGame), m_coordModel(iCoordModel) : QWidget(parent), m_game(iGame), m_coordModel(iCoordModel), m_results(10)
{ {
setupUi(this); setupUi(this);
@ -210,6 +210,15 @@ ArbitrationWidget::ArbitrationWidget(QWidget *parent,
void ArbitrationWidget::refresh() void ArbitrationWidget::refresh()
{ {
const PlayedRack &pldRack = m_game->getHistory().getCurrentRack();
// Update the rack only if needed, to avoid losing cursor position
QString qrack = qfw(pldRack.toString(PlayedRack::RACK_SIMPLE));
if (qrack != lineEditRack->text()) {
// Must be done before updateResultsModel(), because it will
// indirectly call the clearResults() slot
lineEditRack->setText(qrack);
}
updateResultsModel(); updateResultsModel();
updatePlayersModel(); updatePlayersModel();
@ -225,12 +234,6 @@ void ArbitrationWidget::refresh()
labelMasterMove->setText(label); labelMasterMove->setText(label);
} }
const PlayedRack &pldRack = m_game->getHistory().getCurrentRack();
// Update the rack only if needed, to avoid losing cursor position
QString qrack = qfw(pldRack.toString(PlayedRack::RACK_SIMPLE));
if (qrack != lineEditRack->text())
lineEditRack->setText(qrack);
if (m_game->isFinished()) if (m_game->isFinished())
{ {
setEnabled(false); setEnabled(false);
@ -244,8 +247,8 @@ void ArbitrationWidget::updateResultsModel()
// FIXME arbitration begin // FIXME arbitration begin
// Consider that there is nothing to do if the number of lines is correct // Consider that there is nothing to do if the number of lines is correct
// This avoids problems when the game is updated for a test play // This avoids problems when the game is updated for a test play
if (m_game != NULL && m_game->isLastTurn() && if (m_game != NULL &&
m_game->arbitrationGetResults().size() + m_addedMoves.size() == m_results.size() + m_addedMoves.size() ==
static_cast<unsigned int>(m_resultsModel->rowCount())) static_cast<unsigned int>(m_resultsModel->rowCount()))
{ {
return; return;
@ -254,16 +257,15 @@ void ArbitrationWidget::updateResultsModel()
#endif #endif
m_resultsModel->removeRows(0, m_resultsModel->rowCount()); m_resultsModel->removeRows(0, m_resultsModel->rowCount());
if (m_game == NULL || !m_game->isLastTurn()) if (m_game == NULL)
return; return;
// First step: add the search results (type TYPE_ROUND) // First step: add the search results (type TYPE_ROUND)
const Results &results = m_game->arbitrationGetResults();
// Find the highest score // Find the highest score
int bestScore = getBestScore(); int bestScore = getBestScore();
for (unsigned int i = 0; i < results.size(); ++i) for (unsigned int i = 0; i < m_results.size(); ++i)
{ {
Move move(results.get(i)); Move move(m_results.get(i));
addSingleMove(move, TYPE_ROUND, i, bestScore); addSingleMove(move, TYPE_ROUND, i, bestScore);
} }
@ -542,7 +544,8 @@ void ArbitrationWidget::on_buttonSearch_clicked()
{ {
m_game->removeTestRound(); m_game->removeTestRound();
emit notifyInfo(_q("Searching with rack '%1'...").arg(lineEditRack->text())); emit notifyInfo(_q("Searching with rack '%1'...").arg(lineEditRack->text()));
m_game->arbitrationSearch(); m_results.clear();
m_game->arbitrationSearch(m_results);
emit notifyInfo(_q("Search done")); emit notifyInfo(_q("Search done"));
emit gameUpdated(); emit gameUpdated();
@ -683,9 +686,8 @@ Move ArbitrationWidget::getSelectedMove() const
if (origin == TYPE_ROUND) if (origin == TYPE_ROUND)
{ {
unsigned int resNb = m_resultsModel->data(index, MOVE_INDEX_ROLE).toUInt(); unsigned int resNb = m_resultsModel->data(index, MOVE_INDEX_ROLE).toUInt();
const Results &results = m_game->arbitrationGetResults(); ASSERT(resNb < m_results.size(), "Wrong result number");
ASSERT(resNb < results.size(), "Wrong result number"); return Move(m_results.get(resNb));
return Move(results.get(resNb));
} }
else else
{ {
@ -719,12 +721,8 @@ QSet<unsigned int> ArbitrationWidget::getSelectedPlayers() const
void ArbitrationWidget::clearResults() void ArbitrationWidget::clearResults()
{ {
#if 0
// FIXME arbitration begin
m_game->removeTestRound(); m_game->removeTestRound();
m_resultsModel->clear(); m_results.clear();
// FIXME arbitration end
#endif
} }

View file

@ -24,6 +24,7 @@
#include <QtGui/QWidget> #include <QtGui/QWidget>
#include <ui/arbitration_widget.ui.h> #include <ui/arbitration_widget.ui.h>
#include "results.h"
#include "move.h" #include "move.h"
#include "logging.h" #include "logging.h"
@ -85,6 +86,9 @@ private:
/// Coordinates of the next word to play /// Coordinates of the next word to play
CoordModel &m_coordModel; CoordModel &m_coordModel;
/// Search results
LimitResults m_results;
/// Model for the search results /// Model for the search results
QStandardItemModel *m_resultsModel; QStandardItemModel *m_resultsModel;
/// Proxy for the results model /// Proxy for the results model