00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00095
00096
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
00106 current_turn->setNum(turn);
00107 current_turn->setPlayer(player);
00108 current_turn->setRound(round);
00109
00110
00111 current_turn->getPlayedRack().getRack(rack);
00112
00113
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
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
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
00180
00181
00182
00183
00184