board_cross.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 /**
00022  *  \file   board_cross.cpp
00023  *  \brief  Build cross information used to speed up search
00024  *  \author Antoine Fraboulet & Olivier Teulière
00025  *  \date   2005
00026  */
00027 
00028 #include <dic.h>
00029 #include "tile.h"
00030 #include "board.h"
00031 #include "debug.h"
00032 
00033 static void Board_checkout_tile(const Dictionary &iDic,
00034                                 vector<Tile>& iTiles,
00035                                 vector<bool> & iJoker,
00036                                 Cross &oCross,
00037                                 int& oPoints,
00038                                 int index)
00039 {
00040     unsigned int node, succ;
00041     int j;
00042 
00043     oPoints = 0;
00044 
00045     /* Points on the left part */
00046     int i = index;
00047     while (!iTiles[i - 1].isEmpty())
00048     {
00049         i--;
00050         if (!iJoker[i])
00051             oPoints += iTiles[i].getPoints();
00052     }
00053 
00054     /* Tiles that can be played */
00055     // FIXME: create a temporary string until the dictionary uses Tile objects
00056     char leftTiles[BOARD_DIM + 1];
00057     for (j = i; j < index; j++)
00058         leftTiles[j - i] = toupper(iTiles[j].toChar());
00059     leftTiles[index - i] = 0;
00060     node = Dic_lookup(iDic, Dic_root(iDic), leftTiles);
00061     if (node == 0)
00062     {
00063         oCross.clear();
00064         return;
00065     }
00066 
00067     // FIXME: same thing for the right part
00068     char rightTiles[BOARD_DIM + 1];
00069     for (j = index + 1; !iTiles[j].isEmpty(); j++)
00070         rightTiles[j - index - 1] = toupper(iTiles[j].toChar());
00071     rightTiles[j - index - 1] = 0;
00072     for (succ = Dic_succ(iDic, node); succ; succ = Dic_next(iDic, succ))
00073     {
00074         if (Dic_word(iDic, Dic_lookup(iDic, succ, rightTiles)))
00075             oCross.insert(Tile(Dic_chr(iDic, succ)));
00076         if (Dic_last(iDic, succ))
00077             break;
00078     }
00079 
00080     /* Points on the right part */
00081     /* yes, it is REALLY [index+1] */
00082     while (!iTiles[index+1].isEmpty())
00083     {
00084         index++;
00085         if (!iJoker[index])
00086             oPoints += iTiles[index].getPoints();
00087     }
00088 }
00089 
00090 
00091 static void Board_check(const Dictionary &iDic,
00092                         Matrix<Tile> &iTilesMx,
00093                         Matrix<bool> &iJokerMx,
00094                         Matrix<Cross> &iCrossMx,
00095                         Matrix<int> &iPointMx)
00096 {
00097     for (int i = 1; i <= BOARD_DIM; i++)
00098     {
00099         for (int j = 1; j <= BOARD_DIM; j++)
00100         {
00101             iPointMx[j][i] = -1;
00102             if (!iTilesMx[i][j].isEmpty())
00103             {
00104                 iCrossMx[j][i].clear();
00105             }
00106             else if (!iTilesMx[i][j - 1].isEmpty() ||
00107                      !iTilesMx[i][j + 1].isEmpty())
00108             {
00109                 iCrossMx[j][i].clear();
00110                 Board_checkout_tile(iDic,
00111                                     iTilesMx[i],
00112                                     iJokerMx[i],
00113                                     iCrossMx[j][i],
00114                                     iPointMx[j][i],
00115                                     j);
00116             }
00117             else
00118             {
00119                 iCrossMx[j][i].setAny();
00120             }
00121         }
00122     }
00123 }
00124 
00125 
00126 void Board::buildCross(const Dictionary &iDic)
00127 {
00128     Board_check(iDic, m_tilesRow, m_jokerRow, m_crossCol, m_pointCol);
00129     Board_check(iDic, m_tilesCol, m_jokerCol, m_crossRow, m_pointRow);
00130 }
00131 
00132 
00133 /****************************************************************/
00134 /****************************************************************/
00135 
00136 /// Local Variables:
00137 /// mode: c++
00138 /// mode: hs-minor
00139 /// c-basic-offset: 4
00140 /// indent-tabs-mode: nil
00141 /// End:

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