2008-01-08 14:52:32 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Eliot
|
|
|
|
* Copyright (C) 2005-2007 Antoine Fraboulet & Olivier Teulière
|
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
|
|
|
|
* 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
|
|
|
|
*****************************************************************************/
|
2005-08-16 00:20:20 +02:00
|
|
|
|
2008-11-22 14:09:28 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2005-08-16 00:20:20 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include "rack.h"
|
2005-12-26 20:19:42 +01:00
|
|
|
#include "pldrack.h"
|
2008-01-08 14:52:32 +01:00
|
|
|
#include "move.h"
|
2005-12-26 16:33:15 +01:00
|
|
|
#include "turn.h"
|
2005-12-26 20:19:42 +01:00
|
|
|
#include "history.h"
|
2006-01-22 13:23:52 +01:00
|
|
|
#include "encoding.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
2005-08-16 00:20:20 +02:00
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
INIT_LOGGER(game, History);
|
|
|
|
|
|
|
|
|
2005-08-16 00:20:20 +02:00
|
|
|
History::History()
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
Turn* t = new Turn();
|
2005-12-26 20:19:42 +01:00
|
|
|
m_history.clear();
|
2005-12-26 16:33:15 +01:00
|
|
|
m_history.push_back(t);
|
2005-08-16 00:20:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
History::~History()
|
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(Turn *turn, m_history)
|
2005-12-26 16:33:15 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
delete turn;
|
2005-12-26 16:33:15 +01:00
|
|
|
}
|
2005-08-16 00:20:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
unsigned int History::getSize() const
|
2005-08-16 00:20:20 +02:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
ASSERT(!m_history.empty(), "Invalid history size");
|
2005-12-27 01:06:23 +01:00
|
|
|
return m_history.size() - 1;
|
2005-08-16 00:20:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-26 16:33:15 +01:00
|
|
|
const PlayedRack& History::getCurrentRack() const
|
2005-08-16 00:20:20 +02:00
|
|
|
{
|
2005-12-26 16:33:15 +01:00
|
|
|
return m_history.back()->getPlayedRack();
|
2005-08-16 00:20:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void History::setCurrentRack(const PlayedRack &iPld)
|
|
|
|
{
|
2005-12-26 16:33:15 +01:00
|
|
|
m_history.back()->setPlayedRack(iPld);
|
2005-08-16 00:20:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-26 16:33:15 +01:00
|
|
|
const Turn& History::getPreviousTurn() const
|
2005-08-16 00:20:20 +02:00
|
|
|
{
|
2005-12-26 16:33:15 +01:00
|
|
|
int idx = m_history.size() - 2;
|
2005-12-27 01:06:23 +01:00
|
|
|
ASSERT(0 <= idx , "No previous turn");
|
2005-12-26 16:33:15 +01:00
|
|
|
return *(m_history[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const Turn& History::getTurn(unsigned int n) const
|
|
|
|
{
|
2007-08-04 22:01:27 +02:00
|
|
|
ASSERT(n < m_history.size(), "Wrong turn number");
|
2005-12-26 16:33:15 +01:00
|
|
|
return *(m_history[n]);
|
2005-08-16 00:20:20 +02:00
|
|
|
}
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
bool History::beforeFirstRound() const
|
2005-08-16 00:20:20 +02:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
for (unsigned int i = 0; i < m_history.size() - 1; i++)
|
|
|
|
{
|
|
|
|
if (m_history[i]->getMove().getType() == Move::VALID_ROUND)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2005-11-04 21:00:05 +01:00
|
|
|
|
2005-08-16 00:20:20 +02:00
|
|
|
|
2008-11-23 09:18:03 +01:00
|
|
|
void History::playMove(unsigned int iPlayer,
|
|
|
|
const Move &iMove,
|
2012-04-07 14:27:59 +02:00
|
|
|
const PlayedRack &iNewRack)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
Turn * current_turn = m_history.back();
|
|
|
|
|
|
|
|
// Set the number and the round
|
|
|
|
current_turn->setPlayer(iPlayer);
|
|
|
|
current_turn->setMove(iMove);
|
2005-08-16 00:20:20 +02:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Create a new turn
|
2008-11-23 09:18:03 +01:00
|
|
|
Turn * next_turn = new Turn();
|
2012-04-07 14:27:59 +02:00
|
|
|
next_turn->setPlayedRack(iNewRack);
|
2005-12-26 16:33:15 +01:00
|
|
|
m_history.push_back(next_turn);
|
2005-08-16 00:20:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void History::removeLastTurn()
|
|
|
|
{
|
2005-12-26 16:33:15 +01:00
|
|
|
int idx = m_history.size();
|
2005-08-16 00:20:20 +02:00
|
|
|
ASSERT(0 < idx , "Wrong turn number");
|
2005-11-04 21:00:05 +01:00
|
|
|
|
2005-08-16 00:20:20 +02:00
|
|
|
if (idx > 1)
|
2005-12-26 16:33:15 +01:00
|
|
|
{
|
|
|
|
Turn *t = m_history.back();
|
|
|
|
m_history.pop_back();
|
|
|
|
delete t;
|
|
|
|
}
|
2005-08-16 00:20:20 +02:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
// Now we have the previous played round in back()
|
|
|
|
Turn *t = m_history.back();
|
2005-08-16 00:20:20 +02:00
|
|
|
t->setPlayer(0);
|
2008-01-08 14:52:32 +01:00
|
|
|
//t->setRound(Round());
|
2005-08-16 00:20:20 +02:00
|
|
|
#ifdef BACK_REMOVE_RACK_NEW_PART
|
|
|
|
t->getPlayedRound().setNew(Rack());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-23 07:41:16 +01:00
|
|
|
void History::addWarning()
|
|
|
|
{
|
|
|
|
m_history.back()->addWarning();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void History::removeWarning()
|
|
|
|
{
|
|
|
|
m_history.back()->addWarning(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void History::addPenaltyPoints(int iPoints)
|
|
|
|
{
|
|
|
|
m_history.back()->addPenaltyPoints(iPoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void History::addSoloPoints(int iPoints)
|
|
|
|
{
|
|
|
|
m_history.back()->addSoloPoints(iPoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-22 13:23:52 +01:00
|
|
|
wstring History::toString() const
|
2005-08-16 00:20:20 +02:00
|
|
|
{
|
2006-01-22 13:23:52 +01:00
|
|
|
wstring rs;
|
2005-12-26 16:33:15 +01:00
|
|
|
#ifdef DEBUG
|
2006-01-22 13:23:52 +01:00
|
|
|
wchar_t buff[5];
|
|
|
|
_swprintf(buff, 4, L"%ld", m_history.size());
|
|
|
|
rs = L"history size = " + wstring(buff) + L"\n\n";
|
2005-12-26 16:33:15 +01:00
|
|
|
#endif
|
2008-11-22 14:09:28 +01:00
|
|
|
BOOST_FOREACH(const Turn *turn, m_history)
|
2005-12-26 16:33:15 +01:00
|
|
|
{
|
2008-11-22 14:09:28 +01:00
|
|
|
rs += turn->toString() + L"\n";
|
2005-12-26 16:33:15 +01:00
|
|
|
}
|
2005-08-16 00:20:20 +02:00
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|