How To Calculate And/Or Store Adjacent Grid Spaces On Board Game

I’m trying to figure out a quick way to calculate the adjacent spaces of a grid based on the starting location of a given game piece. The board is 5x5 and the pieces can only move laterally (forward, backward, left, and right). If, for example, the piece in question is at grid 0, it can move from 1 to 4, and 5, 10, 15, and 20. See screenshot below:

I’m hoping someone has come across something similar and has the maths on hand.

Worst case scenario, I will manually store the data for each of the 25 grids, but I can’t seem to figure out how to organize the ‘Data Table’ and/or ‘Structure’. Perhaps, I’m overthinking something or just need some rest…

Might be easier to grab one of the many grid movement plugins. eg: Grid System Plugin | Fab

you could convert the index to a grid index (int to int point)

x = index % columns;
y = Math.floor(index / columns);

then if target.x == x || target.y == y its a valid move

you dont need to store the 25 grids. with a 5x5 board the math is just index to row and column: row = index / 5 and col = index % 5 (integer division). then the neighbours of any index are index-1 if col > 0, index+1 if col < 4, index-5 if row > 0, index+5 if row < 4. one small function returning an array of valid indexes and you’re done.

if you want it in blueprint, do the same with the integer Divide and Modulo nodes, and skip any candidate that fails the bounds check instead of wrapping around, otherwise grid 4 ends up next to grid 5.