This repository aims to develop a strategy game similar to 'Fire Emblem Heroes' using React. The settings page is in FrontPage.js, and GameUI.js is the main interface for gameplay. Please create a helper function which return grids from the GameMap 2D array, which are nearest x grids based on a certain single grid, similar to the function 'calculateMovementRange' in GameMap.js.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll help you create a helper function to find the nearest x grids from a given grid position. Based on the repository context, we can see that calculateMovementRange uses terrain costs and movement types, but for finding nearest grids, we'll use a simpler approach with Manhattan distance (similar to how calculateCellsInRadius works).
Here's the helper function we can add to GameMap.js:
javascript
/**
* Finds the x nearest grids to a given position using Manhattan distance
* @param {number} centerRow - Starting row coordinate
* @param {number} centerCol - Starting column coordinate
* @param {number} count - Number of nearest grids to return
* @param {Array<Array<string>>} terrainGrid - Grid of terrain types
* @param {Array<{row: number, col: number}>} [excludedCells=[]] - Array of coordinates to exclude
* @returns {Array<{row: number, col: number, distance: number}>} Array of nearest valid cells with their distances
*/
export const findNearestGrids = (centerRow, centerCol, count, terrainGrid, excludedCells = []) => {
const allCells = [];
const excludedSet = new Set(
excludedCells.map(cell => `${cell.row},${cell.col}`)
);
// Check all cells in the grid
for (let row = 0; row < gridSize.rows; row++) {
for (let col = 0; col < gridSize.cols; col++) {
// Skip if this is the center cell or an excluded cell
if ((row === centerRow && col === centerCol) ||
excludedSet.has(`${row},${col}`)) {
continue;
}
// Skip if terrain is impassable (WALL)
if (terrainGrid[row][col] === TerrainType.WALL) {
continue;
}
// Calculate Manhattan distance
const distance = Math.abs(row - centerRow) + Math.abs(col - centerCol);
allCells.push({
row,
col,
distance
});
}
}
// Sort cells by distance and return the nearest 'count' cells
return allCells
.sort((a, b) => a.distance - b.distance)
.slice(0, count);
};
This function:
Takes parameters for:
centerRow/centerCol: The starting position
count: How many nearest grids to return
terrainGrid: The current map's terrain data
excludedCells: Optional array of cells to exclude from consideration
Uses Manhattan distance (|row1-row2| + |col1-col2|) like the existing functions in the codebase
This function complements the existing calculateMovementRange and calculateCellsInRadius functions, but focuses specifically on finding the nearest grids regardless of movement costs. It's useful for:
Finding nearby targets for AI movement
Calculating closest allies/enemies
Determining optimal positions for support abilities
Planning strategic movements
The function maintains the same style and approach as the existing codebase, using similar parameter structures and utilizing the existing gridSize and TerrainType constants.