2005-02-05 12:14:56 +01:00
|
|
|
/*****************************************************************************
|
2008-01-08 14:52:32 +01:00
|
|
|
* Eliot
|
|
|
|
* Copyright (C) 1999-2007 Antoine Fraboulet & Olivier Teulière
|
|
|
|
* 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
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include <dic.h>
|
|
|
|
#include "tile.h"
|
|
|
|
#include "rack.h"
|
|
|
|
#include "round.h"
|
|
|
|
#include "results.h"
|
|
|
|
#include "board.h"
|
2006-11-05 14:30:06 +01:00
|
|
|
#include "encoding.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
#include "debug.h"
|
2007-08-04 22:01:27 +02:00
|
|
|
#include "game.h"
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* computes the score of a word, coordinates may be changed to reflect
|
|
|
|
* the real direction of the word
|
|
|
|
*/
|
|
|
|
static void BoardSearchEvalMove(const Board &iBoard,
|
2008-11-22 14:09:28 +01:00
|
|
|
const Matrix<Tile> &iTilesMx,
|
|
|
|
const Matrix<int> &iPointsMx,
|
|
|
|
const Matrix<bool> &iJokerMx,
|
2005-02-05 12:14:56 +01:00
|
|
|
Results &iResults, Round &iWord)
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
unsigned int fromrack = 0;
|
|
|
|
int pts = 0;
|
|
|
|
int ptscross = 0;
|
|
|
|
int wordmul = 1;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
unsigned int len = iWord.getWordLen();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
int row = iWord.getCoord().getRow();
|
|
|
|
int col = iWord.getCoord().getCol();
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
for (unsigned int i = 0; i < len; i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-02-17 21:01:59 +01:00
|
|
|
if (!iTilesMx[row][col+i].isEmpty())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-03-27 23:45:04 +02:00
|
|
|
if (!iJokerMx[row][col+i])
|
2005-02-05 12:14:56 +01:00
|
|
|
pts += iWord.getTile(i).getPoints();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
int l;
|
2005-03-27 23:45:04 +02:00
|
|
|
if (!iWord.isJoker(i))
|
2005-02-05 12:14:56 +01:00
|
|
|
l = iWord.getTile(i).getPoints() *
|
2008-01-09 11:48:19 +01:00
|
|
|
iBoard.GetLetterMultiplier(row, col + i);
|
2005-02-05 12:14:56 +01:00
|
|
|
else
|
|
|
|
l = 0;
|
|
|
|
pts += l;
|
2008-01-09 11:48:19 +01:00
|
|
|
wordmul *= iBoard.GetWordMultiplier(row, col + i);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
int t = iPointsMx[row][col+i];
|
2005-02-05 12:14:56 +01:00
|
|
|
if (t >= 0)
|
2008-01-09 11:48:19 +01:00
|
|
|
ptscross += (t + l) * iBoard.GetWordMultiplier(row, col + i);
|
2005-02-05 12:14:56 +01:00
|
|
|
fromrack++;
|
|
|
|
}
|
|
|
|
}
|
2007-08-04 22:01:27 +02:00
|
|
|
|
|
|
|
pts = ptscross + pts * wordmul + Game::BONUS_POINTS * (fromrack == Game::RACK_SIZE);
|
|
|
|
iWord.setBonus(fromrack == Game::RACK_SIZE);
|
2005-02-05 12:14:56 +01:00
|
|
|
iWord.setPoints(pts);
|
|
|
|
|
2005-11-05 14:56:59 +01:00
|
|
|
if (iWord.getCoord().getDir() == Coord::VERTICAL)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-11-05 14:56:59 +01:00
|
|
|
// Exchange the coordinates temporarily
|
|
|
|
iWord.accessCoord().swap();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
iResults.add(iWord);
|
2005-11-05 14:56:59 +01:00
|
|
|
if (iWord.getCoord().getDir() == Coord::VERTICAL)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-11-05 14:56:59 +01:00
|
|
|
// Restore the coordinates
|
|
|
|
iWord.accessCoord().swap();
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ExtendRight(const Board &iBoard,
|
|
|
|
const Dictionary &iDic,
|
2008-11-22 14:09:28 +01:00
|
|
|
const Matrix<Tile> &iTilesMx,
|
|
|
|
const Matrix<Cross> &iCrossMx,
|
|
|
|
const Matrix<int> &iPointsMx,
|
|
|
|
const Matrix<bool> &iJokerMx,
|
2005-11-05 14:56:59 +01:00
|
|
|
Rack &iRack, Round &ioPartialWord,
|
2005-02-05 12:14:56 +01:00
|
|
|
Results &iResults, unsigned int iNode,
|
2005-11-05 14:56:59 +01:00
|
|
|
int iRow, int iCol, int iAnchor)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
Tile l;
|
|
|
|
unsigned int succ;
|
|
|
|
|
2005-02-17 21:01:59 +01:00
|
|
|
if (iTilesMx[iRow][iCol].isEmpty())
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
if (iDic.isEndOfWord(iNode) && iCol > iAnchor)
|
|
|
|
{
|
2005-02-05 12:14:56 +01:00
|
|
|
BoardSearchEvalMove(iBoard, iTilesMx, iPointsMx, iJokerMx,
|
2005-11-05 14:56:59 +01:00
|
|
|
iResults, ioPartialWord);
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization: avoid entering the for loop if no tile can match
|
|
|
|
if (iCrossMx[iRow][iCol].isNone())
|
|
|
|
return;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
bool hasJokerInRack = iRack.in(Tile::Joker());
|
|
|
|
for (succ = iDic.getSucc(iNode); succ; succ = iDic.getNext(succ))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
l = Tile(iDic.getChar(succ));
|
2005-02-05 12:14:56 +01:00
|
|
|
if (iCrossMx[iRow][iCol].check(l))
|
|
|
|
{
|
|
|
|
if (iRack.in(l))
|
|
|
|
{
|
|
|
|
iRack.remove(l);
|
2008-01-08 14:52:32 +01:00
|
|
|
ioPartialWord.addRightFromRack(l, false);
|
2005-02-05 12:14:56 +01:00
|
|
|
ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
|
2005-11-05 14:56:59 +01:00
|
|
|
iJokerMx, iRack, ioPartialWord, iResults,
|
|
|
|
succ, iRow, iCol + 1, iAnchor);
|
2008-01-08 14:52:32 +01:00
|
|
|
ioPartialWord.removeRightToRack(l, false);
|
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-02-05 12:14:56 +01:00
|
|
|
ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
|
2005-11-05 14:56:59 +01:00
|
|
|
iJokerMx, iRack, ioPartialWord, iResults,
|
|
|
|
succ, iRow, iCol + 1, iAnchor);
|
2008-01-08 14:52:32 +01:00
|
|
|
ioPartialWord.removeRightToRack(l, true);
|
2005-02-05 12:14:56 +01:00
|
|
|
iRack.add(Tile::Joker());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
l = iTilesMx[iRow][iCol];
|
2008-01-08 14:52:32 +01:00
|
|
|
wint_t upperChar = towupper(l.toChar());
|
|
|
|
for (succ = iDic.getSucc(iNode); succ ; succ = iDic.getNext(succ))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
if ((wint_t)iDic.getChar(succ) == upperChar)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-11-05 14:56:59 +01:00
|
|
|
ioPartialWord.addRightFromBoard(l);
|
2005-02-05 12:14:56 +01:00
|
|
|
ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
|
2005-11-05 14:56:59 +01:00
|
|
|
iJokerMx, iRack, ioPartialWord,
|
|
|
|
iResults, succ, iRow, iCol + 1, iAnchor);
|
|
|
|
ioPartialWord.removeRightToBoard(l);
|
2008-01-08 14:52: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void LeftPart(const Board &iBoard,
|
|
|
|
const Dictionary &iDic,
|
2008-11-22 14:09:28 +01:00
|
|
|
const Matrix<Tile> &iTilesMx,
|
|
|
|
const Matrix<Cross> &iCrossMx,
|
|
|
|
const Matrix<int> &iPointsMx,
|
|
|
|
const Matrix<bool> &iJokerMx,
|
2005-11-05 14:56:59 +01:00
|
|
|
Rack &iRack, Round &ioPartialWord,
|
|
|
|
Results &iResults, int n, int iRow,
|
|
|
|
int iAnchor, int iLimit)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
Tile l;
|
|
|
|
int succ;
|
|
|
|
|
|
|
|
ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx, iJokerMx, iRack,
|
2005-11-05 14:56:59 +01:00
|
|
|
ioPartialWord, iResults, 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
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
bool hasJokerInRack = iRack.in(Tile::Joker());
|
|
|
|
for (succ = iDic.getSucc(n); succ; succ = iDic.getNext(succ))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2008-01-08 14:52:32 +01:00
|
|
|
l = Tile(iDic.getChar(succ));
|
2005-02-05 12:14:56 +01:00
|
|
|
if (iRack.in(l))
|
|
|
|
{
|
|
|
|
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);
|
2005-02-05 12:14:56 +01:00
|
|
|
LeftPart(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
|
2005-11-05 14:56:59 +01:00
|
|
|
iJokerMx, iRack, ioPartialWord, iResults,
|
|
|
|
succ, iRow, iAnchor, iLimit - 1);
|
|
|
|
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
|
2008-01-08 14:52:32 +01:00
|
|
|
ioPartialWord.removeRightToRack(l, false);
|
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);
|
2005-02-05 12:14:56 +01:00
|
|
|
LeftPart(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
|
2005-11-05 14:56:59 +01:00
|
|
|
iJokerMx, iRack, ioPartialWord, iResults,
|
|
|
|
succ, iRow, iAnchor, iLimit - 1);
|
|
|
|
ioPartialWord.accessCoord().setCol(ioPartialWord.getCoord().getCol() + 1);
|
2008-01-08 14:52:32 +01:00
|
|
|
ioPartialWord.removeRightToRack(l, true);
|
2005-02-05 12:14:56 +01:00
|
|
|
iRack.add(Tile::Joker());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void BoardSearchAux(const Board &iBoard,
|
|
|
|
const Dictionary &iDic,
|
2008-11-22 14:09:28 +01:00
|
|
|
const Matrix<Tile> &iTilesMx,
|
|
|
|
const Matrix<Cross> &iCrossMx,
|
|
|
|
const Matrix<int> &iPointsMx,
|
|
|
|
const Matrix<bool> &iJokerMx,
|
2005-11-05 12:01:58 +01:00
|
|
|
Rack &iRack, Results &iResults,
|
|
|
|
Coord::Direction iDir)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
int row, col, lastanchor;
|
|
|
|
Round partialword;
|
2006-01-22 13:23:52 +01:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
vector<Tile> rackTiles;
|
2006-01-22 13:23:52 +01:00
|
|
|
iRack.getTiles(rackTiles);
|
2008-01-08 14:52:32 +01:00
|
|
|
vector<Tile>::const_iterator it;
|
|
|
|
vector<Tile>::const_iterator itEnd;
|
2006-01-22 13:23:52 +01:00
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
for (row = 1; row <= BOARD_DIM; row++)
|
|
|
|
{
|
|
|
|
partialword.init();
|
2005-11-05 14:56:59 +01:00
|
|
|
partialword.accessCoord().setDir(iDir);
|
|
|
|
partialword.accessCoord().setRow(row);
|
2005-02-05 12:14:56 +01:00
|
|
|
lastanchor = 0;
|
|
|
|
for (col = 1; col <= BOARD_DIM; col++)
|
|
|
|
{
|
2005-02-17 21:01:59 +01:00
|
|
|
if (iTilesMx[row][col].isEmpty() &&
|
|
|
|
(!iTilesMx[row][col - 1].isEmpty() ||
|
|
|
|
!iTilesMx[row][col + 1].isEmpty() ||
|
2008-01-08 14:52:32 +01:00
|
|
|
!iTilesMx[row - 1][col].isEmpty() ||
|
2005-02-17 21:01:59 +01:00
|
|
|
!iTilesMx[row + 1][col].isEmpty()))
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2006-11-05 14:30:06 +01:00
|
|
|
#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
|
2006-01-22 13:23:52 +01:00
|
|
|
// 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
|
2008-01-08 14:52:32 +01:00
|
|
|
bool match = false;
|
|
|
|
for (it = rackTiles.begin(); it != rackTiles.end(); it++)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2006-01-22 13:23:52 +01:00
|
|
|
if (iCrossMx[row][col].check(*it))
|
|
|
|
{
|
|
|
|
match = true;
|
2008-01-08 14:52:32 +01:00
|
|
|
break;
|
2006-01-22 13:23:52 +01:00
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2006-01-22 13:23:52 +01:00
|
|
|
if (match)
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2006-01-22 13:23:52 +01:00
|
|
|
if (!iTilesMx[row][col - 1].isEmpty())
|
|
|
|
{
|
|
|
|
partialword.accessCoord().setCol(lastanchor + 1);
|
|
|
|
ExtendRight(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
|
|
|
|
iJokerMx, iRack, partialword, iResults,
|
2008-01-08 14:52:32 +01:00
|
|
|
iDic.getRoot(), row, lastanchor + 1, col);
|
2006-01-22 13:23:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
partialword.accessCoord().setCol(col);
|
|
|
|
LeftPart(iBoard, iDic, iTilesMx, iCrossMx, iPointsMx,
|
|
|
|
iJokerMx, iRack, partialword, iResults,
|
2008-01-08 14:52:32 +01:00
|
|
|
iDic.getRoot(), row, col, col -
|
2006-01-22 13:23:52 +01:00
|
|
|
lastanchor - 1);
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
lastanchor = col;
|
2006-11-05 14:30:06 +01:00
|
|
|
#endif
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Board::search(const Dictionary &iDic,
|
2005-02-17 21:01:59 +01:00
|
|
|
const Rack &iRack,
|
2008-11-22 14:09:28 +01:00
|
|
|
Results &oResults) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2005-02-17 21:01:59 +01:00
|
|
|
// Create a copy of the rack to avoid modifying the given one
|
|
|
|
Rack copyRack = iRack;
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
BoardSearchAux(*this, iDic, m_tilesRow, m_crossRow,
|
|
|
|
m_pointRow, m_jokerRow,
|
2005-11-05 12:01:58 +01:00
|
|
|
copyRack, oResults, Coord::HORIZONTAL);
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
BoardSearchAux(*this, iDic, m_tilesCol, m_crossCol,
|
|
|
|
m_pointCol, m_jokerCol,
|
2005-11-05 12:01:58 +01:00
|
|
|
copyRack, oResults, Coord::VERTICAL);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Board::searchFirst(const Dictionary &iDic,
|
2005-02-17 21:01:59 +01:00
|
|
|
const Rack &iRack,
|
2008-11-22 14:09:28 +01:00
|
|
|
Results &oResults) const
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
|
|
|
int row = 8, col = 8;
|
|
|
|
|
2005-02-17 21:01:59 +01:00
|
|
|
// Create a copy of the rack to avoid modifying the given one
|
|
|
|
Rack copyRack = iRack;
|
|
|
|
|
2005-11-05 14:56:59 +01:00
|
|
|
Round partialword;
|
|
|
|
partialword.accessCoord().setRow(row);
|
|
|
|
partialword.accessCoord().setCol(col);
|
|
|
|
partialword.accessCoord().setDir(Coord::HORIZONTAL);
|
2005-02-05 12:14:56 +01:00
|
|
|
LeftPart(*this, iDic, m_tilesRow, m_crossRow,
|
|
|
|
m_pointRow, m_jokerRow,
|
2008-01-08 14:52:32 +01:00
|
|
|
copyRack, partialword, oResults, iDic.getRoot(), row, col,
|
|
|
|
copyRack.getNbTiles() - 1);
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2006-01-01 20:37:57 +01:00
|
|
|
|