Getting tiles relative to in a Grid of tiles

So, if your board is h*w, and you are indexing them as

1 2 3 4 5 …w

w+1 w+2 w+3…2w

h*(w-1)…h*w

To find the grid one above, you add -w to the index.

To find the grid one below, add w.

To find the left/right, add -1/1.

To check if you are in the left or right edges of your grid, check if your index%w == 1 or w-1.

To check if you are in the top or bottom edges, check if your index is less than w (top edge) or more than h*(w-1) (bottom edge).

Its a lot simpler in C++ with 2d arrays, if you know how to use C++ i recommend you use it instead. If the math above is too much work for you, or your game has a lot of complex rules, you can check out this solution instead:

You basically create a struct called row, that has an array with the size of your w, and your board is an array of rows.