2005-02-05 12:14:56 +01:00
|
|
|
/*****************************************************************************
|
2008-01-08 14:52:32 +01:00
|
|
|
* Eliot
|
2012-10-07 16:25:41 +02:00
|
|
|
* Copyright (C) 1999-2012 Antoine Fraboulet & Olivier Teulière
|
2008-01-08 14:52:32 +01:00
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
|
|
|
|
* Olivier Teulière <ipkiss @@ gmail.com>
|
2005-02-05 12:14:56 +01:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-23 16:53:42 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-02-05 12:14:56 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-01-30 00:47:20 +01:00
|
|
|
#include <cwctype> // For towupper
|
2009-01-17 15:57:32 +01:00
|
|
|
|
|
|
|
#include "board_search.h"
|
2011-01-30 00:47:20 +01:00
|
|
|
#include "dic.h"
|
2011-08-27 19:21:26 +02:00
|
|
|
#include "game_params.h"
|
2009-01-17 15:57:32 +01:00
|
|
|
#include "board.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "tile.h"
|
|
|
|
#include "rack.h"
|
|
|
|
#include "round.h"
|
|
|
|
#include "results.h"
|
|
|
|
|
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
BoardSearch::BoardSearch(const Dictionary &iDic,
|
2011-08-27 19:21:26 +02:00
|
|
|
const GameParams &iParams,
|
2009-01-17 15:57:32 +01:00
|
|
|
const Matrix<Tile> &iTilesMx,
|
|
|
|
const Matrix<Cross> &iCrossMx,
|
|
|
|
const Matrix<int> &iPointsMx,
|
|
|
|
const Matrix<bool> &iJokerMx,
|
|
|
|
bool isFirstTurn)
|
2011-08-27 19:21:26 +02:00
|
|
|
: m_dic(iDic), m_params(iParams), m_tilesMx(iTilesMx), m_crossMx(iCrossMx),
|
2009-01-17 15:57:32 +01:00
|
|
|
m_pointsMx(iPointsMx), m_jokerMx(iJokerMx), m_firstTurn(isFirstTurn)
|
|
|
|
{
|
|
|
|
}
|
2007-08-04 22:01:27 +02:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
void BoardSearch::search(Rack &iRack, Results &oResults, Coord::Direction iDir) const
|
|
|
|
{
|
|
|
|
// Handle the first turn specifically
|
|
|
|
if (m_firstTurn)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
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(),
|
2012-06-18 21:29:38 +02:00
|
|
|
row, col, std::min(iRack.getNbTiles(), (unsigned)col) - 1);
|
2009-01-17 15:57:32 +01:00
|
|
|
return;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
vector<Tile> rackTiles;
|
|
|
|
iRack.getTiles(rackTiles);
|
|
|
|
vector<Tile>::const_iterator it;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
for (int row = 1; row <= BOARD_DIM; row++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-02-18 23:13:02 +01:00
|
|
|
Round partialWord;
|
2009-01-17 15:57:32 +01:00
|
|
|
partialWord.accessCoord().setDir(iDir);
|
|
|
|
partialWord.accessCoord().setRow(row);
|
|
|
|
int lastanchor = 0;
|
|
|
|
for (int col = 1; col <= BOARD_DIM; col++)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
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()))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
#ifdef DONT_USE_SEARCH_OPTIMIZATION
|
|
|
|
if (!m_tilesMx[row][col - 1].isEmpty())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
partialWord.accessCoord().setCol(lastanchor + 1);
|
|
|
|
extendRight(iRack, partialWord, oResults,
|
2012-06-18 21:21:40 +02:00
|
|
|
m_dic.getRoot(), row, lastanchor + 1, col);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2009-01-17 15:57:32 +01:00
|
|
|
else
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
partialWord.accessCoord().setCol(col);
|
|
|
|
leftPart(iRack, partialWord, oResults,
|
2012-06-18 21:21:40 +02:00
|
|
|
m_dic.getRoot(), row, col, col - lastanchor - 1);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2009-01-17 15:57:32 +01:00
|
|
|
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
|
|
|
|
{
|
|
|
|
partialWord.accessCoord().setCol(col);
|
|
|
|
leftPart(iRack, partialWord, oResults,
|
|
|
|
m_dic.getRoot(), row, col, col - lastanchor - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastanchor = col;
|
|
|
|
#endif
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
void BoardSearch::leftPart(Rack &iRack, Round &ioPartialWord,
|
|
|
|
Results &oResults, int n, int iRow,
|
|
|
|
int iAnchor, int iLimit) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
extendRight(iRack, ioPartialWord, oResults, n, iRow, iAnchor, iAnchor);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2005-11-05 14:56:59 +01:00
|
|
|
if (iLimit > 0)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2013-01-17 17:58:56 +01:00
|
|
|
bool hasJokerInRack = iRack.contains(Tile::Joker());
|
2009-01-17 15:57:32 +01:00
|
|
|
for (unsigned int succ = m_dic.getSucc(n); succ; succ = m_dic.getNext(succ))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2012-02-19 12:49:58 +01:00
|
|
|
const Tile &l = Tile(m_dic.getChar(succ));
|
2013-01-17 17:58:56 +01:00
|
|
|
if (iRack.contains(l))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
iRack.remove(l);
|
2008-01-08 14:52:32 +01:00
|
|
|
ioPartialWord.addRightFromRack(l, false);
|
2005-11-05 14:56:59 +01:00
|
|
|
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() - 1);
|
2009-01-17 15:57:32 +01:00
|
|
|
leftPart(iRack, ioPartialWord, oResults,
|
2005-11-05 14:56:59 +01:00
|
|
|
succ, iRow, iAnchor, iLimit - 1);
|
|
|
|
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
|
2012-02-18 23:13:02 +01:00
|
|
|
ioPartialWord.removeRight();
|
2005-02-05 12:14:56 +01:00
|
|
|
iRack.add(l);
|
|
|
|
}
|
2008-01-08 14:52:32 +01:00
|
|
|
if (hasJokerInRack)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
iRack.remove(Tile::Joker());
|
2008-01-08 14:52:32 +01:00
|
|
|
ioPartialWord.addRightFromRack(l, true);
|
2005-11-05 14:56:59 +01:00
|
|
|
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() - 1);
|
2009-01-17 15:57:32 +01:00
|
|
|
leftPart(iRack, ioPartialWord, oResults,
|
2005-11-05 14:56:59 +01:00
|
|
|
succ, iRow, iAnchor, iLimit - 1);
|
|
|
|
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
|
2012-02-18 23:13:02 +01:00
|
|
|
ioPartialWord.removeRight();
|
2005-02-05 12:14:56 +01:00
|
|
|
iRack.add(Tile::Joker());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
void BoardSearch::extendRight(Rack &iRack, Round &ioPartialWord,
|
|
|
|
Results &oResults, unsigned int iNode,
|
|
|
|
int iRow, int iCol, int iAnchor) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
if (m_tilesMx[iRow][iCol].isEmpty())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
if (m_dic.isEndOfWord(iNode) && iCol > iAnchor)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
evalMove(oResults, ioPartialWord);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization: avoid entering the for loop if no tile can match
|
|
|
|
if (m_crossMx[iRow][iCol].isNone())
|
|
|
|
return;
|
|
|
|
|
2013-01-17 17:58:56 +01:00
|
|
|
bool hasJokerInRack = iRack.contains(Tile::Joker());
|
2009-01-17 15:57:32 +01:00
|
|
|
for (unsigned int succ = m_dic.getSucc(iNode); succ; succ = m_dic.getNext(succ))
|
|
|
|
{
|
2012-02-19 12:49:58 +01:00
|
|
|
const Tile &l = Tile(m_dic.getChar(succ));
|
2009-01-17 15:57:32 +01:00
|
|
|
if (m_crossMx[iRow][iCol].check(l))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2013-01-17 17:58:56 +01:00
|
|
|
if (iRack.contains(l))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
iRack.remove(l);
|
|
|
|
ioPartialWord.addRightFromRack(l, false);
|
|
|
|
extendRight(iRack, ioPartialWord, oResults,
|
|
|
|
succ, iRow, iCol + 1, iAnchor);
|
2012-02-18 23:13:02 +01:00
|
|
|
ioPartialWord.removeRight();
|
2009-01-17 15:57:32 +01:00
|
|
|
iRack.add(l);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2009-01-17 15:57:32 +01:00
|
|
|
if (hasJokerInRack)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2009-01-17 15:57:32 +01:00
|
|
|
iRack.remove(Tile::Joker());
|
|
|
|
ioPartialWord.addRightFromRack(l, true);
|
|
|
|
extendRight(iRack, ioPartialWord, oResults,
|
|
|
|
succ, iRow, iCol + 1, iAnchor);
|
2012-02-18 23:13:02 +01:00
|
|
|
ioPartialWord.removeRight();
|
2009-01-17 15:57:32 +01:00
|
|
|
iRack.add(Tile::Joker());
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2009-01-17 15:57:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-19 12:49:58 +01:00
|
|
|
const Tile &l = m_tilesMx[iRow][iCol];
|
2009-01-17 15:57:32 +01:00
|
|
|
wint_t upperChar = towupper(l.toChar());
|
|
|
|
for (unsigned int succ = m_dic.getSucc(iNode); succ ; succ = m_dic.getNext(succ))
|
|
|
|
{
|
|
|
|
if ((wint_t)m_dic.getChar(succ) == upperChar)
|
|
|
|
{
|
|
|
|
ioPartialWord.addRightFromBoard(l);
|
|
|
|
extendRight(iRack, ioPartialWord,
|
|
|
|
oResults, succ, iRow, iCol + 1, iAnchor);
|
2012-02-18 23:13:02 +01:00
|
|
|
ioPartialWord.removeRight();
|
2009-01-17 15:57:32 +01:00
|
|
|
// The letter will be present only once in the dictionary,
|
|
|
|
// so we can stop looping
|
|
|
|
break;
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
/*
|
|
|
|
* 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
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2011-08-27 19:21:26 +02:00
|
|
|
int fromrack = 0;
|
2009-01-17 15:57:32 +01:00
|
|
|
int pts = 0;
|
|
|
|
int ptscross = 0;
|
|
|
|
int wordmul = 1;
|
2005-02-17 21:01:59 +01:00
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
unsigned int len = iWord.getWordLen();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
int row = iWord.getCoord().getRow();
|
|
|
|
int col = iWord.getCoord().getCol();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2012-12-26 14:14:44 +01:00
|
|
|
const BoardLayout & boardLayout = m_params.getBoardLayout();
|
2009-01-17 15:57:32 +01:00
|
|
|
for (unsigned int i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (!m_tilesMx[row][col+i].isEmpty())
|
|
|
|
{
|
|
|
|
if (!m_jokerMx[row][col+i])
|
|
|
|
pts += iWord.getTile(i).getPoints();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
if (!iWord.isJoker(i))
|
|
|
|
l = iWord.getTile(i).getPoints() *
|
2012-12-26 14:14:44 +01:00
|
|
|
boardLayout.getLetterMultiplier(row, col + i);
|
2009-01-17 15:57:32 +01:00
|
|
|
else
|
|
|
|
l = 0;
|
|
|
|
pts += l;
|
2012-12-26 14:14:44 +01:00
|
|
|
int wm = boardLayout.getWordMultiplier(row, col + i);
|
|
|
|
wordmul *= wm;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
int t = m_pointsMx[row][col+i];
|
|
|
|
if (t >= 0)
|
2012-12-26 14:14:44 +01:00
|
|
|
ptscross += (t + l) * wm;
|
2009-01-17 15:57:32 +01:00
|
|
|
fromrack++;
|
|
|
|
}
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2011-08-27 19:21:26 +02:00
|
|
|
// Ignore words using too many letters from the rack
|
|
|
|
if (fromrack > m_params.getLettersToPlay())
|
|
|
|
return;
|
|
|
|
|
|
|
|
pts = ptscross + pts * wordmul;
|
2011-08-27 20:29:44 +02:00
|
|
|
if (fromrack == m_params.getLettersToPlay())
|
2011-08-27 19:21:26 +02:00
|
|
|
{
|
|
|
|
pts += m_params.getBonusPoints();
|
|
|
|
iWord.setBonus(true);
|
|
|
|
}
|
2009-01-17 15:57:32 +01:00
|
|
|
iWord.setPoints(pts);
|
2005-02-17 21:01:59 +01:00
|
|
|
|
2009-01-17 15:57:32 +01:00
|
|
|
if (iWord.getCoord().getDir() == Coord::VERTICAL)
|
|
|
|
{
|
|
|
|
// Exchange the coordinates temporarily
|
|
|
|
iWord.accessCoord().swap();
|
|
|
|
}
|
|
|
|
oResults.add(iWord);
|
|
|
|
if (iWord.getCoord().getDir() == Coord::VERTICAL)
|
|
|
|
{
|
|
|
|
// Restore the coordinates
|
|
|
|
iWord.accessCoord().swap();
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2006-01-01 20:37:57 +01:00
|
|
|
|