- All Implemented Interfaces:
CellManager
CellGrid class is responsible for managing a grid of Cell
objects, including their creation, clustering, and global operations.
This class provides a structured way to represent a grid of characters, where
each character is encapsulated in a Cell object. The grid can contain
both letters and symbols, which are automatically categorised and clustered
into CellCluster objects based on the provided
ClusterStrategy.
Key responsibilities of the CellGrid include:
- Cell Creation: Initialises all
Cellobjects based on the provided character grid. The cells are stored in a contiguous array for efficient memory usage and easy access. - Clustering: Clusters the cells into groups of letters and symbols
using the provided
ClusterStrategy. These clusters are stored separately for quick access and manipulation. - Global Operations: Provides various operations that can be performed on the entire grid, such as retrieving specific cells, accessing the list of letter or symbol clusters, and searching for and removing specific word clusters.
- Memory Efficiency: Maintains references to all
Cellobjects in a flat array, ensuring efficient memory usage and simplifying global operations that need to access all cells.
The CellGrid does not maintain a persistent reference to the
ClusterStrategy used during initialisation; it is only used during
the setup process to categorise and cluster the cells according to the
specific strategy's logic.
Example use cases include:
- Creating a grid from a matrix of characters and clustering them into words and symbols.
- Retrieving the words formed by letter clusters for further processing or display.
- Performing operations on the entire grid of cells, such as clearing specific clusters or querying the grid for specific patterns.
Important Constraints:
- The character grid provided during instantiation must be rectangular; that is, all rows must have the same length.
- The
ClusterStrategyprovided must not benulland must be able to handle the clustering of both letter and symbol cells.
- Author:
- Kheagen Haskins
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCellGrid(String text, ClusterStrategy clusterStrategy) Constructs a newCellGridwith the specified text grid and clustering strategy.CellGrid(String text, ClusterStrategy clusterStrategy, int rows, int cols) Constructs a newCellGridwith the specified text grid and clustering strategy. -
Method Summary
Modifier and TypeMethodDescriptiongetCell(int index) Retrieves theCellat the specified index from the internal cell array.getCellAt(int row, int col) Returns theCelllocated at the specified row and column in the grid.Cell[]getCells()Returns a flat array of all theCellobjects in the grid.Cell[][]Returns the 2D arrangement of Cells.intRetrieves the number of columns in the grid.Returns the list of letter clusters in the grid.intRetrieves the number of rows in the grid.Returns the list of symbol clusters in the grid.String[]getWords()Returns an array of words formed by the letter clusters in the grid.Searches through the letter clusters for a cluster containing the specifieddudText, removes it, and returns the text of the removed cluster.
-
Constructor Details
-
CellGrid
Constructs a newCellGridwith the specified text grid and clustering strategy.This constructor initialises the grid of
Cellobjects and clusters them based on the providedClusterStrategy. The grid must be rectangular, with all rows of equal length, and both the grid and strategy must be non-null.- Parameters:
text- A string of characters representing the text grid to be converted into cells.clusterStrategy- The strategy used to cluster the cells into letter and symbol clusters.rows- the number of rows in this gridcols- the number of columns in this grid- Throws:
IllegalArgumentException- If thetextGridisnullor does not match the size of the grid as specified by the number of rows and columns, or if theclusterStrategyisnull, or if the grid is not rectangular.
-
CellGrid
Constructs a newCellGridwith the specified text grid and clustering strategy.This constructor initialises the grid of
Cellobjects and clusters them based on the providedClusterStrategy. The grid must be rectangular, with all rows of equal length, and both the grid and strategy must be non-null.- Parameters:
text- A string of characters representing the text grid to be converted into cells.clusterStrategy- The strategy used to cluster the cells into letter and symbol clusters.- Throws:
IllegalArgumentException- If thetextGridisnullor if theclusterStrategyisnull, or if the grid is not rectangular.
-
-
Method Details
-
getCells
Returns a flat array of all theCellobjects in the grid.This method provides access to the entire grid of cells as a single array, which is useful for performing uniform operations across all cells.
- Specified by:
getCellsin interfaceCellManager- Returns:
- An array of
Cellobjects representing the entire grid.
-
getCells2D
Returns the 2D arrangement of Cells.- Specified by:
getCells2Din interfaceCellManager- Returns:
-
getCell
Retrieves theCellat the specified index from the internal cell array.This method calculates the total number of cells based on the dimensions of rows and columns. It then checks if the provided index is within the valid range of indices. If the index is out of bounds, an
ArrayIndexOutOfBoundsExceptionis thrown.- Specified by:
getCellin interfaceCellManager- Parameters:
index- the index of theCellto retrieve.- Returns:
- the
Cellat the specified index. - Throws:
ArrayIndexOutOfBoundsException- if the index is less than 0 or greater than or equal to the total number of cells.
-
getCellAt
Returns theCelllocated at the specified row and column in the grid.This method retrieves a specific
Cellbased on its position in the grid. If the provided row or column index is out of bounds, anIllegalArgumentExceptionis thrown.- Specified by:
getCellAtin interfaceCellManager- Parameters:
row- The row index of the cell to retrieve.col- The column index of the cell to retrieve.- Returns:
- The
Celllocated at the specified row and column. - Throws:
IllegalArgumentException- If the row or column index is out of bounds.
-
getRowCount
public int getRowCount()Retrieves the number of rows in the grid. This method provides access to the private field storing the number of rows, allowing external classes to understand the grid's vertical dimensions without modifying the data.- Returns:
- the number of rows in the grid
-
getColumnCount
public int getColumnCount()Retrieves the number of columns in the grid. This method provides access to the private field storing the number of columns, enabling external classes to understand the grid's horizontal dimensions without altering the underlying structure.- Returns:
- the number of columns in the grid
-
getLetterClusters
Returns the list of letter clusters in the grid.This method provides access to all clusters of letter cells that have been identified in the grid.
- Specified by:
getLetterClustersin interfaceCellManager- Returns:
- A list of
CellClusterobjects containing letter cells.
-
getSymbolClusters
Returns the list of symbol clusters in the grid.This method provides access to all clusters of symbol cells that have been identified in the grid.
- Specified by:
getSymbolClustersin interfaceCellManager- Returns:
- A list of
CellClusterobjects containing symbol cells.
-
getWords
Returns an array of words formed by the letter clusters in the grid.This method retrieves the text content of each letter cluster and returns them as an array of strings, effectively representing the words in the grid.
- Specified by:
getWordsin interfaceCellManager- Returns:
- An array of
Stringobjects, each representing a word formed by a letter cluster.
-
removeDud
Searches through the letter clusters for a cluster containing the specifieddudText, removes it, and returns the text of the removed cluster.This method iterates through all remaining letter clusters to find one that matches the provided
dudText. If a matching cluster is found, it is cleared and removed from the list of clusters, and its text is returned. If no matching cluster is found, the method returnsnull.- Specified by:
removeDudin interfaceCellManager- Parameters:
dudText- The text of the cluster to be removed.- Returns:
- The text of the removed cluster, or
nullif no matching cluster is found.
-