2005-02-05 12:14:56 +01:00
|
|
|
/*****************************************************************************
|
2008-01-08 14:52:32 +01:00
|
|
|
* Eliot
|
2008-11-23 09:18:03 +01:00
|
|
|
* Copyright (C) 2005-2008 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-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"
|
|
|
|
#include "player.h"
|
2008-11-23 09:18:03 +01:00
|
|
|
#include "player_move_cmd.h"
|
|
|
|
#include "player_rack_cmd.h"
|
|
|
|
#include "game_move_cmd.h"
|
2005-02-17 21:01:59 +01:00
|
|
|
#include "ai_player.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "settings.h"
|
2008-11-23 09:18:03 +01:00
|
|
|
#include "turn_cmd.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
Duplicate::Duplicate(const Dictionary &iDic)
|
|
|
|
: Game(iDic)
|
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
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
// Perform all the validity checks, and try to fill a round
|
2005-02-05 12:14:56 +01:00
|
|
|
Round round;
|
|
|
|
int res = checkPlayedWord(iCoord, iWord, round);
|
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
|
|
|
|
// "round" variable, or it is invalid but played nevertheless
|
2008-11-23 09:18:03 +01:00
|
|
|
Player &currPlayer = *m_players[m_currPlayer];
|
2008-01-08 14:52:32 +01:00
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
// Everything is OK, we can play the word
|
2008-11-23 09:18:03 +01:00
|
|
|
recordPlayerMove(Move(round), currPlayer);
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Record the invalid move of the player
|
2008-11-23 09:18:03 +01:00
|
|
|
recordPlayerMove(Move(iWord, iCoord), currPlayer);
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
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");
|
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();
|
2008-01-08 14:52:32 +01:00
|
|
|
if (move.getType() == Move::CHANGE_LETTERS ||
|
|
|
|
move.getType() == Move::PASS)
|
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
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
recordPlayerMove(move, *player);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Duplicate::start()
|
|
|
|
{
|
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;
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Complete the rack for the player that just played
|
2008-11-22 14:09:28 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const PlayedRack &newRack =
|
|
|
|
helperSetRackRandom(getCurrentPlayer().getCurrentRack(), true, RACK_NEW);
|
2008-11-23 09:18:03 +01:00
|
|
|
// All the players have the same rack
|
|
|
|
for (unsigned int i = 0; i < getNPlayers(); i++)
|
|
|
|
{
|
|
|
|
Command *pCmd = new PlayerRackCmd(*m_players[i], newRack);
|
|
|
|
m_turnCommands[m_currTurn]->addAndExecute(pCmd);
|
|
|
|
// Nobody has played yet in this round
|
|
|
|
m_hasPlayed[i] = false;
|
|
|
|
}
|
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();
|
2005-02-05 12:14:56 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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::tryEndTurn()
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
for (unsigned int i = 0; i < getNPlayers(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-02-12 19:54:57 +01:00
|
|
|
if (m_players[i]->isHuman() && !m_hasPlayed[i])
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
// A human player has not played...
|
2005-02-05 12:14:56 +01:00
|
|
|
m_currPlayer = i;
|
2008-01-08 14:52:32 +01:00
|
|
|
// 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
|
|
|
|
for (unsigned int i = 0; i < getNPlayers(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
if (!m_players[i]->isHuman())
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
void Duplicate::recordPlayerMove(const Move &iMove, Player &ioPlayer)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-23 09:18:03 +01:00
|
|
|
Command *pCmd = new PlayerMoveCmd(ioPlayer, iMove);
|
|
|
|
m_turnCommands[m_currTurn]->addAndExecute(pCmd);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
m_hasPlayed[ioPlayer.getId()] = true;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void Duplicate::endTurn()
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
// Find the player with the best score
|
|
|
|
unsigned int imax = 0;
|
|
|
|
for (unsigned int i = 1; i < getNPlayers(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
if (m_players[i]->getLastMove().getScore() >
|
|
|
|
m_players[imax]->getLastMove().getScore())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
imax = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// TODO: do something if nobody played a valid round!
|
|
|
|
|
2008-11-22 14:09:28 +01:00
|
|
|
// Get the best valid move
|
|
|
|
const Move &bestMove = m_players[imax]->getLastMove();
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Handle solo bonus
|
|
|
|
// First check whether there are enough players in the game for the
|
|
|
|
// bonus to apply
|
2008-09-22 23:21:38 +02:00
|
|
|
int minNbPlayers = Settings::Instance().getInt("duplicate.solo-players");
|
2008-01-08 14:52:32 +01:00
|
|
|
if (getNPlayers() >= (unsigned int)minNbPlayers &&
|
2008-11-22 14:09:28 +01:00
|
|
|
bestMove.getType() == Move::VALID_ROUND)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
int maxScore = bestMove.getScore();
|
2008-01-08 14:52:32 +01:00
|
|
|
// Find whether other players than imax have the same score
|
|
|
|
bool otherWithSameScore = false;
|
|
|
|
for (unsigned int i = imax + 1; i < getNPlayers(); i++)
|
|
|
|
{
|
|
|
|
if (m_players[i]->getLastMove().getScore() >= maxScore)
|
|
|
|
{
|
|
|
|
otherWithSameScore = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!otherWithSameScore)
|
|
|
|
{
|
|
|
|
// Give the bonus to player imax
|
2008-09-22 23:21:38 +02:00
|
|
|
int bonus = Settings::Instance().getInt("duplicate.solo-value");
|
2008-01-08 14:52:32 +01:00
|
|
|
m_players[imax]->addPoints(bonus);
|
|
|
|
// TODO: keep a trace of the solo, so the interface
|
|
|
|
// can be aware of it...
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Play the best word on the board
|
2008-11-23 09:18:03 +01:00
|
|
|
Command *pCmd = new GameMoveCmd(*this, bestMove,
|
|
|
|
getPlayer(imax).getLastRack(), imax);
|
|
|
|
m_turnCommands[m_currTurn]->addAndExecute(pCmd);
|
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
|
2005-11-06 02:05:06 +01:00
|
|
|
const PlayedRack& pld = m_players[imax]->getCurrentRack();
|
2008-01-08 14:52:32 +01:00
|
|
|
for (unsigned int i = 0; i < getNPlayers(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
if (i != imax)
|
|
|
|
{
|
2008-11-23 09:18:03 +01:00
|
|
|
Command *pCmd = new PlayerRackCmd(*m_players[i], pld);
|
|
|
|
m_turnCommands[m_currTurn]->addAndExecute(pCmd);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
newTurn();
|
|
|
|
|
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
|
|
|
{
|
2005-02-13 18:14:31 +01:00
|
|
|
m_finished = true;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
int 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())
|
2005-03-27 23:45:04 +02:00
|
|
|
return 1;
|
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
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-02-12 19:54:57 +01:00
|
|
|
|
|
|
|
void Duplicate::prevHumanPlayer()
|
|
|
|
{
|
|
|
|
if (getNHumanPlayers() == 0)
|
|
|
|
return;
|
|
|
|
// FIXME: possible infinite loop...
|
|
|
|
do
|
|
|
|
{
|
|
|
|
prevPlayer();
|
|
|
|
} while (!m_players[m_currPlayer]->isHuman() ||
|
|
|
|
m_hasPlayed[m_currPlayer]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Duplicate::nextHumanPlayer()
|
|
|
|
{
|
|
|
|
if (getNHumanPlayers() == 0)
|
|
|
|
return;
|
|
|
|
// FIXME: possible infinite loop...
|
|
|
|
do
|
|
|
|
{
|
|
|
|
nextPlayer();
|
|
|
|
} while (!m_players[m_currPlayer]->isHuman() ||
|
|
|
|
m_hasPlayed[m_currPlayer]);
|
|
|
|
}
|
|
|
|
|
2008-09-14 19:56:18 +02:00
|
|
|
|
|
|
|
bool Duplicate::hasPlayed(unsigned int p) const
|
|
|
|
{
|
|
|
|
ASSERT(p < getNPlayers(), "Wrong player number");
|
|
|
|
|
|
|
|
map<unsigned int, bool>::const_iterator it = m_hasPlayed.find(p);
|
|
|
|
return it != m_hasPlayed.end() && it->second;
|
|
|
|
}
|
|
|
|
|