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-22 14:09:28 +01:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
#include <iomanip>
|
2006-01-22 13:23:52 +01:00
|
|
|
#include <wctype.h>
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
#include "freegame.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"
|
2012-12-05 20:26:47 +01:00
|
|
|
#include "cmd/player_event_cmd.h"
|
|
|
|
#include "cmd/player_move_cmd.h"
|
|
|
|
#include "cmd/player_rack_cmd.h"
|
|
|
|
#include "cmd/game_move_cmd.h"
|
|
|
|
#include "cmd/game_rack_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"
|
2012-10-06 01:50:58 +02:00
|
|
|
#include "turn_data.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, FreeGame);
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2012-12-30 16:14:13 +01:00
|
|
|
FreeGame::FreeGame(const GameParams &iParams, const Game *iMasterGame)
|
|
|
|
: Game(iParams, iMasterGame), m_finished(false)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-22 13:23:52 +01:00
|
|
|
int FreeGame::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
|
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("freegame.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(move, *m_players[m_currPlayer]);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Next turn
|
2005-02-05 12:14:56 +01:00
|
|
|
endTurn();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void FreeGame::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");
|
|
|
|
ASSERT(!m_players[p]->isHuman(), "AI requested for a human player");
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
AIPlayer *player = static_cast<AIPlayer*>(m_players[p]);
|
2005-02-05 12:14:56 +01: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-11-23 09:18:03 +01:00
|
|
|
ASSERT(checkPass(*player, move.getChangedLetters()) == 0,
|
|
|
|
"AI tried to cheat!");
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Update the rack and the score of the current player
|
2012-10-05 00:01:18 +02:00
|
|
|
recordPlayerMove(move, *player);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
endTurn();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-05 00:01:18 +02:00
|
|
|
void FreeGame::recordPlayerMove(const Move &iMove, Player &ioPlayer)
|
2008-11-22 14:09:28 +01:00
|
|
|
{
|
2011-07-30 21:48:05 +02:00
|
|
|
LOG_INFO("Player " << ioPlayer.getId() << " plays: " << lfw(iMove.toString()));
|
2008-11-23 09:18:03 +01:00
|
|
|
Command *pCmd = new PlayerMoveCmd(ioPlayer, iMove);
|
2008-11-23 17:55:28 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
2008-11-22 14:09:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 18:04:40 +01:00
|
|
|
void FreeGame::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
|
|
|
// Set the initial racks of the players
|
2012-12-05 21:23:25 +01:00
|
|
|
BOOST_FOREACH(Player *player, m_players)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
const PlayedRack &newRack =
|
2012-12-05 21:23:25 +01:00
|
|
|
helperSetRackRandom(player->getCurrentRack(), false, RACK_NEW);
|
|
|
|
Command *pCmd = new PlayerRackCmd(*player, newRack);
|
2008-11-23 17:55:28 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2012-02-25 23:16:42 +01:00
|
|
|
// Set the game rack to the rack of the current player
|
|
|
|
Command *pCmd = new GameRackCmd(*this, getPlayer(0).getCurrentRack());
|
|
|
|
accessNavigation().addAndExecute(pCmd);
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// If the first player is an AI, make it play now
|
|
|
|
if (!m_players[m_currPlayer]->isHuman())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
playAI(m_currPlayer);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-30 01:23:45 +01:00
|
|
|
bool FreeGame::isFinished() const
|
|
|
|
{
|
2012-10-05 01:42:46 +02:00
|
|
|
// FIXME: the flag is never reset to false!
|
2011-01-30 01:23:45 +01:00
|
|
|
return m_finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
int FreeGame::endTurn()
|
|
|
|
{
|
2012-04-07 17:49:22 +02:00
|
|
|
// Give a "no move" pseudo-move to all the players
|
|
|
|
// who didn't play at this turn
|
|
|
|
for (unsigned int i = 0; i < getNPlayers(); ++i)
|
|
|
|
{
|
|
|
|
if (i == m_currPlayer)
|
|
|
|
continue;
|
|
|
|
Command *pCmd = new PlayerMoveCmd(*m_players[i], Move());
|
|
|
|
// The pseudo-moves should be completely transparent
|
|
|
|
pCmd->setHumanIndependent(true);
|
|
|
|
accessNavigation().addAndExecute(pCmd);
|
|
|
|
}
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
const Move &move = getCurrentPlayer().getLastMove();
|
2008-11-22 14:09:28 +01:00
|
|
|
// Update the game
|
2013-01-16 14:40:34 +01:00
|
|
|
Command *pCmd = new GameMoveCmd(*this, move);
|
2008-11-23 17:55:28 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
2008-11-22 14:09:28 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Complete the rack for the player that just played
|
2012-04-30 21:21:38 +02:00
|
|
|
if (move.isValid() || move.isChangeLetters())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const PlayedRack &newRack =
|
|
|
|
helperSetRackRandom(getCurrentPlayer().getCurrentRack(), false, RACK_NEW);
|
2012-02-25 23:16:42 +01:00
|
|
|
Command *pCmd2 = new PlayerRackCmd(*m_players[m_currPlayer], newRack);
|
|
|
|
accessNavigation().addAndExecute(pCmd2);
|
2008-11-22 14:09:28 +01:00
|
|
|
}
|
|
|
|
catch (EndGameException &e)
|
2008-02-12 22:02:39 +01:00
|
|
|
{
|
|
|
|
// End of the game
|
|
|
|
endGame();
|
|
|
|
return 1;
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Next player
|
2005-02-05 12:14:56 +01:00
|
|
|
nextPlayer();
|
|
|
|
|
2012-02-25 23:16:42 +01:00
|
|
|
// Set the game rack to the rack of the current player
|
|
|
|
Command *pCmd3 = new GameRackCmd(*this, getCurrentPlayer().getCurrentRack());
|
|
|
|
accessNavigation().addAndExecute(pCmd3);
|
|
|
|
|
2008-11-23 17:55:28 +01:00
|
|
|
accessNavigation().newTurn();
|
2008-11-23 09:18:03 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// If this player is an AI, make it play now
|
2008-11-23 09:18:03 +01:00
|
|
|
if (!getCurrentPlayer().isHuman())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
playAI(m_currPlayer);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-27 23:45:04 +02:00
|
|
|
// Adjust the scores of the players with the points of the remaining tiles
|
2008-01-08 14:52:32 +01:00
|
|
|
void FreeGame::endGame()
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2011-01-30 00:47:20 +01:00
|
|
|
LOG_INFO("End of the game");
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
vector<Tile> tiles;
|
|
|
|
|
2005-12-23 23:19:22 +01:00
|
|
|
// TODO: According to the rules of the game in the ODS, a game can end in 3
|
2005-03-28 17:23:55 +02:00
|
|
|
// cases:
|
|
|
|
// 1) no more letter in the bag, and one player has no letter in his rack
|
|
|
|
// 2) the game is "blocked", no one can play
|
|
|
|
// 3) the players have used all the time they had (for example: 30 min
|
|
|
|
// in total, for each player)
|
|
|
|
// We currently handle case 1, and cannot handle case 3 until timers are
|
|
|
|
// implemented.
|
|
|
|
// For case 2, we need both to detect a blocked situation (not easy...) and
|
2008-01-08 14:52:32 +01:00
|
|
|
// to handle it in the endGame() method (very easy).
|
2005-03-28 17:23:55 +02:00
|
|
|
|
2012-10-05 17:51:01 +02:00
|
|
|
// Add the points of the remaining tiles to the score of the current
|
|
|
|
// player (i.e. the first player with an empty rack), and remove them
|
|
|
|
// from the score of the players who still have tiles
|
|
|
|
int addedPoints = 0;
|
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 != m_currPlayer)
|
|
|
|
{
|
2005-02-09 23:33:56 +01:00
|
|
|
const PlayedRack &pld = m_players[i]->getCurrentRack();
|
2005-02-05 12:14:56 +01:00
|
|
|
pld.getAllTiles(tiles);
|
2008-11-30 22:10:25 +01:00
|
|
|
int points = 0;
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, tiles)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-30 22:10:25 +01:00
|
|
|
points += tile.getPoints();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2012-10-05 17:51:01 +02:00
|
|
|
addedPoints += points;
|
|
|
|
// Remove the points from the "losing" player
|
|
|
|
Command *pCmd = new PlayerEventCmd(*m_players[i],
|
|
|
|
PlayerEventCmd::END_GAME, -points);
|
2008-11-30 22:10:25 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-05 17:51:01 +02:00
|
|
|
// Add all the points to the current player
|
|
|
|
Command *pCmd = new PlayerEventCmd(*m_players[m_currPlayer],
|
|
|
|
PlayerEventCmd::END_GAME, addedPoints);
|
|
|
|
accessNavigation().addAndExecute(pCmd);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Lock game
|
2005-02-05 12:14:56 +01:00
|
|
|
m_finished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
int FreeGame::checkPass(const Player &iPlayer,
|
|
|
|
const wstring &iToChange) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
// Check that the game is not finished
|
2012-10-05 17:55:41 +02:00
|
|
|
if (isFinished())
|
2005-03-28 16:55:27 +02:00
|
|
|
return 3;
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Check that the letters are valid for the current dictionary
|
2008-11-22 15:40:25 +01:00
|
|
|
if (!getDic().validateLetters(iToChange))
|
2008-01-08 14:52:32 +01:00
|
|
|
return 4;
|
2005-03-27 23:45:04 +02:00
|
|
|
|
2005-03-28 17:23:55 +02:00
|
|
|
// It is forbidden to change letters when the bag does not contain at
|
2008-01-08 14:52:32 +01:00
|
|
|
// least 7 letters (this is explicitly stated in the ODS). But it is
|
|
|
|
// still allowed to pass
|
2008-11-22 15:40:25 +01:00
|
|
|
Bag bag(getDic());
|
2005-02-05 12:14:56 +01:00
|
|
|
realBag(bag);
|
2008-01-08 14:52:32 +01:00
|
|
|
if (bag.getNbTiles() < 7 && !iToChange.empty())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Check that the letters are all present in the player's rack
|
2008-11-23 09:18:03 +01:00
|
|
|
const PlayedRack &pld = iPlayer.getCurrentRack();
|
2012-01-26 21:10:41 +01:00
|
|
|
Rack rack = pld.getRack();
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(wchar_t wch, iToChange)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
// Remove the letter from the rack
|
2013-01-17 17:55:00 +01:00
|
|
|
if (!rack.count(Tile(wch)))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
2008-11-22 14:09:28 +01:00
|
|
|
rack.remove(Tile(wch));
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// According to the rules in the ODS, it is allowed to pass its turn (no
|
|
|
|
// need to change letters for that).
|
|
|
|
// TODO: However, if all the players pass their turn, the first one has to
|
|
|
|
// play, or change at least one letter. To implement this behaviour, we
|
|
|
|
// must also take care of blocked positions, where no one _can_ play (see
|
|
|
|
// also comment in the endGame() method).
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-02-09 23:33:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
int FreeGame::pass(const wstring &iToChange)
|
|
|
|
{
|
2008-11-23 09:18:03 +01:00
|
|
|
Player &player = *m_players[m_currPlayer];
|
|
|
|
int res = checkPass(player, iToChange);
|
2008-01-08 14:52:32 +01:00
|
|
|
if (res != 0)
|
|
|
|
return res;
|
|
|
|
|
2012-10-05 10:28:54 +02:00
|
|
|
ASSERT(player.isHuman(), "AI tried to pass using the wrong method");
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
Move move(iToChange);
|
|
|
|
// End the player's turn
|
2012-10-05 00:01:18 +02:00
|
|
|
recordPlayerMove(move, player);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
// Next game turn
|
|
|
|
endTurn();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|