history.cpp

Go to the documentation of this file.
00001 /* Eliot                                                                     */
00002 /* Copyright (C) 1999  Antoine Fraboulet                                     */
00003 /*                                                                           */
00004 /* This file is part of Eliot.                                               */
00005 /*                                                                           */
00006 /* Eliot is free software; you can redistribute it and/or modify             */
00007 /* it under the terms of the GNU General Public License as published by      */
00008 /* the Free Software Foundation; either version 2 of the License, or         */
00009 /* (at your option) any later version.                                       */
00010 /*                                                                           */
00011 /* Eliot is distributed in the hope that it will be useful,                  */
00012 /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
00013 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
00014 /* GNU General Public License for more details.                              */
00015 /*                                                                           */
00016 /* You should have received a copy of the GNU General Public License         */
00017 /* along with this program; if not, write to the Free Software               */
00018 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00019 
00020 /**
00021  *  \file   history.cpp
00022  *  \brief  Game history  system
00023  *  \author Antoine Fraboulet
00024  *  \date   2005
00025  */
00026 
00027 #include <string>
00028 #include "rack.h"
00029 #include "pldrack.h"
00030 #include "round.h"
00031 #include "turn.h"
00032 #include "debug.h"
00033 #include "history.h"
00034 
00035 /* ******************************************************** */
00036 /* ******************************************************** */
00037 /* ******************************************************** */
00038 
00039 
00040 History::History()
00041 {
00042     Turn* t = new Turn ();
00043     m_history.clear();
00044     m_history.push_back(t);
00045 }
00046 
00047 
00048 History::~History()
00049 {
00050     for (unsigned int i = 0; i < m_history.size(); i++)
00051     {
00052         if (m_history[i] != NULL)
00053         {
00054             delete m_history[i];
00055             m_history[i] = NULL;
00056         }
00057     }
00058 }
00059 
00060 
00061 int History::getSize() const
00062 {
00063     return m_history.size() - 1;
00064 }
00065 
00066 
00067 const PlayedRack& History::getCurrentRack() const
00068 {
00069     return m_history.back()->getPlayedRack();
00070 }
00071 
00072 
00073 void History::setCurrentRack(const PlayedRack &iPld)
00074 {
00075     m_history.back()->setPlayedRack(iPld);
00076 }
00077 
00078 
00079 const Turn& History::getPreviousTurn() const
00080 {
00081     int idx = m_history.size() - 2;
00082     ASSERT(0 <= idx , "No previous turn");
00083     return *(m_history[idx]);
00084 }
00085 
00086 
00087 const Turn& History::getTurn(unsigned int n) const
00088 {
00089     ASSERT(0 <= n && n < m_history.size(), "Wrong turn number");
00090     return *(m_history[n]);
00091 }
00092 
00093 /*
00094  * This function increments the number of racks, and fills the new rack
00095  * with the unplayed tiles from the previous one.
00096  * 03 sept 2000 : We have to sort the tiles according to the new rules
00097  */
00098 void History::playRound(int player, int turn, const Round& round)
00099 {
00100     Rack rack;
00101     Turn * current_turn;
00102 
00103     current_turn = m_history.back();
00104 
00105     /* set the number and the round */
00106     current_turn->setNum(turn);
00107     current_turn->setPlayer(player);
00108     current_turn->setRound(round);
00109 
00110     /* get what was the rack for the current turn */
00111     current_turn->getPlayedRack().getRack(rack);
00112 
00113     /* remove the played tiles from the rack */
00114     for (int i = 0; i < round.getWordLen(); i++)
00115     {
00116         if (round.isPlayedFromRack(i))
00117         {
00118             if (round.isJoker(i))
00119                 rack.remove(Tile::Joker());
00120             else
00121                 rack.remove(round.getTile(i));
00122         }
00123     }
00124 
00125     /* create a new turn */
00126     Turn * next_turn = new Turn();
00127     PlayedRack pldrack;
00128     pldrack.setOld(rack);
00129     next_turn->setPlayedRack(pldrack);
00130     m_history.push_back(next_turn);
00131 }
00132 
00133 
00134 void History::removeLastTurn()
00135 {
00136     int idx = m_history.size();
00137     ASSERT(0 < idx , "Wrong turn number");
00138 
00139     if (idx > 1)
00140     {
00141         Turn *t = m_history.back();
00142         m_history.pop_back();
00143         delete t;
00144     }
00145 
00146     // now we have the previous played round in back()
00147     Turn* t = m_history.back();
00148     t->setNum(0);
00149     t->setPlayer(0);
00150     t->setRound(Round());
00151 #ifdef BACK_REMOVE_RACK_NEW_PART
00152     t->getPlayedRound().setNew(Rack());
00153 #endif
00154 }
00155 
00156 
00157 string History::toString() const
00158 {
00159     unsigned int i;
00160     string rs = "";
00161 #ifdef DEBUG
00162     char buff[20];
00163     sprintf(buff,"%d",m_history.size());
00164     rs = "history size = " + string(buff) + "\n\n";
00165 #endif
00166     for (i = 0; i < m_history.size(); i++)
00167     {
00168         Turn *t = m_history[i];
00169         rs += t->toString() + string("\n");
00170     }
00171     return rs;
00172 }
00173 
00174 /* ******************************************************** */
00175 /* ******************************************************** */
00176 /* ******************************************************** */
00177 
00178 
00179 /// Local Variables:
00180 /// mode: c++
00181 /// mode: hs-minor
00182 /// c-basic-offset: 4
00183 /// indent-tabs-mode: nil
00184 /// End:

Generated on Thu Dec 29 02:01:14 2005 for Eliot by  doxygen 1.4.5