eliot-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Eliot-dev] eliot/game board.cpp board.h board_search.cpp b...


From: Olivier Teulière
Subject: [Eliot-dev] eliot/game board.cpp board.h board_search.cpp b...
Date: Sat, 17 Jan 2009 14:57:33 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Changes by:     Olivier Teulière <ipkiss>       09/01/17 14:57:33

Modified files:
        game           : board.cpp board.h board_search.cpp 
Added files:
        game           : board_search.h matrix.h 

Log message:
        New BoardSearch class to avoid carrying const parameters all over the 
place.
        No functional change.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/game/board.cpp?cvsroot=eliot&r1=1.21&r2=1.22
http://cvs.savannah.gnu.org/viewcvs/eliot/game/board.h?cvsroot=eliot&r1=1.17&r2=1.18
http://cvs.savannah.gnu.org/viewcvs/eliot/game/board_search.cpp?cvsroot=eliot&r1=1.17&r2=1.18
http://cvs.savannah.gnu.org/viewcvs/eliot/game/board_search.h?cvsroot=eliot&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/eliot/game/matrix.h?cvsroot=eliot&rev=1.1

Patches:
Index: board.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/board.cpp,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -b -r1.21 -r1.22
--- board.cpp   22 Nov 2008 13:09:29 -0000      1.21
+++ board.cpp   17 Jan 2009 14:57:32 -0000      1.22
@@ -20,13 +20,17 @@
  *****************************************************************************/
 
 #include <wctype.h>
+
 #include "dic.h"
+
+#include "board.h"
+#include "board_search.h"
 #include "tile.h"
 #include "round.h"
 #include "rack.h"
 #include "results.h"
-#include "board.h"
 #include "debug.h"
+// FIXME: should not be included here
 #include "game.h"
 
 #define oo 0
@@ -509,23 +513,45 @@
         {
             if (m_tilesRow[row][col] != m_tilesCol[col][row])
                 printf("tiles diff %d %d\n", row, col);
-
-            // The crossckecks and the points have no reason to be the same
-            // in both directions
-            /*
-            if (m_crossRow[row][col] != m_crossCol[col][row])
-            {
-                printf("cross diff %d %d\n",row,col);
-            }
-
-            if (m_pointRow[row][col] != m_pointCol[col][row])
-                printf("point diff %d %d\n",row,col);
-            */
-
             if (m_jokerRow[row][col] != m_jokerCol[col][row])
                 printf("joker diff %d %d\n", row, col);
+            // The crossckecks and the points have no reason to be the same
+            // in both directions
         }
     }
 }
 #endif
 
+
+void Board::search(const Dictionary &iDic,
+                   const Rack &iRack,
+                   Results &oResults) const
+{
+    // Create a copy of the rack to avoid modifying the given one
+    Rack copyRack = iRack;
+
+    // Search horizontal words
+    BoardSearch horizSearch(iDic, m_tilesRow, m_crossRow,
+                            m_pointRow, m_jokerRow);
+    horizSearch.search(copyRack, oResults, Coord::HORIZONTAL);
+
+    // Search vertical words
+    BoardSearch vertSearch(iDic, m_tilesCol, m_crossCol,
+                            m_pointCol, m_jokerCol);
+    vertSearch.search(copyRack, oResults, Coord::VERTICAL);
+}
+
+
+void Board::searchFirst(const Dictionary &iDic,
+                        const Rack &iRack,
+                        Results &oResults) const
+{
+    // Create a copy of the rack to avoid modifying the given one
+    Rack copyRack = iRack;
+
+    // Search horizontal words
+    BoardSearch horizSearch(iDic, m_tilesRow, m_crossRow,
+                            m_pointRow, m_jokerRow, true);
+    horizSearch.search(copyRack, oResults, Coord::HORIZONTAL);
+}
+

Index: board.h
===================================================================
RCS file: /cvsroot/eliot/eliot/game/board.h,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -b -r1.17 -r1.18
--- board.h     22 Nov 2008 13:09:29 -0000      1.17
+++ board.h     17 Jan 2009 14:57:32 -0000      1.18
@@ -23,8 +23,8 @@
 #define _BOARD_H_
 
 #include <string>
-#include <vector>
 
+#include "matrix.h"
 #include "tile.h"
 #include "cross.h"
 
@@ -41,24 +41,6 @@
 #define BOARD_REALDIM (BOARD_DIM + 2)
 
 
-// Template matrix class for convenience.
-template <class T>
-class Matrix: public vector<vector<T> >
-{
-public:
-    // Construct a matrix with an initial value
-    Matrix(int iSize1, int iSize2, const T &iValue)
-    {
-        resize(iSize1, vector<T>(iSize2, iValue));
-    }
-    // Construct a square matrix with an initial value
-    Matrix(int iSize, const T &iValue)
-    {
-        resize(iSize, vector<T>(iSize, iValue));
-    }
-};
-
-
 class Board
 {
 public:

Index: board_search.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/game/board_search.cpp,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -b -r1.17 -r1.18
--- board_search.cpp    22 Nov 2008 13:09:30 -0000      1.17
+++ board_search.cpp    17 Jan 2009 14:57:32 -0000      1.18
@@ -1,6 +1,6 @@
 /*****************************************************************************
  * Eliot
- * Copyright (C) 1999-2007 Antoine Fraboulet & Olivier Teulière
+ * Copyright (C) 1999-2009 Antoine Fraboulet & Olivier Teulière
  * Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
  *          Olivier Teulière <ipkiss @@ gmail.com>
  *
@@ -19,116 +19,183 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *****************************************************************************/
 
-#include <dic.h>
+#include "dic.h"
+
+#include "board_search.h"
+#include "board.h"
 #include "tile.h"
 #include "rack.h"
 #include "round.h"
 #include "results.h"
-#include "board.h"
-#include "encoding.h"
-#include "debug.h"
+// FIXME: should not be included here
 #include "game.h"
 
-/*
- * computes the score of a word, coordinates may be changed to reflect
- * the real direction of the word
- */
-static void BoardSearchEvalMove(const Board &iBoard,
+
+BoardSearch::BoardSearch(const Dictionary &iDic,
                                 const Matrix<Tile> &iTilesMx,
+                         const Matrix<Cross> &iCrossMx,
                                 const Matrix<int> &iPointsMx,
                                 const Matrix<bool> &iJokerMx,
-                                Results &iResults, Round &iWord)
+                         bool isFirstTurn)
+    : m_dic(iDic), m_tilesMx(iTilesMx), m_crossMx(iCrossMx),
+      m_pointsMx(iPointsMx), m_jokerMx(iJokerMx), m_firstTurn(isFirstTurn)
 {
-    unsigned int fromrack = 0;
-    int pts      = 0;
-    int ptscross = 0;
-    int wordmul  = 1;
-
-    unsigned int len = iWord.getWordLen();
+}
 
-    int row = iWord.getCoord().getRow();
-    int col = iWord.getCoord().getCol();
 
-    for (unsigned int i = 0; i < len; i++)
+void BoardSearch::search(Rack &iRack, Results &oResults, Coord::Direction 
iDir) const
+{
+    // Handle the first turn specifically
+    if (m_firstTurn)
     {
-        if (!iTilesMx[row][col+i].isEmpty())
+        const int row = 8, col = 8;
+        Round tmpRound;
+        tmpRound.accessCoord().setRow(row);
+        tmpRound.accessCoord().setCol(col);
+        tmpRound.accessCoord().setDir(Coord::HORIZONTAL);
+        leftPart(iRack, tmpRound, oResults, m_dic.getRoot(),
+                 row, col, iRack.getNbTiles() - 1);
+        return;
+    }
+
+
+    vector<Tile> rackTiles;
+    iRack.getTiles(rackTiles);
+    vector<Tile>::const_iterator it;
+    Round partialWord;
+
+    for (int row = 1; row <= BOARD_DIM; row++)
         {
-            if (!iJokerMx[row][col+i])
-                pts += iWord.getTile(i).getPoints();
+        partialWord.init();
+        partialWord.accessCoord().setDir(iDir);
+        partialWord.accessCoord().setRow(row);
+        int lastanchor = 0;
+        for (int col = 1; col <= BOARD_DIM; col++)
+        {
+            if (m_tilesMx[row][col].isEmpty() &&
+                (!m_tilesMx[row][col - 1].isEmpty() ||
+                 !m_tilesMx[row][col + 1].isEmpty() ||
+                 !m_tilesMx[row - 1][col].isEmpty() ||
+                 !m_tilesMx[row + 1][col].isEmpty()))
+            {
+#ifdef DONT_USE_SEARCH_OPTIMIZATION
+                if (!m_tilesMx[row][col - 1].isEmpty())
+                {
+                    partialWord.accessCoord().setCol(lastanchor + 1);
+                    extendRight(iRack, partialWord, oResults,
+                                Dic_root(m_dic), row, lastanchor + 1, col);
         }
         else
         {
-            int l;
-            if (!iWord.isJoker(i))
-                l = iWord.getTile(i).getPoints() *
-                    iBoard.GetLetterMultiplier(row, col + i);
+                    partialWord.accessCoord().setCol(col);
+                    leftPart(iRack, partialWord, oResults,
+                             Dic_root(m_dic), row, col, col - lastanchor - 1);
+                }
+                lastanchor = col;
+#else
+                // Optimization compared to the original Appel & Jacobson
+                // algorithm: skip leftPart if none of the tiles of the rack
+                // matches the cross mask for the current anchor
+                bool match = false;
+                for (it = rackTiles.begin(); it != rackTiles.end(); it++)
+                {
+                    if (m_crossMx[row][col].check(*it))
+                    {
+                        match = true;
+                        break;
+                    }
+                }
+                if (match)
+                {
+                    if (!m_tilesMx[row][col - 1].isEmpty())
+                    {
+                        partialWord.accessCoord().setCol(lastanchor + 1);
+                        extendRight(iRack, partialWord, oResults,
+                                    m_dic.getRoot(), row, lastanchor + 1, col);
+                    }
             else
-                l = 0;
-            pts += l;
-            wordmul *= iBoard.GetWordMultiplier(row, col + i);
-
-            int t = iPointsMx[row][col+i];
-            if (t >= 0)
-                ptscross += (t + l) * iBoard.GetWordMultiplier(row, col + i);
-            fromrack++;
+                    {
+                        partialWord.accessCoord().setCol(col);
+                        leftPart(iRack, partialWord, oResults,
+                                 m_dic.getRoot(), row, col, col - lastanchor - 
1);
+                    }
+                }
+                lastanchor = col;
+#endif
         }
     }
+    }
+}
 
-    pts = ptscross + pts * wordmul + Game::BONUS_POINTS * (fromrack == 
Game::RACK_SIZE);
-    iWord.setBonus(fromrack == Game::RACK_SIZE);
-    iWord.setPoints(pts);
 
-    if (iWord.getCoord().getDir() == Coord::VERTICAL)
+void BoardSearch::leftPart(Rack &iRack, Round &ioPartialWord,
+                           Results &oResults, int n, int iRow,
+                           int iAnchor, int iLimit) const
+{
+    extendRight(iRack, ioPartialWord, oResults, n, iRow, iAnchor, iAnchor);
+
+    if (iLimit > 0)
     {
-        // Exchange the coordinates temporarily
-        iWord.accessCoord().swap();
+        Tile l;
+        bool hasJokerInRack = iRack.in(Tile::Joker());
+        for (unsigned int succ = m_dic.getSucc(n); succ; succ = 
m_dic.getNext(succ))
+        {
+            l = Tile(m_dic.getChar(succ));
+            if (iRack.in(l))
+            {
+                iRack.remove(l);
+                ioPartialWord.addRightFromRack(l, false);
+                
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() - 1);
+                leftPart(iRack, ioPartialWord, oResults,
+                         succ, iRow, iAnchor, iLimit - 1);
+                
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
+                ioPartialWord.removeRightToRack(l, false);
+                iRack.add(l);
     }
-    iResults.add(iWord);
-    if (iWord.getCoord().getDir() == Coord::VERTICAL)
+            if (hasJokerInRack)
     {
-        // Restore the coordinates
-        iWord.accessCoord().swap();
+                iRack.remove(Tile::Joker());
+                ioPartialWord.addRightFromRack(l, true);
+                
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() - 1);
+                leftPart(iRack, ioPartialWord, oResults,
+                         succ, iRow, iAnchor, iLimit - 1);
+                
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
+                ioPartialWord.removeRightToRack(l, true);
+                iRack.add(Tile::Joker());
+            }
+        }
     }
 }
 
 
-static void ExtendRight(const Board &iBoard,
-                        const Dictionary &iDic,
-                        const Matrix<Tile> &iTilesMx,
-                        const Matrix<Cross> &iCrossMx,
-                        const Matrix<int> &iPointsMx,
-                        const Matrix<bool> &iJokerMx,
-                        Rack &iRack, Round &ioPartialWord,
-                        Results &iResults, unsigned int iNode,
-                        int iRow, int iCol, int iAnchor)
+void BoardSearch::extendRight(Rack &iRack, Round &ioPartialWord,
+                              Results &oResults, unsigned int iNode,
+                              int iRow, int iCol, int iAnchor) const
 {
     Tile l;
-    unsigned int succ;
 
-    if (iTilesMx[iRow][iCol].isEmpty())
+    if (m_tilesMx[iRow][iCol].isEmpty())
     {
-        if (iDic.isEndOfWord(iNode) && iCol > iAnchor)
+        if (m_dic.isEndOfWord(iNode) && iCol > iAnchor)
         {
-            BoardSearchEvalMove(iBoard, iTilesMx, iPointsMx, iJokerMx,
-                                iResults, ioPartialWord);
+            evalMove(oResults, ioPartialWord);
         }
 
         // Optimization: avoid entering the for loop if no tile can match
-        if (iCrossMx[iRow][iCol].isNone())
+        if (m_crossMx[iRow][iCol].isNone())
             return;
 
         bool hasJokerInRack = iRack.in(Tile::Joker());
-        for (succ = iDic.getSucc(iNode); succ; succ = iDic.getNext(succ))
+        for (unsigned int succ = m_dic.getSucc(iNode); succ; succ = 
m_dic.getNext(succ))
         {
-            l = Tile(iDic.getChar(succ));
-            if (iCrossMx[iRow][iCol].check(l))
+            l = Tile(m_dic.getChar(succ));
+            if (m_crossMx[iRow][iCol].check(l))
             {
                 if (iRack.in(l))
                 {
                     iRack.remove(l);
                     ioPartialWord.addRightFromRack(l, false);
-                    ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
-                                iJokerMx, iRack, ioPartialWord, iResults,
+                    extendRight(iRack, ioPartialWord, oResults,
                                 succ, iRow, iCol + 1, iAnchor);
                     ioPartialWord.removeRightToRack(l, false);
                     iRack.add(l);
@@ -137,8 +204,7 @@
                 {
                     iRack.remove(Tile::Joker());
                     ioPartialWord.addRightFromRack(l, true);
-                    ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
-                                iJokerMx, iRack, ioPartialWord, iResults,
+                    extendRight(iRack, ioPartialWord, oResults,
                                 succ, iRow, iCol + 1, iAnchor);
                     ioPartialWord.removeRightToRack(l, true);
                     iRack.add(Tile::Joker());
@@ -148,16 +214,15 @@
     }
     else
     {
-        l = iTilesMx[iRow][iCol];
+        l = m_tilesMx[iRow][iCol];
         wint_t upperChar = towupper(l.toChar());
-        for (succ = iDic.getSucc(iNode); succ ; succ = iDic.getNext(succ))
+        for (unsigned int succ = m_dic.getSucc(iNode); succ ; succ = 
m_dic.getNext(succ))
         {
-            if ((wint_t)iDic.getChar(succ) == upperChar)
+            if ((wint_t)m_dic.getChar(succ) == upperChar)
             {
                 ioPartialWord.addRightFromBoard(l);
-                ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
-                            iJokerMx, iRack, ioPartialWord,
-                            iResults, succ, iRow, iCol + 1, iAnchor);
+                extendRight(iRack, ioPartialWord,
+                            oResults, succ, iRow, iCol + 1, iAnchor);
                 ioPartialWord.removeRightToBoard(l);
                 // The letter will be present only once in the dictionary,
                 // so we can stop looping
@@ -168,177 +233,61 @@
 }
 
 
-static void LeftPart(const Board &iBoard,
-                     const Dictionary &iDic,
-                     const Matrix<Tile> &iTilesMx,
-                     const Matrix<Cross> &iCrossMx,
-                     const Matrix<int> &iPointsMx,
-                     const Matrix<bool> &iJokerMx,
-                     Rack &iRack, Round &ioPartialWord,
-                     Results &iResults, int n, int iRow,
-                     int iAnchor, int iLimit)
+/*
+ * Computes the score of a word, coordinates may be changed to reflect
+ * the real direction of the word
+ */
+void BoardSearch::evalMove(Results &oResults, Round &iWord) const
 {
-    Tile l;
-    int succ;
+    unsigned int fromrack = 0;
+    int pts      = 0;
+    int ptscross = 0;
+    int wordmul  = 1;
 
-    ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx, iJokerMx, iRack,
-                ioPartialWord, iResults, n, iRow, iAnchor, iAnchor);
+    unsigned int len = iWord.getWordLen();
 
-    if (iLimit > 0)
-    {
-        bool hasJokerInRack = iRack.in(Tile::Joker());
-        for (succ = iDic.getSucc(n); succ; succ = iDic.getNext(succ))
+    int row = iWord.getCoord().getRow();
+    int col = iWord.getCoord().getCol();
+
+    for (unsigned int i = 0; i < len; i++)
         {
-            l = Tile(iDic.getChar(succ));
-            if (iRack.in(l))
+        if (!m_tilesMx[row][col+i].isEmpty())
             {
-                iRack.remove(l);
-                ioPartialWord.addRightFromRack(l, false);
-                
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() - 1);
-                LeftPart(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
-                         iJokerMx, iRack, ioPartialWord, iResults,
-                         succ, iRow, iAnchor, iLimit - 1);
-                
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
-                ioPartialWord.removeRightToRack(l, false);
-                iRack.add(l);
+            if (!m_jokerMx[row][col+i])
+                pts += iWord.getTile(i).getPoints();
             }
-            if (hasJokerInRack)
+        else
             {
-                iRack.remove(Tile::Joker());
-                ioPartialWord.addRightFromRack(l, true);
-                
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() - 1);
-                LeftPart(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
-                         iJokerMx, iRack, ioPartialWord, iResults,
-                         succ, iRow, iAnchor, iLimit - 1);
-                
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
-                ioPartialWord.removeRightToRack(l, true);
-                iRack.add(Tile::Joker());
-            }
+            int l;
+            if (!iWord.isJoker(i))
+                l = iWord.getTile(i).getPoints() *
+                    Board::GetLetterMultiplier(row, col + i);
+            else
+                l = 0;
+            pts += l;
+            wordmul *= Board::GetWordMultiplier(row, col + i);
+
+            int t = m_pointsMx[row][col+i];
+            if (t >= 0)
+                ptscross += (t + l) * Board::GetWordMultiplier(row, col + i);
+            fromrack++;
         }
     }
-}
 
+    pts = ptscross + pts * wordmul + Game::BONUS_POINTS * (fromrack == 
Game::RACK_SIZE);
+    iWord.setBonus(fromrack == Game::RACK_SIZE);
+    iWord.setPoints(pts);
 
-static void BoardSearchAux(const Board &iBoard,
-                           const Dictionary &iDic,
-                           const Matrix<Tile> &iTilesMx,
-                           const Matrix<Cross> &iCrossMx,
-                           const Matrix<int> &iPointsMx,
-                           const Matrix<bool> &iJokerMx,
-                           Rack &iRack, Results &iResults,
-                           Coord::Direction iDir)
-{
-    int row, col, lastanchor;
-    Round partialword;
-
-    vector<Tile> rackTiles;
-    iRack.getTiles(rackTiles);
-    vector<Tile>::const_iterator it;
-    vector<Tile>::const_iterator itEnd;
-
-    for (row = 1; row <= BOARD_DIM; row++)
-    {
-        partialword.init();
-        partialword.accessCoord().setDir(iDir);
-        partialword.accessCoord().setRow(row);
-        lastanchor = 0;
-        for (col = 1; col <= BOARD_DIM; col++)
-        {
-            if (iTilesMx[row][col].isEmpty() &&
-                (!iTilesMx[row][col - 1].isEmpty() ||
-                 !iTilesMx[row][col + 1].isEmpty() ||
-                 !iTilesMx[row - 1][col].isEmpty() ||
-                 !iTilesMx[row + 1][col].isEmpty()))
-            {
-#if defined(DONT_USE_SEARCH_OPTIMIZATION)
-                if (!iTilesMx[row][col - 1].isEmpty())
-                {
-                    partialword.accessCoord().setCol(lastanchor + 1);
-                    ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
-                                iJokerMx, iRack, partialword, iResults,
-                                Dic_root(iDic), row, lastanchor + 1, col);
-                }
-                else
-                {
-                    partialword.accessCoord().setCol(col);
-                    LeftPart(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
-                             iJokerMx, iRack, partialword, iResults,
-                             Dic_root(iDic), row, col, col -
-                             lastanchor - 1);
-                }
-                lastanchor = col;
-#else
-                // Optimization compared to the original Appel & Jacobson
-                // algorithm: skip Leftpart if none of the tiles of the rack
-                // matches the cross mask for the current anchor
-                bool match = false;
-                for (it = rackTiles.begin(); it != rackTiles.end(); it++)
-                {
-                    if (iCrossMx[row][col].check(*it))
-                    {
-                        match = true;
-                        break;
-                    }
-                }
-                if (match)
-                {
-                    if (!iTilesMx[row][col - 1].isEmpty())
+    if (iWord.getCoord().getDir() == Coord::VERTICAL)
                     {
-                        partialword.accessCoord().setCol(lastanchor + 1);
-                        ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, 
iPointsMx,
-                                    iJokerMx, iRack, partialword, iResults,
-                                    iDic.getRoot(), row, lastanchor + 1, col);
+        // Exchange the coordinates temporarily
+        iWord.accessCoord().swap();
                     }
-                    else
+    oResults.add(iWord);
+    if (iWord.getCoord().getDir() == Coord::VERTICAL)
                     {
-                        partialword.accessCoord().setCol(col);
-                        LeftPart(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
-                                 iJokerMx, iRack, partialword, iResults,
-                                 iDic.getRoot(), row, col, col -
-                                 lastanchor - 1);
-                    }
-                }
-                lastanchor = col;
-#endif
-            }
-        }
+        // Restore the coordinates
+        iWord.accessCoord().swap();
     }
 }
 
-
-void Board::search(const Dictionary &iDic,
-                   const Rack &iRack,
-                   Results &oResults) const
-{
-    // Create a copy of the rack to avoid modifying the given one
-    Rack copyRack = iRack;
-
-    BoardSearchAux(*this, iDic, m_tilesRow, m_crossRow,
-                   m_pointRow, m_jokerRow,
-                   copyRack, oResults, Coord::HORIZONTAL);
-
-    BoardSearchAux(*this, iDic, m_tilesCol, m_crossCol,
-                   m_pointCol, m_jokerCol,
-                   copyRack, oResults, Coord::VERTICAL);
-}
-
-
-void Board::searchFirst(const Dictionary &iDic,
-                        const Rack &iRack,
-                        Results &oResults) const
-{
-    int row = 8, col = 8;
-
-    // Create a copy of the rack to avoid modifying the given one
-    Rack copyRack = iRack;
-
-    Round partialword;
-    partialword.accessCoord().setRow(row);
-    partialword.accessCoord().setCol(col);
-    partialword.accessCoord().setDir(Coord::HORIZONTAL);
-    LeftPart(*this, iDic, m_tilesRow, m_crossRow,
-             m_pointRow, m_jokerRow,
-             copyRack, partialword, oResults, iDic.getRoot(), row, col,
-             copyRack.getNbTiles() - 1);
-}
-

Index: board_search.h
===================================================================
RCS file: board_search.h
diff -N board_search.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ board_search.h      17 Jan 2009 14:57:32 -0000      1.1
@@ -0,0 +1,67 @@
+/*****************************************************************************
+ * Eliot
+ * Copyright (C) 2009 Olivier Teulière
+ * Authors: Olivier Teulière <ipkiss @@ gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *****************************************************************************/
+
+#ifndef _BOARD_SEARCH_H_
+#define _BOARD_SEARCH_H_
+
+#include "coord.h"
+#include "matrix.h"
+
+class Dictionary;
+class Rack;
+class Results;
+class Round;
+class Cross;
+
+
+class BoardSearch
+{
+public:
+    BoardSearch(const Dictionary &iDic,
+                const Matrix<Tile> &iTilesMx,
+                const Matrix<Cross> &iCrossMx,
+                const Matrix<int> &iPointsMx,
+                const Matrix<bool> &iJokerMx,
+                bool isFirstTurn = false);
+
+    void search(Rack &iRack, Results &oResults, Coord::Direction iDir) const;
+
+private:
+    const Dictionary &m_dic;
+    const Matrix<Tile> &m_tilesMx;
+    const Matrix<Cross> &m_crossMx;
+    const Matrix<int> &m_pointsMx;
+    const Matrix<bool> &m_jokerMx;
+    const bool m_firstTurn;
+
+    void leftPart(Rack &iRack, Round &ioPartialWord,
+                  Results &oResults, int n, int iRow,
+                  int iAnchor, int iLimit) const;
+
+    void extendRight(Rack &iRack, Round &ioPartialWord,
+                     Results &oResults, unsigned int iNode,
+                     int iRow, int iCol, int iAnchor) const;
+
+    void evalMove(Results &oResults, Round &iWord) const;
+};
+
+#endif
+
+

Index: matrix.h
===================================================================
RCS file: matrix.h
diff -N matrix.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ matrix.h    17 Jan 2009 14:57:33 -0000      1.1
@@ -0,0 +1,47 @@
+/*****************************************************************************
+ * Eliot
+ * Copyright (C) 2003-2007 Olivier Teulière
+ * Authors: Olivier Teulière <ipkiss @@ gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *****************************************************************************/
+
+#ifndef _MATRIX_H_
+#define _MATRIX_H_
+
+#include <vector>
+
+using std::vector;
+
+
+// Template matrix class for convenience.
+template <class T>
+class Matrix: public vector<vector<T> >
+{
+public:
+    // Construct a matrix with an initial value
+    Matrix(int iSize1, int iSize2, const T &iValue)
+    {
+        resize(iSize1, vector<T>(iSize2, iValue));
+    }
+    // Construct a square matrix with an initial value
+    Matrix(int iSize, const T &iValue)
+    {
+        resize(iSize, vector<T>(iSize, iValue));
+    }
+};
+
+#endif
+




reply via email to

[Prev in Thread] Current Thread [Next in Thread]