2012-12-25 17:30:03 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Eliot
|
|
|
|
* Copyright (C) 2012 Olivier Teulière
|
|
|
|
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-12-27 13:58:58 +01:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2012-12-25 17:30:03 +01:00
|
|
|
#include "config.h"
|
|
|
|
#if ENABLE_NLS
|
|
|
|
# include <libintl.h>
|
|
|
|
# define _(String) gettext(String)
|
|
|
|
#else
|
|
|
|
# define _(String) String
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "topping.h"
|
|
|
|
#include "dic.h"
|
|
|
|
#include "tile.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "rack.h"
|
|
|
|
#include "results.h"
|
|
|
|
#include "pldrack.h"
|
|
|
|
#include "player.h"
|
2012-12-27 13:58:58 +01:00
|
|
|
#include "turn.h"
|
|
|
|
#include "cmd/topping_move_cmd.h"
|
2013-01-11 20:24:47 +01:00
|
|
|
#include "cmd/player_rack_cmd.h"
|
2012-12-25 17:30:03 +01:00
|
|
|
#include "cmd/player_move_cmd.h"
|
2013-01-11 20:24:47 +01:00
|
|
|
#include "cmd/player_event_cmd.h"
|
2012-12-25 17:30:03 +01:00
|
|
|
#include "cmd/game_move_cmd.h"
|
|
|
|
#include "encoding.h"
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
|
|
|
INIT_LOGGER(game, Topping);
|
|
|
|
|
|
|
|
|
2012-12-30 16:14:13 +01:00
|
|
|
Topping::Topping(const GameParams &iParams, const Game *iMasterGame)
|
|
|
|
: Game(iParams, iMasterGame)
|
2012-12-25 17:30:03 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Topping::start()
|
|
|
|
{
|
|
|
|
ASSERT(getNPlayers(), "Cannot start a game without any player");
|
|
|
|
|
|
|
|
// Complete the rack
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const PlayedRack &newRack =
|
|
|
|
helperSetRackRandom(getHistory().getCurrentRack(), true, RACK_NEW);
|
2013-01-15 11:38:46 +01:00
|
|
|
setGameAndPlayersRack(newRack, false);
|
2012-12-25 17:30:03 +01:00
|
|
|
}
|
|
|
|
catch (EndGameException &e)
|
|
|
|
{
|
|
|
|
endGame();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-27 13:58:58 +01:00
|
|
|
void Topping::tryWord(const wstring &iWord, const wstring &iCoord, int iElapsed)
|
2012-12-25 17:30:03 +01:00
|
|
|
{
|
|
|
|
m_board.removeTestRound();
|
|
|
|
|
2012-12-27 13:58:58 +01:00
|
|
|
// Perform all the validity checks, and fill a move.
|
|
|
|
// We don't really care if the move is valid or not.
|
2012-12-25 17:30:03 +01:00
|
|
|
Move move;
|
2012-12-27 13:58:58 +01:00
|
|
|
checkPlayedWord(iCoord, iWord, move);
|
|
|
|
|
|
|
|
// Record the try
|
|
|
|
LOG_INFO("Player " << m_currPlayer << " plays topping move after " <<
|
|
|
|
iElapsed << "s: " << lfw(move.toString()));
|
|
|
|
Command *pCmd = new ToppingMoveCmd(m_currPlayer, move, iElapsed);
|
|
|
|
accessNavigation().addAndExecute(pCmd);
|
2012-12-25 17:30:03 +01:00
|
|
|
|
|
|
|
// Find the best score
|
2012-12-27 13:58:58 +01:00
|
|
|
int bestScore = getTopScore();
|
|
|
|
LOG_DEBUG("Top score to be found: " << bestScore);
|
|
|
|
if (bestScore < 0)
|
2012-12-25 17:30:03 +01:00
|
|
|
{
|
|
|
|
endGame();
|
2012-12-27 13:58:58 +01:00
|
|
|
return;
|
2012-12-25 17:30:03 +01:00
|
|
|
}
|
2012-12-27 13:58:58 +01:00
|
|
|
ASSERT(move.getScore() <= bestScore, "The player found better than the top");
|
2012-12-25 17:30:03 +01:00
|
|
|
|
|
|
|
if (move.getScore() < bestScore)
|
|
|
|
{
|
2012-12-27 13:58:58 +01:00
|
|
|
LOG_INFO("End of the game");
|
2012-12-25 17:30:03 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// End the turn
|
2013-01-11 20:24:47 +01:00
|
|
|
recordPlayerMove(move, *m_players[m_currPlayer], iElapsed);
|
2012-12-25 17:30:03 +01:00
|
|
|
|
|
|
|
// Next turn
|
|
|
|
endTurn();
|
|
|
|
}
|
2012-12-27 13:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-16 15:51:09 +01:00
|
|
|
void Topping::turnTimeOut(int iElapsed)
|
2013-01-11 20:24:47 +01:00
|
|
|
{
|
|
|
|
LOG_INFO("Timeout reached, finishing turn automatically");
|
|
|
|
|
|
|
|
m_board.removeTestRound();
|
|
|
|
|
2013-01-16 15:51:09 +01:00
|
|
|
// Retrieve some settings
|
|
|
|
bool giveElapsedPenalty = Settings::Instance().getBool("topping.elapsed-penalty");
|
|
|
|
int timeoutPenalty = Settings::Instance().getInt("topping.timeout-penalty");
|
|
|
|
|
|
|
|
// Compute the points to give to the player
|
|
|
|
int points = timeoutPenalty;
|
|
|
|
if (giveElapsedPenalty)
|
|
|
|
points += iElapsed;
|
|
|
|
|
2013-01-11 20:24:47 +01:00
|
|
|
// The player didn't find the move
|
2013-01-16 15:51:09 +01:00
|
|
|
Command *pCmd = new PlayerMoveCmd(*m_players[m_currPlayer], Move(points));
|
2013-01-11 20:24:47 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
|
|
|
|
|
|
|
// Next turn
|
|
|
|
endTurn();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Topping::addPenalty(int iPenalty)
|
|
|
|
{
|
|
|
|
Command *pCmd = new PlayerEventCmd(*m_players[m_currPlayer],
|
|
|
|
PlayerEventCmd::PENALTY, iPenalty);
|
|
|
|
accessNavigation().addAndExecute(pCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-06 19:05:30 +01:00
|
|
|
int Topping::play(const wstring &, const wstring &)
|
2012-12-27 13:58:58 +01:00
|
|
|
{
|
|
|
|
ASSERT(false, "The play() method should not be called in topping mode");
|
|
|
|
throw GameException("The play() method should not be called in topping mode. Please use tryWord() instead.");
|
2012-12-25 17:30:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-11 20:24:47 +01:00
|
|
|
void Topping::recordPlayerMove(const Move &iMove, Player &ioPlayer, int iElapsed)
|
2012-12-25 17:30:03 +01:00
|
|
|
{
|
2013-01-16 15:51:09 +01:00
|
|
|
// Compute the penalty points to give to the player
|
|
|
|
bool giveElapsedPenalty = Settings::Instance().getBool("topping.elapsed-penalty");
|
|
|
|
int points = giveElapsedPenalty ? iElapsed : 0;
|
|
|
|
|
2013-01-11 20:24:47 +01:00
|
|
|
ASSERT(iMove.isValid(), "Only valid rounds should be played");
|
2013-01-16 15:51:09 +01:00
|
|
|
// Modify the score of the given move, to be the computed score
|
2013-01-11 20:24:47 +01:00
|
|
|
Round copyRound = iMove.getRound();
|
2013-01-16 15:51:09 +01:00
|
|
|
copyRound.setPoints(points);
|
2013-01-11 20:24:47 +01:00
|
|
|
Move newMove(copyRound);
|
|
|
|
|
2012-12-25 17:30:03 +01:00
|
|
|
// Update the rack and the score of the current player
|
|
|
|
// PlayerMoveCmd::execute() must be called before Game::helperPlayMove()
|
|
|
|
// (called in this class in endTurn()).
|
|
|
|
// See the big comment in game.cpp, line 96
|
2013-01-11 20:24:47 +01:00
|
|
|
Command *pCmd = new PlayerMoveCmd(ioPlayer, newMove);
|
2012-12-25 17:30:03 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Topping::isFinished() const
|
|
|
|
{
|
|
|
|
return !canDrawRack(m_players[0]->getHistory().getCurrentRack(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Topping::endTurn()
|
|
|
|
{
|
2013-01-11 20:24:47 +01:00
|
|
|
// Play the top move on the board
|
|
|
|
const Move &move = getTopMove();
|
2013-01-16 14:40:34 +01:00
|
|
|
Command *pCmd = new GameMoveCmd(*this, move);
|
2012-12-25 17:30:03 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
|
|
|
accessNavigation().newTurn();
|
|
|
|
|
2013-01-11 20:24:47 +01:00
|
|
|
// Make sure that the player has the correct rack
|
|
|
|
// (in case he didn't find the top, or not the same one)
|
|
|
|
Command *pCmd2 = new PlayerRackCmd(*m_players[m_currPlayer],
|
|
|
|
getHistory().getCurrentRack());
|
|
|
|
accessNavigation().addAndExecute(pCmd2);
|
|
|
|
|
2012-12-25 17:30:03 +01:00
|
|
|
// Start next turn...
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Topping::endGame()
|
|
|
|
{
|
|
|
|
LOG_INFO("End of the game");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Topping::addPlayer(Player *iPlayer)
|
|
|
|
{
|
|
|
|
ASSERT(getNPlayers() == 0,
|
|
|
|
"Only one player can be added in Topping mode");
|
|
|
|
// Force the name of the player
|
|
|
|
iPlayer->setName(wfl(_("Topping")));
|
|
|
|
Game::addPlayer(iPlayer);
|
|
|
|
}
|
|
|
|
|
2012-12-27 13:58:58 +01:00
|
|
|
|
2013-01-08 15:54:11 +01:00
|
|
|
Move Topping::getTopMove() const
|
2012-12-27 13:58:58 +01:00
|
|
|
{
|
2013-01-17 18:39:03 +01:00
|
|
|
// Find the most interesting top
|
|
|
|
MasterResults results(getBag());
|
2013-01-11 20:24:47 +01:00
|
|
|
results.search(getDic(), getBoard(), getHistory().getCurrentRack().getRack(),
|
2012-12-27 13:58:58 +01:00
|
|
|
getHistory().beforeFirstRound());
|
2013-01-16 11:09:28 +01:00
|
|
|
ASSERT(!results.isEmpty(), "No top move found");
|
2013-01-16 18:40:34 +01:00
|
|
|
|
2013-01-17 18:39:03 +01:00
|
|
|
return Move(results.get(0));
|
2013-01-08 15:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Topping::getTopScore() const
|
|
|
|
{
|
|
|
|
return getTopMove().getScore();
|
2012-12-27 13:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<Move> Topping::getTriedMoves() const
|
|
|
|
{
|
|
|
|
vector<Move> results;
|
|
|
|
const vector<const ToppingMoveCmd*> &cmdVect =
|
|
|
|
getNavigation().getCurrentTurn().findAllMatchingCmd<ToppingMoveCmd>();
|
|
|
|
BOOST_FOREACH(const ToppingMoveCmd * cmd, cmdVect)
|
|
|
|
{
|
|
|
|
results.push_back(cmd->getMove());
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
|