round.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 1999-2005 Eliot
00003  * Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
00004  *          Olivier Teuliere  <ipkiss@via.ecp.fr>
00005  *
00006  * This program 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  * This program 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 #include <string>
00022 #include "tile.h"
00023 #include "round.h"
00024 
00025 
00026 #define FROMBOARD 0x1
00027 #define FROMRACK  0x2
00028 #define JOKER     0x4
00029 
00030 
00031 Round::Round()
00032 {
00033     init();
00034 }
00035 
00036 
00037 void Round::init()
00038 {
00039     m_word.clear();
00040     m_tileOrigin.clear();
00041     m_coord.setRow(1);
00042     m_coord.setCol(1);
00043     m_coord.setDir(Coord::HORIZONTAL);
00044     m_points = 0;
00045     m_bonus  = 0;
00046 }
00047 
00048 
00049 void Round::setWord(const vector<Tile> &iTiles)
00050 {
00051     m_word.clear();
00052 
00053     vector<Tile>::const_iterator it;
00054     for (it = iTiles.begin(); it != iTiles.end(); it++)
00055     {
00056         m_word.push_back(*it);
00057         // XXX: always from rack?
00058         m_tileOrigin.push_back(FROMRACK);
00059     }
00060 }
00061 
00062 
00063 void Round::setFromRack(int iIndex)
00064 {
00065     m_tileOrigin[iIndex] &= ~FROMBOARD;
00066     m_tileOrigin[iIndex] |= FROMRACK;
00067 }
00068 
00069 
00070 void Round::setFromBoard(int iIndex)
00071 {
00072     m_tileOrigin[iIndex] &= ~FROMRACK;
00073     m_tileOrigin[iIndex] |= FROMBOARD;
00074 }
00075 
00076 
00077 void Round::setJoker(int iIndex, bool value)
00078 {
00079     if (value)
00080         m_tileOrigin[iIndex] |= JOKER;
00081     else
00082         m_tileOrigin[iIndex] &= ~JOKER;
00083 }
00084 
00085 
00086 bool Round::isJoker(int iIndex) const
00087 {
00088      return m_tileOrigin[iIndex] & JOKER;
00089 }
00090 
00091 
00092 const Tile& Round::getTile(int iIndex) const
00093 {
00094      return m_word[iIndex];
00095 }
00096 
00097 
00098 int Round::getWordLen() const
00099 {
00100      return m_word.size();
00101 }
00102 
00103 
00104 bool Round::isPlayedFromRack(int iIndex) const
00105 {
00106      return m_tileOrigin[iIndex] & FROMRACK;
00107 }
00108 
00109 
00110 void Round::addRightFromBoard(Tile c)
00111 {
00112     m_word.push_back(c);
00113     m_tileOrigin.push_back(FROMBOARD);
00114 }
00115 
00116 
00117 void Round::removeRightToBoard(Tile c)
00118 {
00119     // c is unused.
00120     m_word.pop_back();
00121     m_tileOrigin.pop_back();
00122 }
00123 
00124 
00125 void Round::addRightFromRack(Tile c, bool iJoker)
00126 {
00127     m_word.push_back(c);
00128     char origin = FROMRACK;
00129     if (iJoker)
00130     {
00131         origin |= JOKER;
00132     }
00133     m_tileOrigin.push_back(origin);
00134 }
00135 
00136 
00137 void Round::removeRightToRack(Tile c, bool iJoker)
00138 {
00139     // c is unused.
00140     m_word.pop_back();
00141     m_tileOrigin.pop_back();
00142 }
00143 
00144 string Round::getWord() const
00145 {
00146   char c;
00147   string s;
00148 
00149   for (int i = 0; i < getWordLen(); i++)
00150     {
00151       c = getTile(i).toChar();
00152       if (isJoker(i))
00153         c = tolower(c);
00154       s += c;
00155     }
00156   return s;
00157 }
00158 
00159 string Round::toString() const
00160 {
00161     char buff[5];
00162     string rs(" ");
00163 
00164     if (getWord().size() > 0)
00165     {
00166         rs  = getWord();
00167         rs += string(16 - getWord().size(), ' ');
00168         rs += getBonus() ? '*' : ' ';
00169         sprintf(buff,"%4d",getPoints());
00170         rs += buff;
00171         rs += " " + getCoord().toString();
00172     }
00173 
00174     return rs;
00175 }
00176 
00177 /// Local Variables:
00178 /// mode: c++
00179 /// mode: hs-minor
00180 /// c-basic-offset: 4
00181 /// indent-tabs-mode: nil
00182 /// End:

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