Original author: Pierre Sarrazin. Preserve upstream GPLv3-or-later and file-specific notices. Source: http://sarrazip.com/dev/coco-letters-0.1.0.tar.gz Archive SHA-256: ad7c7771845e1a35014644fb562634a558abee632e1d7738683db6e9e9e46b94
239 lines
7.6 KiB
C
239 lines
7.6 KiB
C
/* WordSearch.c
|
|
Copyright (C) 2026 Pierre Sarrazin <http://sarrazip.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 3 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, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "WordSearch.h"
|
|
|
|
#include "GameState.h"
|
|
#include "PositionIterator.h"
|
|
|
|
|
|
void
|
|
WordSearch_init(WordSearch *ws,
|
|
byte *rack,
|
|
BOOL allInRack,
|
|
byte difficultyLevel,
|
|
const GameState *gs)
|
|
{
|
|
ws->rack = rack;
|
|
ws->allInRack = allInRack;
|
|
ws->difficultyLevel = difficultyLevel;
|
|
ws->numWordsProcessed = 0;
|
|
ws->foundWord[0] = '\0';
|
|
ws->foundWordRow = ws->foundWordCol = 0;
|
|
ws->foundWordHorizontal = FALSE;
|
|
ws->gameState = gs;
|
|
}
|
|
|
|
|
|
void
|
|
WordSearch_initWordWalk(const WordSearch *ws,
|
|
byte *row,
|
|
byte *col,
|
|
byte *rowInc,
|
|
byte *colInc,
|
|
const char **wordToPlace,
|
|
byte *wordLen)
|
|
{
|
|
assert(ws->foundWordRow < BOARD_SIZE);
|
|
*row = ws->foundWordRow;
|
|
*col = ws->foundWordCol;
|
|
if (ws->foundWordHorizontal)
|
|
{
|
|
*rowInc = 0;
|
|
*colInc = 1;
|
|
}
|
|
else
|
|
{
|
|
*rowInc = 1;
|
|
*colInc = 0;
|
|
}
|
|
*wordToPlace = ws->foundWord;
|
|
*wordLen = (byte) strlen(*wordToPlace);
|
|
}
|
|
|
|
|
|
// wordFactor: In/out. May get multiplied by 2 or 3.
|
|
//
|
|
static void
|
|
processSquareType(byte row, byte col,
|
|
byte *squareType, byte *wordFactor, byte *letterFactor)
|
|
{
|
|
*squareType = GameState_boardSquareTypes[row][col];
|
|
*letterFactor = 1;
|
|
switch (*squareType)
|
|
{
|
|
case SQ_EMPTY: break;
|
|
case SQ_START: *wordFactor *= 2; break;
|
|
case SQ_TRIPLE_WORD: *wordFactor *= 3; break;
|
|
case SQ_DOUBLE_WORD: *wordFactor *= 2; break;
|
|
case SQ_TRIPLE_LETTER: *letterFactor = 3; break;
|
|
case SQ_DOUBLE_LETTER: *letterFactor = 2; break;
|
|
default: assert(FALSE);
|
|
}
|
|
}
|
|
|
|
|
|
// wordFactor: In/out.
|
|
// tileToPlace: 1..26, or TILE_TYPE_BLANK.
|
|
// Returns a number of points scored.
|
|
//
|
|
static word
|
|
countPointsOfTileToPlace(const GameState *gs,
|
|
byte tileToPlace,
|
|
byte row,
|
|
byte col,
|
|
byte *wordFactor)
|
|
{
|
|
byte tileOnBoard = GameState_getTile(gs, row, col);
|
|
//printf("\t\t""countPointsOfTileToPlace: row=%u, col=%u: tileOnBoard=%u\n", row, col, tileOnBoard);
|
|
|
|
if (tileOnBoard != TILE_TYPE_NONE)
|
|
return 0; // square already occupied, so no points
|
|
|
|
byte squareType;
|
|
byte letterFactor;
|
|
processSquareType(row, col, &squareType, wordFactor, &letterFactor);
|
|
|
|
//printf("\t\t""countPointsOfTileToPlace: squareType=%u, wordFactor=%u, letterFactor=%u\n",
|
|
// squareType, *wordFactor, letterFactor);
|
|
|
|
// Count the tile's points.
|
|
byte tilePoints = GameState_getTileValue(gs, tileToPlace);
|
|
|
|
//printf("\t\t""countPointsOfTileToPlace: tileToPlace=%u, tilePoints=%u.\n",
|
|
// tileToPlace, tilePoints);
|
|
|
|
return (word) tilePoints * letterFactor;
|
|
}
|
|
|
|
|
|
typedef struct CrossingWord
|
|
{
|
|
BOOL across;
|
|
byte row;
|
|
byte col;
|
|
byte indexOfTileToPlace;
|
|
byte tileToPlace;
|
|
byte length;
|
|
byte blankIndex; // index in the crossing word
|
|
} CrossingWord;
|
|
|
|
|
|
static word
|
|
countPointsOfCrossingWords(const GameState *gs,
|
|
const CrossingWord crossingWords[],
|
|
byte numCrossingWords)
|
|
{
|
|
word wordValue = 0;
|
|
byte wordFactor = 1;
|
|
|
|
for (byte w = 0; w < numCrossingWords; ++w)
|
|
{
|
|
const CrossingWord *cw = &crossingWords[w];
|
|
|
|
byte row = cw->row, col = cw->col;
|
|
//printf("\t""countPointsOfCrossingWords: w=%u, (%u, %u)\n", w, row, col);
|
|
PositionIterator posIter;
|
|
PositionIterator_init(&posIter, cw->across, &row, &col);
|
|
|
|
for (byte i = 0; i < cw->length; ++i)
|
|
{
|
|
byte tileToPlace;
|
|
if (i == cw->blankIndex)
|
|
tileToPlace = TILE_TYPE_BLANK;
|
|
else if (i == cw->indexOfTileToPlace)
|
|
tileToPlace = cw->tileToPlace;
|
|
else
|
|
tileToPlace = TILE_TYPE_NONE; // tile already on board is worth 0
|
|
|
|
//printf("\t\t""i=%u, tileToPlace=%u\n", i, tileToPlace);
|
|
|
|
// This may multiply *wordFactor by 2 or 3.
|
|
//
|
|
word tilePoints = countPointsOfTileToPlace(
|
|
gs, tileToPlace, row, col, &wordFactor);
|
|
|
|
//printf("\t\t"" tilePoints=%u\n", tilePoints);
|
|
wordValue += tilePoints;
|
|
PositionIterator_increment(&posIter);
|
|
}
|
|
}
|
|
|
|
//printf("\t""countPointsOfCrossingWords: wordValue=%u, wordFactor=%u\n", wordValue, wordFactor);
|
|
return wordValue * wordFactor;
|
|
}
|
|
|
|
|
|
word
|
|
WordSearch_countPointsFromWordsPlayed(const WordSearch *ws, const GameState *gs)
|
|
{
|
|
byte row, col, rowInc, colInc, wordLen;
|
|
const char *wordToPlace;
|
|
WordSearch_initWordWalk(ws, &row, &col, &rowInc, &colInc, &wordToPlace, &wordLen);
|
|
|
|
//printf("countPoints: row=%u, col=%u, rowInc=%u, colInc=%u, wordLen=%u, wordToPlace='%s', wordLen=%u.\n",
|
|
// row, col, rowInc, colInc, wordLen, wordToPlace, wordLen);
|
|
|
|
word wordValue = 0;
|
|
byte wordFactor = 1;
|
|
CrossingWord foundCrossingWords[MAX_WORD_LEN];
|
|
byte numFoundCrossingWords = 0;
|
|
|
|
for (byte i = 0; i < wordLen; ++i, row += rowInc, col += colInc)
|
|
{
|
|
byte letterTileTypeBehindBlank = ws->letterTileTypeBehindBlank[i];
|
|
byte tileToPlace = (letterTileTypeBehindBlank == TILE_TYPE_NONE ? wordToPlace[i] - '@' : TILE_TYPE_BLANK);
|
|
wordValue += countPointsOfTileToPlace(gs, tileToPlace, row, col, &wordFactor);
|
|
|
|
// Detect if a word crosses the current letter at (row, col).
|
|
// Pass colInc and rowInc in reverse order, so that this call
|
|
// advances perpendicularly.
|
|
//
|
|
CrossingWord *cw = &foundCrossingWords[numFoundCrossingWords];
|
|
if (numFoundCrossingWords < MAX_WORD_LEN
|
|
&& GameState_detectCrossingWord(gs, row, col, colInc, rowInc,
|
|
&cw->row, &cw->col,
|
|
&cw->indexOfTileToPlace,
|
|
&cw->length))
|
|
{
|
|
cw->tileToPlace = tileToPlace;
|
|
|
|
cw->across = !ws->foundWordHorizontal;
|
|
|
|
// If letter at i is a blank, remember the position
|
|
// of the blank in the crossing word.
|
|
//
|
|
cw->blankIndex = (letterTileTypeBehindBlank == TILE_TYPE_NONE ? 0xFF : i);
|
|
//printf(" cw->blankIndex=%u\n", cw->blankIndex);
|
|
|
|
++numFoundCrossingWords;
|
|
}
|
|
}
|
|
|
|
wordValue *= wordFactor;
|
|
|
|
//printf(" %u*%u=%u\n", wordValue, wordFactor, wordValue * wordFactor);
|
|
//printf(" numFoundCrossingWords=%u\n", numFoundCrossingWords);
|
|
|
|
word crossingWordsValue = countPointsOfCrossingWords(
|
|
gs, foundCrossingWords, numFoundCrossingWords);
|
|
|
|
//printf(" crossingWordsValue=%u\n", crossingWordsValue);
|
|
|
|
return wordValue + crossingWordsValue;
|
|
}
|