2005-02-05 12:14:56 +01:00
|
|
|
|
/*****************************************************************************
|
2008-01-08 14:52:32 +01:00
|
|
|
|
* Eliot
|
2012-10-07 16:25:41 +02:00
|
|
|
|
* Copyright (C) 2005-2012 Antoine Fraboulet & Olivier Teuli<EFBFBD>re
|
2008-01-08 14:52:32 +01:00
|
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
|
|
|
|
|
* Olivier Teuli<EFBFBD>re <ipkiss @@ gmail.com>
|
2005-02-05 12:14:56 +01:00
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-23 16:53:42 +02:00
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-02-05 12:14:56 +01:00
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2009-01-22 19:30:22 +01:00
|
|
|
|
#include <boost/foreach.hpp>
|
2005-02-05 12:14:56 +01:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <functional>
|
2008-03-03 23:13:58 +01:00
|
|
|
|
#include <cwctype>
|
2009-01-22 19:30:22 +01:00
|
|
|
|
#include <cmath>
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
|
|
#include "tile.h"
|
|
|
|
|
#include "round.h"
|
2005-02-17 21:01:59 +01:00
|
|
|
|
#include "board.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
#include "results.h"
|
2005-03-27 23:45:04 +02:00
|
|
|
|
#include "debug.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
|
INIT_LOGGER(game, Results);
|
|
|
|
|
|
|
|
|
|
|
2009-07-04 01:00:12 +02:00
|
|
|
|
bool tileCompare(const Tile &t1, const Tile &t2)
|
2008-03-03 23:13:58 +01:00
|
|
|
|
{
|
2009-07-04 01:00:12 +02:00
|
|
|
|
return t1.toCode() < t2.toCode();
|
2008-03-03 23:13:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct less_points : public binary_function<const Round&, const Round&, bool>
|
2005-02-05 12:14:56 +01:00
|
|
|
|
{
|
|
|
|
|
bool operator()(const Round &r1, const Round &r2)
|
|
|
|
|
{
|
|
|
|
|
// We want higher scores first, so we use '>' instead of '<'
|
2008-03-03 23:13:58 +01:00
|
|
|
|
if (r1.getPoints() > r2.getPoints())
|
|
|
|
|
return true;
|
|
|
|
|
else if (r1.getPoints() < r2.getPoints())
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-07-04 01:00:12 +02:00
|
|
|
|
// If the scores are equal, sort alphabetically (i.e. in the
|
|
|
|
|
// order of the letters in the dictionary), ignoring the case
|
2008-07-04 21:03:12 +02:00
|
|
|
|
const wstring &s1 = r1.getWord();
|
|
|
|
|
const wstring &s2 = r2.getWord();
|
2009-07-04 01:00:12 +02:00
|
|
|
|
if (std::lexicographical_compare(r1.getTiles().begin(),
|
|
|
|
|
r1.getTiles().end(),
|
|
|
|
|
r2.getTiles().begin(),
|
|
|
|
|
r2.getTiles().end(),
|
|
|
|
|
tileCompare))
|
2008-10-16 19:51:16 +02:00
|
|
|
|
{
|
|
|
|
|
return true;;
|
|
|
|
|
}
|
2009-07-04 01:00:12 +02:00
|
|
|
|
else if (std::lexicographical_compare(r2.getTiles().begin(),
|
|
|
|
|
r2.getTiles().end(),
|
|
|
|
|
r1.getTiles().begin(),
|
|
|
|
|
r1.getTiles().end(),
|
|
|
|
|
tileCompare))
|
2008-10-16 19:51:16 +02:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// If the rounds are still equal, compare the coordinates
|
|
|
|
|
const wstring &c1 = r1.getCoord().toString();
|
|
|
|
|
const wstring &c2 = r2.getCoord().toString();
|
|
|
|
|
if (c1 < c2)
|
|
|
|
|
return true;
|
|
|
|
|
else if (c2 < c1)
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Still equal? This time compare taking the case into
|
|
|
|
|
// account. After that, we are sure that the rounds will
|
|
|
|
|
// be different...
|
|
|
|
|
return std::lexicographical_compare(s1.begin(),
|
|
|
|
|
s1.end(),
|
|
|
|
|
s2.begin(),
|
|
|
|
|
s2.end());
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-03-03 23:13:58 +01:00
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
const Round & Results::get(unsigned int i) const
|
2005-02-05 12:14:56 +01:00
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
|
ASSERT(i < size(), "Results index out of bounds");
|
2005-03-27 23:45:04 +02:00
|
|
|
|
return m_rounds[i];
|
2005-02-05 12:14:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-01-22 19:30:22 +01:00
|
|
|
|
void Results::sort()
|
|
|
|
|
{
|
|
|
|
|
less_points lp;
|
|
|
|
|
std::sort(m_rounds.begin(), m_rounds.end(), lp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BestResults::BestResults()
|
|
|
|
|
: m_bestScore(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BestResults::search(const Dictionary &iDic, const Board &iBoard,
|
|
|
|
|
const Rack &iRack, bool iFirstWord)
|
2005-02-05 12:14:56 +01:00
|
|
|
|
{
|
2005-02-17 21:01:59 +01:00
|
|
|
|
clear();
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
if (iFirstWord)
|
2005-02-17 21:01:59 +01:00
|
|
|
|
iBoard.searchFirst(iDic, iRack, *this);
|
2009-01-22 19:30:22 +01:00
|
|
|
|
else
|
|
|
|
|
iBoard.search(iDic, iRack, *this);
|
|
|
|
|
|
|
|
|
|
sort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BestResults::add(const Round &iRound)
|
|
|
|
|
{
|
|
|
|
|
// Ignore too low scores
|
|
|
|
|
if (m_bestScore > iRound.getPoints())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_bestScore < iRound.getPoints())
|
|
|
|
|
{
|
|
|
|
|
// New best score: clear the stored results
|
|
|
|
|
m_bestScore = iRound.getPoints();
|
|
|
|
|
m_rounds.clear();
|
|
|
|
|
}
|
|
|
|
|
m_rounds.push_back(iRound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BestResults::clear()
|
|
|
|
|
{
|
|
|
|
|
m_rounds.clear();
|
|
|
|
|
m_bestScore = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PercentResults::PercentResults(float iPercent)
|
2012-10-11 23:21:32 +02:00
|
|
|
|
: m_percent(iPercent), m_bestScore(0), m_minScore(0)
|
2009-01-22 19:30:22 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Predicate
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Predicate(int iPoints) : m_chosenPoints(iPoints) {}
|
|
|
|
|
bool operator()(const Round &iRound) const
|
|
|
|
|
{
|
|
|
|
|
return iRound.getPoints() != m_chosenPoints;
|
2005-12-26 22:22:21 +01:00
|
|
|
|
}
|
2009-01-22 19:30:22 +01:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const int m_chosenPoints;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PercentResults::search(const Dictionary &iDic, const Board &iBoard,
|
|
|
|
|
const Rack &iRack, bool iFirstWord)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
|
|
|
|
|
if (iFirstWord)
|
|
|
|
|
iBoard.searchFirst(iDic, iRack, *this);
|
2005-02-17 21:01:59 +01:00
|
|
|
|
else
|
2009-01-22 19:30:22 +01:00
|
|
|
|
iBoard.search(iDic, iRack, *this);
|
|
|
|
|
|
|
|
|
|
if (m_rounds.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// At this point, add() has been called, so the best score is valid
|
|
|
|
|
|
|
|
|
|
// Find the lowest score at least equal to the min_score
|
|
|
|
|
int chosenPoints = m_bestScore;
|
|
|
|
|
BOOST_FOREACH(const Round &iRound, m_rounds)
|
|
|
|
|
{
|
|
|
|
|
int points = iRound.getPoints();
|
|
|
|
|
if (points >= m_minScore && points < chosenPoints)
|
|
|
|
|
{
|
|
|
|
|
chosenPoints = points;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Keep only the rounds with the "chosenPoints" score
|
2010-02-20 14:31:12 +01:00
|
|
|
|
vector<Round>::iterator last =
|
|
|
|
|
std::remove_if(m_rounds.begin(), m_rounds.end(), Predicate(chosenPoints));
|
|
|
|
|
m_rounds.erase(last, m_rounds.end());
|
2009-01-22 19:30:22 +01:00
|
|
|
|
ASSERT(!m_rounds.empty(), "Bug in PercentResults");
|
|
|
|
|
|
|
|
|
|
// Sort the remaining rounds
|
|
|
|
|
sort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PercentResults::add(const Round &iRound)
|
|
|
|
|
{
|
|
|
|
|
// Ignore too low scores
|
|
|
|
|
if (m_minScore > iRound.getPoints())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_bestScore < iRound.getPoints())
|
2005-12-26 22:22:21 +01:00
|
|
|
|
{
|
2009-01-22 19:30:22 +01:00
|
|
|
|
m_bestScore = iRound.getPoints();
|
2010-03-06 17:54:20 +01:00
|
|
|
|
m_minScore = lrint(ceil(m_bestScore * m_percent));
|
2009-01-22 19:30:22 +01:00
|
|
|
|
}
|
|
|
|
|
m_rounds.push_back(iRound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PercentResults::clear()
|
|
|
|
|
{
|
|
|
|
|
m_rounds.clear();
|
|
|
|
|
m_bestScore = 0;
|
|
|
|
|
m_minScore = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LimitResults::LimitResults(int iLimit)
|
|
|
|
|
: m_limit(iLimit), m_total(0), m_minScore(-1)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LimitResults::search(const Dictionary &iDic, const Board &iBoard,
|
|
|
|
|
const Rack &iRack, bool iFirstWord)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
|
|
|
|
|
if (iFirstWord)
|
|
|
|
|
iBoard.searchFirst(iDic, iRack, *this);
|
|
|
|
|
else
|
2005-02-17 21:01:59 +01:00
|
|
|
|
iBoard.search(iDic, iRack, *this);
|
2009-01-22 19:30:22 +01:00
|
|
|
|
|
|
|
|
|
if (m_rounds.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Sort the rounds
|
|
|
|
|
sort();
|
|
|
|
|
|
|
|
|
|
// Truncate the results to respect the limit
|
|
|
|
|
if (m_limit != 0 && m_rounds.size() > (unsigned int) m_limit)
|
|
|
|
|
m_rounds.resize(m_limit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LimitResults::add(const Round &iRound)
|
|
|
|
|
{
|
|
|
|
|
// If we ignore the limit, simply add the round
|
|
|
|
|
if (m_limit == 0)
|
|
|
|
|
{
|
|
|
|
|
m_rounds.push_back(iRound);
|
|
|
|
|
return;
|
2005-12-26 22:22:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-22 19:30:22 +01:00
|
|
|
|
// Ignore too low scores
|
|
|
|
|
if (m_minScore >= iRound.getPoints())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Add the round
|
|
|
|
|
m_rounds.push_back(iRound);
|
|
|
|
|
++m_total;
|
|
|
|
|
++m_scoresCount[iRound.getPoints()];
|
|
|
|
|
|
|
|
|
|
// Can we increase the minimum score required?
|
|
|
|
|
if (m_total - m_scoresCount[m_minScore] >= m_limit)
|
|
|
|
|
{
|
|
|
|
|
// Yes! "Forget" the rounds of score m_minScore
|
|
|
|
|
// They are still present in m_rounds, but they will be removed
|
|
|
|
|
// for real later in the search() method
|
|
|
|
|
m_total -= m_scoresCount[m_minScore];
|
|
|
|
|
m_scoresCount.erase(m_minScore);
|
|
|
|
|
|
|
|
|
|
// Find the new min score
|
|
|
|
|
map<int, int>::const_iterator it =
|
|
|
|
|
m_scoresCount.lower_bound(m_minScore);
|
|
|
|
|
ASSERT(it != m_scoresCount.end(), "Bug in LimitResults::add())");
|
|
|
|
|
m_minScore = it->first;
|
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-17 21:01:59 +01:00
|
|
|
|
|
2009-01-22 19:30:22 +01:00
|
|
|
|
void LimitResults::clear()
|
2005-02-17 21:01:59 +01:00
|
|
|
|
{
|
2009-01-22 19:30:22 +01:00
|
|
|
|
m_rounds.clear();
|
|
|
|
|
m_scoresCount.clear();
|
|
|
|
|
m_minScore = -1;
|
|
|
|
|
m_total = 0;
|
2005-02-17 21:01:59 +01:00
|
|
|
|
}
|
|
|
|
|
|