2008-11-23 09:18:03 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* Eliot
|
2012-10-07 16:25:41 +02:00
|
|
|
* Copyright (C) 2008-2012 Olivier Teulière
|
2008-11-23 09:18:03 +01:00
|
|
|
* 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
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-11-23 18:02:33 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
#include "game_move_cmd.h"
|
|
|
|
#include "player.h"
|
2011-08-27 19:21:26 +02:00
|
|
|
#include "game_params.h"
|
2008-11-23 09:18:03 +01:00
|
|
|
#include "game.h"
|
|
|
|
#include "rack.h"
|
2012-10-06 01:50:58 +02:00
|
|
|
#include "turn_data.h"
|
2008-11-23 09:18:03 +01:00
|
|
|
|
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
INIT_LOGGER(game, GameMoveCmd);
|
|
|
|
|
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
GameMoveCmd::GameMoveCmd(Game &ioGame, const Move &iMove,
|
2009-11-29 17:01:31 +01:00
|
|
|
unsigned int iPlayerId)
|
|
|
|
: m_game(ioGame), m_move(iMove),
|
2012-02-25 23:16:42 +01:00
|
|
|
m_moveRack(ioGame.getHistory().getCurrentRack()),
|
2008-11-23 09:18:03 +01:00
|
|
|
m_playerId(iPlayerId)
|
|
|
|
{
|
2012-02-19 16:25:27 +01:00
|
|
|
setAutoExecutable(false);
|
2008-11-23 09:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameMoveCmd::doExecute()
|
|
|
|
{
|
|
|
|
// Get the original rack from the player history
|
2012-04-07 14:27:59 +02:00
|
|
|
const PlayedRack &newRack = Move::ComputeRackForMove(m_moveRack, m_move);
|
2008-11-23 09:18:03 +01:00
|
|
|
|
|
|
|
// History of the game
|
|
|
|
History &history = m_game.accessHistory();
|
|
|
|
history.playMove(m_playerId, m_move, newRack);
|
|
|
|
|
|
|
|
// Points
|
|
|
|
m_game.addPoints(m_move.getScore());
|
|
|
|
|
|
|
|
// For moves corresponding to a valid round, we have much more
|
|
|
|
// work to do...
|
2012-04-30 21:21:38 +02:00
|
|
|
if (m_move.isValid())
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
|
|
|
playRound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameMoveCmd::doUndo()
|
|
|
|
{
|
|
|
|
// Undo playing the round on the board
|
2012-04-30 21:21:38 +02:00
|
|
|
if (m_move.isValid())
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
|
|
|
unplayRound();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Points
|
|
|
|
m_game.addPoints(- m_move.getScore());
|
|
|
|
|
|
|
|
// History
|
|
|
|
m_game.accessHistory().removeLastTurn();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameMoveCmd::playRound()
|
|
|
|
{
|
|
|
|
// Copy the round, because we may need to modify it (case of
|
|
|
|
// the joker games). It will also be convenient for the unplayRound()
|
|
|
|
// method.
|
|
|
|
m_round = m_move.getRound();
|
|
|
|
|
|
|
|
// Update the bag
|
|
|
|
// We remove tiles from the bag only when they are played
|
|
|
|
// on the board. When going back in the game, we must only
|
|
|
|
// replace played tiles.
|
|
|
|
// We test a rack when it is set but tiles are left in the bag.
|
|
|
|
Bag & bag = m_game.accessBag();
|
|
|
|
for (unsigned int i = 0; i < m_round.getWordLen(); i++)
|
|
|
|
{
|
|
|
|
if (m_round.isPlayedFromRack(i))
|
|
|
|
{
|
|
|
|
if (m_round.isJoker(i))
|
|
|
|
{
|
|
|
|
bag.takeTile(Tile::Joker());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bag.takeTile(m_round.getTile(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-28 19:24:37 +02:00
|
|
|
if (m_game.getParams().hasVariant(GameParams::kJOKER))
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < m_round.getWordLen(); i++)
|
|
|
|
{
|
|
|
|
if (m_round.isPlayedFromRack(i) && m_round.isJoker(i))
|
|
|
|
{
|
|
|
|
// Is the represented letter still available in the bag?
|
2012-02-18 23:52:07 +01:00
|
|
|
const Tile &t = m_round.getTile(i).toUpper();
|
2009-02-21 17:56:32 +01:00
|
|
|
if (bag.in(t))
|
2008-11-23 09:18:03 +01:00
|
|
|
{
|
|
|
|
bag.replaceTile(Tile::Joker());
|
|
|
|
bag.takeTile(t);
|
|
|
|
m_round.setTile(i, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In a joker game we should have only 1 joker in the rack
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the board
|
|
|
|
m_game.accessBoard().addRound(m_game.getDic(), m_round);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameMoveCmd::unplayRound()
|
|
|
|
{
|
|
|
|
// Update the board
|
|
|
|
m_game.accessBoard().removeRound(m_game.getDic(), m_round);
|
|
|
|
|
|
|
|
// Update the bag
|
|
|
|
// We remove tiles from the bag only when they are played
|
|
|
|
// on the board. When going back in the game, we must only
|
|
|
|
// replace played tiles.
|
|
|
|
// We test a rack when it is set but tiles are left in the bag.
|
|
|
|
Bag & bag = m_game.accessBag();
|
|
|
|
for (unsigned int i = 0; i < m_round.getWordLen(); i++)
|
|
|
|
{
|
|
|
|
if (m_round.isPlayedFromRack(i))
|
|
|
|
{
|
|
|
|
if (m_round.isJoker(i))
|
|
|
|
{
|
|
|
|
bag.replaceTile(Tile::Joker());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bag.replaceTile(m_round.getTile(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-23 18:02:33 +01:00
|
|
|
|
|
|
|
wstring GameMoveCmd::toString() const
|
|
|
|
{
|
|
|
|
wostringstream oss;
|
|
|
|
oss << L"GameMoveCmd (move: " << m_move.toString() << L", rack: "
|
|
|
|
<< m_moveRack.toString() << L")";
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|