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-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"
|
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-02-12 22:02:39 +01:00
|
|
|
#include "turn.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
FreeGame::FreeGame(const Dictionary &iDic)
|
|
|
|
: Game(iDic)
|
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
|
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("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
|
|
|
|
// "round" variable, or it is invalid but played nevertheless
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
Move move(round);
|
|
|
|
|
|
|
|
// Update the rack and the score of the current player
|
2008-11-23 09:18:03 +01:00
|
|
|
recordPlayerMove(move, *m_players[m_currPlayer]);
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Move move(iWord, iCoord);
|
|
|
|
|
|
|
|
// Record the invalid move of the player
|
2008-11-23 09:18:03 +01:00
|
|
|
recordPlayerMove(move, *m_players[m_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
|
|
|
// 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();
|
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-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
|
2008-11-23 09:18:03 +01:00
|
|
|
recordPlayerMove(move, *player);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
endTurn();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
void FreeGame::recordPlayerMove(const Move &iMove, Player &ioPlayer)
|
2008-11-22 14:09:28 +01:00
|
|
|
{
|
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
|
|
|
|
for (unsigned int i = 0; i < getNPlayers(); i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
const PlayedRack &newRack =
|
|
|
|
helperSetRackRandom(getPlayer(i).getCurrentRack(), false, RACK_NEW);
|
2008-11-23 09:18:03 +01:00
|
|
|
Command *pCmd = new PlayerRackCmd(*m_players[i], newRack);
|
2008-11-23 17:55:28 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
2008-11-23 18:07:42 +01:00
|
|
|
firstPlayer();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-11-23 18:05:13 +01:00
|
|
|
accessNavigation().newTurn();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FreeGame::endTurn()
|
|
|
|
{
|
2008-11-23 09:18:03 +01:00
|
|
|
const Move &move = getCurrentPlayer().getLastMove();
|
2008-11-22 14:09:28 +01:00
|
|
|
// Update the game
|
2008-11-23 09:18:03 +01:00
|
|
|
Command *pCmd = new GameMoveCmd(*this, move,
|
|
|
|
getCurrentPlayer().getLastRack(),
|
|
|
|
m_currPlayer);
|
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
|
2008-02-12 22:02:39 +01:00
|
|
|
if (move.getType() == Move::VALID_ROUND ||
|
|
|
|
move.getType() == Move::CHANGE_LETTERS)
|
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);
|
2008-11-23 09:18:03 +01:00
|
|
|
Command *pCmd = new PlayerRackCmd(*m_players[m_currPlayer],
|
|
|
|
newRack);
|
2008-11-23 17:55:28 +01:00
|
|
|
accessNavigation().addAndExecute(pCmd);
|
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();
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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
|
|
|
|
2005-02-05 12:14:56 +01: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
|
2005-03-27 19:30:48 +02:00
|
|
|
* from the score of the players who still have tiles */
|
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-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Tile &tile, tiles)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
m_players[i]->addPoints(- tile.getPoints());
|
|
|
|
m_players[m_currPlayer]->addPoints(tile.getPoints());
|
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
|
2005-03-28 16:55:27 +02:00
|
|
|
if (m_finished)
|
|
|
|
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();
|
2005-02-05 12:14:56 +01:00
|
|
|
Rack rack;
|
|
|
|
pld.getRack(rack);
|
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
|
2008-11-22 14:09:28 +01:00
|
|
|
if (!rack.in(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;
|
|
|
|
|
|
|
|
Move move(iToChange);
|
|
|
|
// End the player's turn
|
2008-11-23 09:18:03 +01: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;
|
|
|
|
}
|
|
|
|
|