eliot/game/history.cpp

203 lines
5 KiB
C++
Raw Normal View History

/*****************************************************************************
* 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
*****************************************************************************/
/**
* \file history.cpp
* \brief Game history system
* \author Antoine Fraboulet
* \date 2005
*/
#include <string>
#include "rack.h"
2005-12-26 20:19:42 +01:00
#include "pldrack.h"
#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"
#include "encoding.h"
#include "debug.h"
/* ******************************************************** */
/* ******************************************************** */
/* ******************************************************** */
History::History()
{
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);
}
History::~History()
{
2005-12-26 16:33:15 +01:00
for (unsigned int i = 0; i < m_history.size(); i++)
{
delete m_history[i];
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);
}
2005-12-26 16:33:15 +01:00
const Turn& 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]);
}
const Turn& History::getTurn(unsigned int n) const
{
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().getType() == Move::VALID_ROUND)
return false;
}
return true;
}
2005-11-04 21:00:05 +01:00
void History::playMove(unsigned int iPlayer, unsigned int iTurn, const Move &iMove)
{
Turn * current_turn = m_history.back();
// Set the number and the round
current_turn->setNum(iTurn);
current_turn->setPlayer(iPlayer);
current_turn->setMove(iMove);
// Get what was the rack for the current turn
Rack rack;
current_turn->getPlayedRack().getRack(rack);
if (iMove.getType() == Move::VALID_ROUND)
{
// Remove the played tiles from the rack
const Round &round = iMove.getRound();
for (unsigned int i = 0; i < round.getWordLen(); i++)
{
if (round.isPlayedFromRack(i))
{
if (round.isJoker(i))
rack.remove(Tile::Joker());
else
rack.remove(round.getTile(i));
}
}
}
else if (iMove.getType() == Move::CHANGE_LETTERS)
{
// Remove the changed tiles from the rack
const wstring & changed = iMove.getChangedLetters();
for (unsigned int i = 0; i < changed.size(); ++i)
{
rack.remove(Tile(changed[i]));
}
}
// Create a new turn
2005-12-26 16:33:15 +01:00
Turn * next_turn = new Turn();
PlayedRack pldrack;
pldrack.setOld(rack);
next_turn->setPlayedRack(pldrack);
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
{
Turn *t = m_history.back();
m_history.pop_back();
delete t;
}
// Now we have the previous played round in back()
Turn *t = m_history.back();
t->setNum(0);
t->setPlayer(0);
//t->setRound(Round());
#ifdef BACK_REMOVE_RACK_NEW_PART
t->getPlayedRound().setNew(Rack());
#endif
}
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
for (unsigned int i = 0; i < m_history.size(); i++)
2005-12-26 16:33:15 +01:00
{
Turn *t = m_history[i];
rs += t->toString() + L"\n";
2005-12-26 16:33:15 +01:00
}
return rs;
}
/* ******************************************************** */
/* ******************************************************** */
/* ******************************************************** */
2005-12-26 16:33:15 +01:00
/// Local Variables:
2006-01-01 20:49:35 +01:00
/// mode: c++
/// mode: hs-minor
/// c-basic-offset: 4
2006-01-01 20:49:35 +01:00
/// indent-tabs-mode: nil
/// End: