eliot/game/history.cpp

185 lines
4.1 KiB
C++
Raw Permalink Normal View History

/*****************************************************************************
* Eliot
* Copyright (C) 2005-2012 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
*****************************************************************************/
#include <boost/foreach.hpp>
#include <string>
#include "rack.h"
2005-12-26 20:19:42 +01:00
#include "pldrack.h"
#include "move.h"
2012-10-06 01:50:58 +02:00
#include "turn_data.h"
2005-12-26 20:19:42 +01:00
#include "history.h"
#include "encoding.h"
#include "debug.h"
2012-02-18 22:26:52 +01:00
INIT_LOGGER(game, History);
History::History()
{
2012-10-06 01:57:39 +02:00
TurnData *t = new TurnData();
2005-12-26 20:19:42 +01:00
m_history.clear();
2005-12-26 16:33:15 +01:00
m_history.push_back(t);
}
History::~History()
{
2012-10-06 01:57:39 +02:00
BOOST_FOREACH(TurnData *turn, m_history)
2005-12-26 16:33:15 +01:00
{
delete turn;
2005-12-26 16:33:15 +01:00
}
}
unsigned int History::getSize() const
{
ASSERT(!m_history.empty(), "Invalid history size");
return m_history.size() - 1;
}
2005-12-26 16:33:15 +01:00
const PlayedRack& History::getCurrentRack() const
{
2005-12-26 16:33:15 +01:00
return m_history.back()->getPlayedRack();
}
void History::setCurrentRack(const PlayedRack &iPld)
{
2005-12-26 16:33:15 +01:00
m_history.back()->setPlayedRack(iPld);
}
2012-10-06 01:57:39 +02:00
const TurnData& History::getPreviousTurn() const
{
2005-12-26 16:33:15 +01:00
int idx = m_history.size() - 2;
ASSERT(0 <= idx , "No previous turn");
2005-12-26 16:33:15 +01:00
return *(m_history[idx]);
}
2012-10-06 01:57:39 +02:00
const TurnData& History::getTurn(unsigned int n) const
2005-12-26 16:33:15 +01:00
{
ASSERT(n < m_history.size(), "Wrong turn number");
2005-12-26 16:33:15 +01:00
return *(m_history[n]);
}
bool History::beforeFirstRound() const
{
for (unsigned int i = 0; i < m_history.size() - 1; i++)
{
if (m_history[i]->getMove().isValid())
return false;
}
return true;
}
2005-11-04 21:00:05 +01:00
2013-01-16 14:40:34 +01:00
void History::playMove(const Move &iMove,
const PlayedRack &iNewRack)
{
2012-10-06 01:57:39 +02:00
TurnData * current_turn = m_history.back();
// Set the number and the round
current_turn->setMove(iMove);
// Create a new turn
2012-10-06 01:57:39 +02:00
TurnData * next_turn = new TurnData();
next_turn->setPlayedRack(iNewRack);
2005-12-26 16:33:15 +01:00
m_history.push_back(next_turn);
}
void History::removeLastTurn()
{
2005-12-26 16:33:15 +01:00
int idx = m_history.size();
ASSERT(0 < idx , "Wrong turn number");
2005-11-04 21:00:05 +01:00
if (idx > 1)
2005-12-26 16:33:15 +01:00
{
2012-10-06 01:57:39 +02:00
TurnData *t = m_history.back();
2005-12-26 16:33:15 +01:00
m_history.pop_back();
delete t;
}
2013-01-16 14:40:34 +01:00
#ifdef BACK_REMOVE_RACK_NEW_PART
// Now we have the previous played round in back()
2012-10-06 01:57:39 +02:00
TurnData *t = m_history.back();
//t->setRound(Round());
t->getPlayedRound().setNew(Rack());
#endif
}
2012-03-23 07:41:16 +01:00
void History::addWarning()
{
ASSERT(m_history.size() > 1, "Too short history");
m_history[m_history.size() - 2]->addWarning();
2012-03-23 07:41:16 +01:00
}
void History::removeWarning()
{
ASSERT(m_history.size() > 1, "Too short history");
m_history[m_history.size() - 2]->addWarning(-1);
2012-03-23 07:41:16 +01:00
}
void History::addPenaltyPoints(int iPoints)
{
ASSERT(m_history.size() > 1, "Too short history");
m_history[m_history.size() - 2]->addPenaltyPoints(iPoints);
2012-03-23 07:41:16 +01:00
}
void History::addSoloPoints(int iPoints)
{
ASSERT(m_history.size() > 1, "Too short history");
m_history[m_history.size() - 2]->addSoloPoints(iPoints);
2012-03-23 07:41:16 +01:00
}
void History::addEndGamePoints(int iPoints)
{
ASSERT(m_history.size() > 1, "Too short history");
m_history[m_history.size() - 2]->addEndGamePoints(iPoints);
}
wstring History::toString() const
{
wstring rs;
2005-12-26 16:33:15 +01:00
#ifdef DEBUG
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
2012-10-06 01:57:39 +02:00
BOOST_FOREACH(const TurnData *turn, m_history)
2005-12-26 16:33:15 +01:00
{
rs += turn->toString() + L"\n";
2005-12-26 16:33:15 +01:00
}
return rs;
}