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

View file

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

View file

@ -249,17 +249,12 @@ void PublicGame::arbitrationSetRackManual(const wstring &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,
const wstring &iCoords) const
{

View file

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

View file

@ -58,7 +58,7 @@ INIT_LOGGER(qt, ArbitrationWidget);
ArbitrationWidget::ArbitrationWidget(QWidget *parent,
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);
@ -210,6 +210,15 @@ ArbitrationWidget::ArbitrationWidget(QWidget *parent,
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();
updatePlayersModel();
@ -225,12 +234,6 @@ void ArbitrationWidget::refresh()
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())
{
setEnabled(false);
@ -244,8 +247,8 @@ void ArbitrationWidget::updateResultsModel()
// FIXME arbitration begin
// 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
if (m_game != NULL && m_game->isLastTurn() &&
m_game->arbitrationGetResults().size() + m_addedMoves.size() ==
if (m_game != NULL &&
m_results.size() + m_addedMoves.size() ==
static_cast<unsigned int>(m_resultsModel->rowCount()))
{
return;
@ -254,16 +257,15 @@ void ArbitrationWidget::updateResultsModel()
#endif
m_resultsModel->removeRows(0, m_resultsModel->rowCount());
if (m_game == NULL || !m_game->isLastTurn())
if (m_game == NULL)
return;
// First step: add the search results (type TYPE_ROUND)
const Results &results = m_game->arbitrationGetResults();
// Find the highest score
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);
}
@ -542,7 +544,8 @@ void ArbitrationWidget::on_buttonSearch_clicked()
{
m_game->removeTestRound();
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 gameUpdated();
@ -683,9 +686,8 @@ Move ArbitrationWidget::getSelectedMove() const
if (origin == TYPE_ROUND)
{
unsigned int resNb = m_resultsModel->data(index, MOVE_INDEX_ROLE).toUInt();
const Results &results = m_game->arbitrationGetResults();
ASSERT(resNb < results.size(), "Wrong result number");
return Move(results.get(resNb));
ASSERT(resNb < m_results.size(), "Wrong result number");
return Move(m_results.get(resNb));
}
else
{
@ -719,12 +721,8 @@ QSet<unsigned int> ArbitrationWidget::getSelectedPlayers() const
void ArbitrationWidget::clearResults()
{
#if 0
// FIXME arbitration begin
m_game->removeTestRound();
m_resultsModel->clear();
// FIXME arbitration end
#endif
m_results.clear();
}

View file

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