tile.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 1999-2005 Eliot
00003  * Authors: Olivier Teuliere  <ipkiss@via.ecp.fr>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *****************************************************************************/
00019 
00020 #include "tile.h"
00021 #include <ctype.h>
00022 
00023 /*************************
00024  * French tiles
00025  * Zero + 26 letters + joker
00026  * tiles ares supposed to be contiguous and joker is separated
00027  *************************/
00028 
00029 #define TILE_START     'A'
00030 #define TILE_END       'Z'
00031 #define TILE_JOKER     '?'
00032 #define TILE_DUMMY     '%'
00033 
00034 #define TILE_IDX_DUMMY   0
00035 #define TILE_IDX_START   1
00036 #define TILE_IDX_END    26
00037 #define TILE_IDX_JOKER  27
00038 
00039 #define TILES_NUMBER    28
00040 
00041 /* The jokers and the 'Y' can be considered both as vowels or consonants */
00042 const unsigned int Tiles_vowels[TILES_NUMBER] =
00043 {
00044 /* x A B C D  E F G H I J  K L M N O P Q R S T U V  W  X  Y  Z ? */
00045    0,1,0,0,0, 1,0,0,0,1,0, 0,0,0,0,1,0,0,0,0,0,1,0, 0, 0, 1, 0,1
00046 };
00047 
00048 const unsigned int Tiles_consonants[TILES_NUMBER] =
00049 {
00050 /* x A B C D  E F G H I J  K L M N O P Q R S T U V  W  X  Y  Z ? */
00051    0,0,1,1,1, 0,1,1,1,0,1, 1,1,1,1,0,1,1,1,1,1,0,1, 1, 1, 1, 1,1
00052 };
00053 
00054 const unsigned int Tiles_numbers[TILES_NUMBER] =
00055 {
00056 /* x A B C D  E F G H I J  K L M N O P Q R S T U V  W  X  Y  Z ? */
00057    0,9,2,2,3,15,2,2,2,8,1, 1,5,3,6,6,2,1,6,6,6,6,2, 1, 1, 1, 1,2
00058 };
00059 
00060 const unsigned int Tiles_points[TILES_NUMBER] =
00061 {
00062 /* x A B C D  E F G H I J  K L M N O P Q R S T U V  W  X  Y  Z ? */
00063    0,1,3,3,2, 1,4,2,4,1,8,10,1,2,1,1,3,8,1,1,1,1,4,10,10,10,10,0
00064 };
00065 
00066 /***************************
00067  ***************************/
00068 
00069 list<Tile> Tile::m_tilesList;
00070 const Tile Tile::m_TheJoker(TILE_JOKER);
00071 const Tile Tile::m_TheDummy(0);
00072 
00073 
00074 Tile::Tile(char c)
00075 {
00076     if (c == TILE_JOKER)
00077     {
00078         m_joker = true;
00079         m_dummy = false;
00080         m_char = TILE_JOKER;
00081     }
00082     else if (isalpha(c))
00083     {
00084         m_joker = islower(c);
00085         m_dummy = false;
00086         m_char = toupper(c);
00087     }
00088     else
00089     {
00090         m_joker = false;
00091         m_dummy = true;
00092         m_char = 0;
00093     }
00094 }
00095 
00096 
00097 bool Tile::isVowel() const
00098 {
00099     if (m_dummy)
00100         return false;
00101     if (m_joker)
00102         return Tiles_vowels[TILE_IDX_JOKER];
00103     return Tiles_vowels[TILE_IDX_START + m_char - TILE_START];
00104 }
00105 
00106 
00107 bool Tile::isConsonant() const
00108 {
00109     if (m_dummy)
00110         return false;
00111     if (m_joker)
00112         return Tiles_consonants[TILE_IDX_JOKER];
00113     return Tiles_consonants[TILE_IDX_START + m_char - TILE_START];
00114 }
00115 
00116 
00117 unsigned int Tile::maxNumber() const
00118 {
00119     if (m_dummy)
00120         return false;
00121     if (m_joker)
00122         return Tiles_numbers[TILE_IDX_JOKER];
00123     return Tiles_numbers[TILE_IDX_START + m_char - TILE_START];
00124 }
00125 
00126 
00127 unsigned int Tile::getPoints() const
00128 {
00129     if (m_dummy)
00130         return false;
00131     if (m_joker)
00132         return Tiles_points[TILE_IDX_JOKER];
00133     return Tiles_points[TILE_IDX_START + m_char - TILE_START];
00134 }
00135 
00136 
00137 const list<Tile>& Tile::getAllTiles()
00138 {
00139     if (Tile::m_tilesList.size() == 0)
00140     {
00141         // XXX: this should be filled from a "language file" instead
00142         for (char i = TILE_START; i <= TILE_END; i++)
00143             Tile::m_tilesList.push_back(Tile(i));
00144         m_tilesList.push_back(Tile(TILE_JOKER));
00145     }
00146     return Tile::m_tilesList;
00147 }
00148 
00149 
00150 char Tile::toChar() const
00151 {
00152     if (m_dummy)
00153         return TILE_DUMMY;
00154     if (m_joker)
00155     {
00156         if (isalpha(m_char))
00157             return tolower(m_char);
00158         else
00159             return TILE_JOKER;
00160     }
00161     return m_char;
00162 }
00163 
00164 int Tile::toCode() const
00165 {
00166     if (m_dummy)
00167         return TILE_IDX_DUMMY;
00168     if (m_joker)
00169         return TILE_IDX_DUMMY;
00170     return (TILE_IDX_START + m_char - TILE_START);
00171 }
00172 
00173 bool Tile::operator <(const Tile &iOther) const
00174 {
00175     if (iOther.m_dummy)
00176         return false;
00177     else if (m_dummy)
00178         return true;
00179     else if (m_joker)
00180         return false;
00181     else if (iOther.m_joker)
00182         return true;
00183     else
00184         return m_char < iOther.m_char;
00185 }
00186 
00187 
00188 bool Tile::operator ==(const Tile &iOther) const
00189 {
00190     if (m_dummy || iOther.m_dummy)
00191         return m_dummy == iOther.m_dummy;
00192     if (m_joker || iOther.m_joker)
00193     {
00194         if (m_joker != iOther.m_joker)
00195             return false;
00196         return m_char == iOther.m_char;
00197     }
00198     return m_char == iOther.m_char;
00199 //     return (m_joker && iOther.m_joker && m_char == iOther.m_char) ||
00200 //            (m_dummy && iOther.m_dummy) ||
00201 //            (!m_dummy && !iOther.m_dummy
00202 //             && !m_joker && !iOther.m_joker
00203 //             && m_char == iOther.m_char);
00204 }
00205 
00206 
00207 bool Tile::operator !=(const Tile &iOther) const
00208 {
00209     return !(*this == iOther);
00210 }
00211 
00212 /// Local Variables:
00213 /// mode: c++
00214 /// mode: hs-minor
00215 /// c-basic-offset: 4
00216 /// indent-tabs-mode: nil
00217 /// End:

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