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 Olivier Teulière
|
2008-01-08 14:52:32 +01:00
|
|
|
* Authors: Olivier Teuliè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
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-11-23 18:03:29 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2008-11-23 18:06:44 +01:00
|
|
|
#include <sstream>
|
2008-11-23 18:03:29 +01:00
|
|
|
|
2009-01-24 18:11:07 +01:00
|
|
|
#include "config.h"
|
|
|
|
#if ENABLE_NLS
|
|
|
|
# include <libintl.h>
|
|
|
|
# define _(String) gettext(String)
|
|
|
|
#else
|
|
|
|
# define _(String) String
|
|
|
|
#endif
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "duplicate.h"
|
2008-11-22 14:09:28 +01:00
|
|
|
#include "game_exception.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "dic.h"
|
|
|
|
#include "tile.h"
|
|
|
|
#include "rack.h"
|
|
|
|
#include "round.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "move.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "pldrack.h"
|
|
|
|
#include "results.h"
|
2013-01-16 18:40:34 +01:00
|
|
|
#include "move_selector.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "player.h"
|
2012-12-05 20:26:47 +01:00
|
|
|
#include "cmd/player_move_cmd.h"
|
|
|
|
#include "cmd/player_rack_cmd.h"
|
|
|
|
#include "cmd/player_event_cmd.h"
|
|
|
|
#include "cmd/game_move_cmd.h"
|
|
|
|
#include "cmd/master_move_cmd.h"
|
2005-02-17 21:01:59 +01:00
|
|
|
#include "ai_player.h"
|
2012-03-05 01:27:56 +01:00
|
|
|
#include "navigation.h"
|
2012-10-06 11:14:01 +02:00
|
|
|
#include "turn.h"
|
2012-12-05 23:02:12 +01:00
|
|
|
#include "turn_data.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "settings.h"
|
2011-01-30 00:47:20 +01:00
|
|
|
#include "encoding.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
2011-01-30 00:47:20 +01:00
|
|
|
INIT_LOGGER(game, Duplicate);
|
|
|
|
|
|
|
|
|
2012-12-30 16:14:13 +01:00
|
|
|
Duplicate::Duplicate(const GameParams &iParams, const Game *iMasterGame)
|
|
|
|
: Game(iParams, iMasterGame)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-22 13:23:52 +01:00
|
|
|
int Duplicate::play(const wstring &iCoord, const wstring &iWord)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-03-14 20:37:21 +01:00
|
|
|
Player &currPlayer = *m_players[m_currPlayer];
|
|
|
|
|
|
|
|
ASSERT(currPlayer.isHuman(), "Human player expected");
|
2012-03-05 01:27:56 +01:00
|
|
|
ASSERT(!hasPlayed(m_currPlayer), "Human player has already played");
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Perform all the validity checks, and try to fill a round
|
2012-12-23 19:31:03 +01:00
|
|
|
Move move;
|
|
|
|
int res = checkPlayedWord(iCoord, iWord, move);
|
2008-09-22 23:21:38 +02:00
|
|
|
if (res != 0 && Settings::Instance().getBool("duplicate.reject-invalid"))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// If we reach this point, either the move is valid and we can use the
|
2012-12-23 19:31:03 +01:00
|
|
|
// "move" variable, or it is invalid but played nevertheless
|
|
|
|
recordPlayerMove(currPlayer, move);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Little hack to handle duplicate games with only AI players.
|
|
|
|
// This will have no effect when there is at least one human player
|
|
|
|
tryEndTurn();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void Duplicate::playAI(unsigned int p)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
ASSERT(p < getNPlayers(), "Wrong player number");
|
2012-03-05 01:27:56 +01:00
|
|
|
ASSERT(!hasPlayed(p), "AI player has already played");
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
AIPlayer *player = dynamic_cast<AIPlayer*>(m_players[p]);
|
|
|
|
ASSERT(player != NULL, "AI requested for a human player");
|
2005-03-27 23:45:04 +02:00
|
|
|
|
2008-11-22 15:40:25 +01:00
|
|
|
player->compute(getDic(), getBoard(), getHistory().beforeFirstRound());
|
2008-11-23 09:18:03 +01:00
|
|
|
const Move &move = player->getMove();
|
2012-04-30 21:21:38 +02:00
|
|
|
if (move.isChangeLetters() || move.isPass())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
// The AI player must be buggy...
|
|
|
|
ASSERT(false, "AI tried to cheat!");
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2012-03-14 20:37:21 +01:00
|
|
|
recordPlayerMove(*player, move);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 18:04:40 +01:00
|
|
|
void Duplicate::start()
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-03-27 23:45:04 +02:00
|
|
|
ASSERT(getNPlayers(), "Cannot start a game without any player");
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Arbitrary player, since they should all have the same rack
|
2005-02-05 12:14:56 +01:00
|
|
|
m_currPlayer = 0;
|
|
|
|
|
2012-02-25 23:16:42 +01:00
|
|
|
// Complete the racks
|
2008-11-22 14:09:28 +01:00
|
|
|
try
|
|
|
|
{
|
2012-12-30 16:14:13 +01:00
|
|
|
if (hasMasterGame())
|
|
|
|
{
|
|
|
|
// When a master game is defined, it forces the master move
|
|
|
|
setMasterMove(getMoveFromMasterGame());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Reset the master move
|
|
|
|
setMasterMove(Move());
|
|
|
|
}
|
2012-03-11 00:23:39 +01:00
|
|
|
|
2012-04-16 23:02:11 +02:00
|
|
|
bool fillRacks = Settings::Instance().getBool("arbitration.fill-rack");
|
2012-12-30 16:14:13 +01:00
|
|
|
if (isArbitrationGame() && !fillRacks && !hasMasterGame())
|
2013-01-15 11:38:46 +01:00
|
|
|
setGameAndPlayersRack(getHistory().getCurrentRack(), true);
|
2012-04-16 23:02:11 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const PlayedRack &newRack =
|
|
|
|
helperSetRackRandom(getHistory().getCurrentRack(), true, RACK_NEW);
|
2013-01-15 11:38:46 +01:00
|
|
|
setGameAndPlayersRack(newRack, true);
|
2012-04-16 23:02:11 +02:00
|
|
|
}
|
2008-11-22 14:09:28 +01:00
|
|
|
}
|
|
|
|
catch (EndGameException &e)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
endGame();
|
2008-11-23 18:04:40 +01:00
|
|
|
return;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2012-10-04 23:15:35 +02:00
|
|
|
if (!isArbitrationGame())
|
2012-04-08 01:07:59 +02:00
|
|
|
{
|
|
|
|
// Little hack to handle duplicate games with only AI players.
|
|
|
|
// This will have no effect when there is at least one human player
|
|
|
|
tryEndTurn();
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-30 01:23:45 +01:00
|
|
|
bool Duplicate::isFinished() const
|
|
|
|
{
|
2012-10-05 01:42:46 +02:00
|
|
|
return !canDrawRack(m_players[0]->getHistory().getCurrentRack(), true);
|
2011-01-30 01:23:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void Duplicate::tryEndTurn()
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-10-04 23:15:35 +02:00
|
|
|
if (!isArbitrationGame())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-04-08 01:07:59 +02:00
|
|
|
for (unsigned int i = 0; i < getNPlayers(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-04-08 01:07:59 +02:00
|
|
|
if (m_players[i]->isHuman() && !hasPlayed(i))
|
|
|
|
{
|
|
|
|
// A human player has not played...
|
|
|
|
m_currPlayer = i;
|
|
|
|
// So we don't finish the turn
|
|
|
|
return;
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Now that all the human players have played,
|
|
|
|
// make AI players play their turn
|
2012-03-05 01:27:56 +01:00
|
|
|
// Some may have already played, in arbitration mode, if the future turns
|
|
|
|
// were removed (because of the isHumanIndependent() behaviour)
|
2008-01-08 14:52:32 +01:00
|
|
|
for (unsigned int i = 0; i < getNPlayers(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-04-08 00:27:48 +02:00
|
|
|
if (!m_players[i]->isHuman() && !hasPlayed(i))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
playAI(i);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Next turn
|
|
|
|
endTurn();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-05 01:27:56 +01:00
|
|
|
struct MatchingPlayer : public unary_function<PlayerMoveCmd, bool>
|
|
|
|
{
|
|
|
|
MatchingPlayer(unsigned iPlayerId) : m_playerId(iPlayerId) {}
|
|
|
|
|
2012-03-07 09:17:56 +01:00
|
|
|
bool operator()(const PlayerMoveCmd &cmd)
|
2012-03-05 01:27:56 +01:00
|
|
|
{
|
2012-03-07 09:17:56 +01:00
|
|
|
return cmd.getPlayer().getId() == m_playerId;
|
2012-03-05 01:27:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned m_playerId;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-08 01:07:59 +02:00
|
|
|
void Duplicate::recordPlayerMove(Player &ioPlayer, const Move &iMove)
|
2012-03-05 01:27:56 +01:00
|
|
|
{
|
2012-04-08 01:07:59 +02:00
|
|
|
LOG_INFO("Player " << ioPlayer.getId() << " plays: " << lfw(iMove.toString()));
|
2012-03-05 01:27:56 +01:00
|
|
|
|
2012-04-08 01:07:59 +02:00
|
|
|
// Search a PlayerMoveCmd for the given player
|
2012-03-05 01:27:56 +01:00
|
|
|
MatchingPlayer predicate(ioPlayer.getId());
|
|
|
|
const PlayerMoveCmd *cmd =
|
|
|
|
getNavigation().getCurrentTurn().findMatchingCmd<PlayerMoveCmd>(predicate);
|
2012-04-08 01:07:59 +02:00
|
|
|
if (cmd == 0)
|
|
|
|
{
|
2012-10-04 23:15:35 +02:00
|
|
|
Command *pCmd = new PlayerMoveCmd(ioPlayer, iMove, isArbitrationGame());
|
2012-04-08 01:07:59 +02:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Replace the player move
|
|
|
|
LOG_DEBUG("Replacing move for player " << ioPlayer.getId());
|
2012-10-04 23:15:35 +02:00
|
|
|
if (!isArbitrationGame() && !getNavigation().isLastTurn())
|
2012-04-08 01:07:59 +02:00
|
|
|
throw GameException("Cannot add a command to an old turn");
|
2012-10-04 23:15:35 +02:00
|
|
|
Command *pCmd = new PlayerMoveCmd(ioPlayer, iMove, isArbitrationGame());
|
2012-04-08 01:07:59 +02:00
|
|
|
accessNavigation().replaceCommand(*cmd, pCmd);
|
|
|
|
}
|
2012-03-05 01:27:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-17 23:35:16 +01:00
|
|
|
Player * Duplicate::findBestPlayer() const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-23 18:03:29 +01:00
|
|
|
Player *bestPlayer = NULL;
|
2012-01-17 23:35:16 +01:00
|
|
|
int bestScore = -1;
|
2008-11-23 18:03:29 +01:00
|
|
|
BOOST_FOREACH(Player *player, m_players)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-01-17 23:35:16 +01:00
|
|
|
const Move &move = player->getLastMove();
|
2012-04-30 21:21:38 +02:00
|
|
|
if (move.isValid() && move.getScore() > bestScore)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-01-17 23:35:16 +01:00
|
|
|
bestScore = move.getScore();
|
2008-11-23 18:03:29 +01:00
|
|
|
bestPlayer = player;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-17 23:35:16 +01:00
|
|
|
return bestPlayer;
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2012-01-17 23:35:16 +01:00
|
|
|
|
|
|
|
void Duplicate::endTurn()
|
|
|
|
{
|
|
|
|
static const unsigned int REF_PLAYER_ID = 0;
|
|
|
|
|
2012-12-30 16:14:13 +01:00
|
|
|
// If there is a master game, the master move should still have its
|
|
|
|
// original value (set in the start() method)
|
|
|
|
ASSERT(!hasMasterGame() || m_masterMove == getMoveFromMasterGame(),
|
|
|
|
"The master move has been modified");
|
|
|
|
|
2012-01-17 23:35:16 +01:00
|
|
|
// Define the master move if it is not already defined
|
2012-04-30 21:21:38 +02:00
|
|
|
if (!m_masterMove.isValid())
|
2008-11-23 18:03:29 +01:00
|
|
|
{
|
2012-01-17 23:35:16 +01:00
|
|
|
// The chosen implementation is to find the best move among the players' moves.
|
|
|
|
// It is more user-friendly than forcing the best move when nobody found it.
|
|
|
|
// If you want the best move instead, simply add an AI player to the game!
|
|
|
|
|
|
|
|
// Find the player with the best score
|
|
|
|
const Player *bestPlayer = findBestPlayer();
|
|
|
|
if (bestPlayer != NULL)
|
2008-11-23 18:03:29 +01:00
|
|
|
{
|
2012-01-17 23:35:16 +01:00
|
|
|
setMasterMove(bestPlayer->getLastMove());
|
2008-11-23 18:03:29 +01:00
|
|
|
}
|
2012-01-17 23:35:16 +01:00
|
|
|
else
|
2008-11-23 18:03:29 +01:00
|
|
|
{
|
2012-01-17 23:35:16 +01:00
|
|
|
// If nobody played a valid round, we are forced to play a valid move.
|
|
|
|
// So let's take the best one...
|
|
|
|
BestResults results;
|
|
|
|
// Take the first player's rack
|
2012-01-26 21:10:41 +01:00
|
|
|
const Rack &rack =
|
|
|
|
m_players[REF_PLAYER_ID]->getLastRack().getRack();
|
|
|
|
results.search(getDic(), getBoard(), rack, getHistory().beforeFirstRound());
|
2013-01-16 11:09:28 +01:00
|
|
|
if (results.isEmpty())
|
2012-01-17 23:35:16 +01:00
|
|
|
{
|
|
|
|
// This would be very bad luck that no move is possible...
|
|
|
|
// It's probably not even possible, but let's be safe.
|
|
|
|
throw EndGameException(_("No possible move"));
|
|
|
|
}
|
2013-01-16 18:40:34 +01:00
|
|
|
|
|
|
|
// Select a clever master move if possible
|
|
|
|
MoveSelector selector;
|
|
|
|
setMasterMove(Move(selector.selectMaster(results)));
|
2008-11-23 18:03:29 +01:00
|
|
|
}
|
|
|
|
}
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2012-12-05 23:02:12 +01:00
|
|
|
// Handle solo bonus
|
2012-10-05 12:52:42 +02:00
|
|
|
if (!isArbitrationGame())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-12-05 23:02:12 +01:00
|
|
|
unsigned minNbPlayers = Settings::Instance().getInt("duplicate.solo-players");
|
|
|
|
int soloValue = Settings::Instance().getInt("duplicate.solo-value");
|
|
|
|
setSoloAuto(minNbPlayers, soloValue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool useSoloAuto = Settings::Instance().getBool("arbitration.solo-auto");
|
|
|
|
if (useSoloAuto)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2012-12-05 23:02:12 +01:00
|
|
|
unsigned minNbPlayers = Settings::Instance().getInt("arbitration.solo-players");
|
|
|
|
int soloValue = Settings::Instance().getInt("arbitration.solo-value");
|
|
|
|
setSoloAuto(minNbPlayers, soloValue);
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 23:35:16 +01:00
|
|
|
// Play the master word on the board
|
2012-01-17 21:36:22 +01:00
|
|
|
// We assign it to player 0 arbitrarily (this is only used
|
|
|
|
// to retrieve the rack, which is the same for all players...)
|
2013-01-16 14:40:34 +01:00
|
|
|
Command *pCmd = new GameMoveCmd(*this, m_masterMove);
|
2008-11-23 17:55:28 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2012-02-19 18:34:37 +01:00
|
|
|
// Change the turn after doing all the game changes.
|
|
|
|
// The navigation system expects auto-executable commands (like setting
|
|
|
|
// the players racks) at the beginning of the turn, to work properly.
|
|
|
|
accessNavigation().newTurn();
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Leave the same reliquate to all players
|
|
|
|
// This is required by the start() method which will be called to
|
|
|
|
// start the next turn
|
2012-01-17 21:36:22 +01:00
|
|
|
const PlayedRack& pld = getHistory().getCurrentRack();
|
2008-11-23 18:03:29 +01:00
|
|
|
BOOST_FOREACH(Player *player, m_players)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-01-17 21:36:22 +01:00
|
|
|
Command *pCmd = new PlayerRackCmd(*player, pld);
|
|
|
|
accessNavigation().addAndExecute(pCmd);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Start next turn...
|
|
|
|
start();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void Duplicate::endGame()
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2011-01-30 00:47:20 +01:00
|
|
|
LOG_INFO("End of the game");
|
2012-03-11 00:23:39 +01:00
|
|
|
// No more master move
|
|
|
|
setMasterMove(Move());
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-24 18:11:07 +01:00
|
|
|
void Duplicate::setPlayer(unsigned int p)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
ASSERT(p < getNPlayers(), "Wrong player number");
|
2005-03-27 23:45:04 +02:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Forbid switching to an AI player
|
|
|
|
if (!m_players[p]->isHuman())
|
2009-01-24 18:11:07 +01:00
|
|
|
throw GameException(_("Cannot switch to a non-human player"));
|
|
|
|
|
|
|
|
// Forbid switching back to a player who has already played
|
|
|
|
if (hasPlayed(p))
|
|
|
|
throw GameException(_("Cannot switch to a player who has already played"));
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
m_currPlayer = p;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2005-02-12 19:54:57 +01:00
|
|
|
|
2012-04-08 01:07:59 +02:00
|
|
|
bool Duplicate::hasPlayed(unsigned iPlayerId) const
|
2008-11-23 18:06:44 +01:00
|
|
|
{
|
|
|
|
ASSERT(iPlayerId < getNPlayers(), "Wrong player number");
|
|
|
|
|
2012-04-08 01:07:59 +02:00
|
|
|
// Search a PlayerMoveCmd for the given player
|
|
|
|
MatchingPlayer predicate(iPlayerId);
|
|
|
|
const PlayerMoveCmd *cmd =
|
|
|
|
getNavigation().getCurrentTurn().findMatchingCmd<PlayerMoveCmd>(predicate);
|
2012-04-30 21:21:38 +02:00
|
|
|
return cmd != 0 && cmd->isExecuted() && !cmd->getMove().isNull();
|
2008-11-23 18:06:44 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 17:29:19 +01:00
|
|
|
|
|
|
|
void Duplicate::innerSetMasterMove(const Move &iMove)
|
|
|
|
{
|
|
|
|
m_masterMove = iMove;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Duplicate::setMasterMove(const Move &iMove)
|
|
|
|
{
|
2012-04-30 21:21:38 +02:00
|
|
|
ASSERT(iMove.isValid() || iMove.isNull(), "Invalid move type");
|
2012-01-17 17:29:19 +01:00
|
|
|
|
|
|
|
// If this method is called several times for the same turn, it will
|
|
|
|
// result in many MasterMoveCmd commands in the command stack.
|
|
|
|
// This shouldn't be a problem though.
|
2012-03-05 01:27:56 +01:00
|
|
|
LOG_DEBUG("Setting master move: " + lfw(iMove.toString()));
|
2012-01-17 17:29:19 +01:00
|
|
|
Command *pCmd = new MasterMoveCmd(*this, iMove);
|
|
|
|
accessNavigation().addAndExecute(pCmd);
|
|
|
|
}
|
|
|
|
|
2012-03-11 00:24:21 +01:00
|
|
|
|
2012-10-04 23:15:35 +02:00
|
|
|
bool Duplicate::isArbitrationGame() const
|
|
|
|
{
|
|
|
|
return getParams().getMode() == GameParams::kARBITRATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-05 23:02:12 +01:00
|
|
|
void Duplicate::setSoloAuto(unsigned int minNbPlayers, int iSoloValue)
|
|
|
|
{
|
|
|
|
// Remove all existing solos
|
|
|
|
BOOST_FOREACH(const Player *player, m_players)
|
|
|
|
{
|
|
|
|
const PlayerEventCmd *cmd = getPlayerEvent(player->getId(), PlayerEventCmd::SOLO);
|
|
|
|
if (cmd != 0)
|
|
|
|
{
|
|
|
|
accessNavigation().dropCommand(*cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether there are enough players in the game for the
|
|
|
|
// solo to apply. We count only the "active" players, i.e. the ones
|
|
|
|
// which have played at least one word during the game, even if they
|
|
|
|
// have left the game since then, or have arrived after the beginning.
|
|
|
|
unsigned countActive = 0;
|
|
|
|
BOOST_FOREACH(const Player *player, m_players)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < player->getHistory().getSize(); ++i)
|
|
|
|
{
|
|
|
|
if (!player->getHistory().getTurn(i).getMove().isNull())
|
|
|
|
{
|
|
|
|
++countActive;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (countActive < minNbPlayers)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find the player with the best score
|
|
|
|
Player *bestPlayer = findBestPlayer();
|
|
|
|
if (bestPlayer != NULL)
|
|
|
|
{
|
|
|
|
int bestScore = bestPlayer->getLastMove().getScore();
|
|
|
|
|
|
|
|
// Find whether other players than imax have the same score
|
|
|
|
bool otherWithSameScore = false;
|
|
|
|
BOOST_FOREACH(const Player *player, m_players)
|
|
|
|
{
|
|
|
|
if (player != bestPlayer &&
|
|
|
|
player->getLastMove().getScore() >= bestScore &&
|
|
|
|
player->getLastMove().isValid())
|
|
|
|
{
|
|
|
|
otherWithSameScore = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!otherWithSameScore)
|
|
|
|
{
|
|
|
|
// Give the bonus to the player of the best move
|
|
|
|
LOG_INFO("Giving a solo of " << iSoloValue << " to player " << bestPlayer->getId());
|
|
|
|
Command *pCmd = new PlayerEventCmd(*bestPlayer, PlayerEventCmd::SOLO, iSoloValue);
|
|
|
|
accessNavigation().insertCommand(pCmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-05 22:05:31 +01:00
|
|
|
/// Predicate to help retrieving commands
|
|
|
|
struct MatchingPlayerAndEventType : public unary_function<PlayerEventCmd, bool>
|
|
|
|
{
|
|
|
|
MatchingPlayerAndEventType(unsigned iPlayerId, int iEventType)
|
|
|
|
: m_playerId(iPlayerId), m_eventType(iEventType) {}
|
|
|
|
|
|
|
|
bool operator()(const PlayerEventCmd &cmd)
|
|
|
|
{
|
|
|
|
return cmd.getPlayer().getId() == m_playerId
|
|
|
|
&& cmd.getEventType() == m_eventType;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned m_playerId;
|
|
|
|
const int m_eventType;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const PlayerEventCmd * Duplicate::getPlayerEvent(unsigned iPlayerId,
|
|
|
|
int iEventType) const
|
|
|
|
{
|
|
|
|
ASSERT(iPlayerId < getNPlayers(), "Wrong player number");
|
|
|
|
MatchingPlayerAndEventType predicate(iPlayerId, iEventType);
|
|
|
|
const Turn &currTurn = getNavigation().getCurrentTurn();
|
|
|
|
return currTurn.findMatchingCmd<PlayerEventCmd>(predicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
|