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
171 lines
5.5 KiB
C
171 lines
5.5 KiB
C
/* GameState.h
|
|
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/>.
|
|
*/
|
|
|
|
#ifndef _H_GameState
|
|
#define _H_GameState
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
typedef struct PlayedBlankTile
|
|
{
|
|
byte row, col; // position where blank tile played on board
|
|
byte letterTileType; // 1..26 = A..Z
|
|
} PlayedBlankTile;
|
|
|
|
|
|
typedef struct GameState
|
|
{
|
|
// Bag of tiles from which the players pick at random.
|
|
// Indexed with TILE_TYPE_BLANK and 1..26.
|
|
//
|
|
byte numTiles[NUM_TILE_TYPES];
|
|
|
|
byte boardTiles[BOARD_SIZE][BOARD_SIZE]; // TILE_TYPE_BLANK, 1..26 = A..Z, TILE_TYPE_NONE
|
|
BOOL isBoardEmpty;
|
|
byte numPlayedBlankTiles;
|
|
PlayedBlankTile playedBlankTiles[NUM_BLANK_TILES]; // only indices < numPlayedBlankTiles are used
|
|
} GameState;
|
|
|
|
|
|
// Matrix of SQ_* enum values.
|
|
//
|
|
extern const byte GameState_boardSquareTypes[BOARD_SIZE][BOARD_SIZE];
|
|
|
|
|
|
void GameState_init(GameState *gs);
|
|
|
|
|
|
byte GameState_getNumTilesOfType(const GameState *gs, byte tileType);
|
|
|
|
|
|
// Returns the number of points associated with the given tile type,
|
|
// without regard to where on the board it gets played.
|
|
//
|
|
byte GameState_getTileValue(const GameState *gs, byte tileType);
|
|
|
|
|
|
// row, col: 0..BOARD_SIZE-1.
|
|
// No bounds checking.
|
|
// A cast is used to prevent using this function to change the board.
|
|
//
|
|
#define GameState_getTile(gs, row, col) ((byte) (gs)->boardTiles[row][col])
|
|
|
|
|
|
// Like GameState_getTile() but checks if the coordinates are valid.
|
|
// If they are not, returns TILE_TYPE_NONE.
|
|
//
|
|
byte GameState_getTileWithBoundsChecking(const GameState *self,
|
|
byte row, byte col);
|
|
|
|
|
|
// Returns the letter tile type (1..26) represented by the blank at
|
|
// the specified position, if any. Otherwise, returns TILE_TYPE_NONE.
|
|
//
|
|
byte GameState_getBlankTileLetter(const GameState *gs, byte row, byte col);
|
|
|
|
|
|
// Sets square (row, col) to tileType.
|
|
// If tileType is TILE_TYPE_BLANK, remembers that the blanks
|
|
// represents letterTileTypeIfBlank, which then must be 1..26.
|
|
// Must not be used for from than NUM_BLANK_TILES.
|
|
// This is ensured by how the bag is initialized using
|
|
// NUM_BLANK_TILES by GameState_init().
|
|
//
|
|
void GameState_setTile(GameState *gs, byte row, byte col,
|
|
byte tileType, byte letterTileTypeIfBlank);
|
|
|
|
|
|
#define GameState_isBoardEmpty(gs) ((gs)->isBoardEmpty)
|
|
|
|
|
|
byte GameState_countTilesInBag(const GameState *gs);
|
|
|
|
|
|
// Pick a tile at random from the bag.
|
|
// Returns TILE_TYPE_BLANK or 1..26 upon success, TILE_TYPE_NONE upon failure.
|
|
//
|
|
byte GameState_pickRandomLetter(GameState *gs);
|
|
|
|
|
|
void GameState_fillRackEmptySlots(GameState *gs, byte rack[RACK_SIZE]);
|
|
|
|
|
|
BOOL GameState_tryHangWord(const GameState *gs,
|
|
const char *candidateWord, byte candidateWordLength,
|
|
char boardLetterToHangTo,
|
|
byte freeSquaresNeededBefore,
|
|
byte freeSquaresNeededAfter,
|
|
BOOL *outHorizontally, byte *outRow, byte *outCol);
|
|
|
|
|
|
BOOL GameState_checkAdjacentWords(const GameState *gs,
|
|
BOOL horizontally,
|
|
byte row,
|
|
byte col,
|
|
byte hangLetterOffset,
|
|
const char *candidateWord,
|
|
byte candidateWordLength);
|
|
|
|
|
|
// tileType: TILE_TYPE_BLANK, 1..26 or TILE_TYPE_NONE.
|
|
//
|
|
char GameState_getTileTypeChar(byte tileType);
|
|
|
|
|
|
BOOL GameState_detectCrossingWord(const GameState *gs,
|
|
byte initRow,
|
|
byte initCol,
|
|
byte rowInc,
|
|
byte colInc,
|
|
byte *foundWordRow,
|
|
byte *foundWordCol,
|
|
byte *indexOfTileToPlace,
|
|
byte *foundWordLen);
|
|
|
|
byte GameState_detectHorizontalPrefix(const GameState *gs, byte row, byte col);
|
|
|
|
|
|
byte GameState_detectHorizontalSuffix(const GameState *gs, byte row, byte col);
|
|
|
|
|
|
byte GameState_detectVerticalPrefix(const GameState *gs, byte row, byte col);
|
|
|
|
|
|
byte GameState_detectVerticalSuffix(const GameState *gs, byte row, byte col);
|
|
|
|
|
|
// Returns the length of the detected word, or zero if none found.
|
|
// If the returned length is not zero, *startCol becomes the starting column
|
|
// of the detected word.
|
|
//
|
|
byte GameState_detectHorizontalWordLimits(const GameState *gs,
|
|
byte row, byte col,
|
|
byte *startCol);
|
|
|
|
|
|
// Returns the length of the detected word, or zero if none found.
|
|
// If the returned length is not zero, *startRow becomes the starting row
|
|
// of the detected word.
|
|
//
|
|
byte GameState_detectVerticalWordLimits(const GameState *gs,
|
|
byte row, byte col,
|
|
byte *startRow);
|
|
|
|
|
|
#endif /* _H_GameState */
|