Let's say I have a 1D game grid like the following:
As an example, the cell south of 27 will be cell 35, and the cell south of 59 will be 3 (because of wrap-around). This can be implemented like so:
var s = spot = 59
var r = row length = 8
var b = board size = 64
var south = (s+r) mod b
Okay, now let's try to find the cell east of another. The cell east of 27 is 28, and the cell east of 31 is 24 (also because of wrap-around). The best I could come up with is this:
var s = spot = 31
var r = row length = 8
var lc = left column = Math.floor(s / 8) * 8
var east = lc + ((s - lc + 1) % 8)
This is considerably more complex, which makes me think I'm missing something obvious. Is there not a better way to do this?
Also, I haven't yet implemented, but I'd imagine finding the diagonal cells like north-east and south-east even more complex still.
For the purposes of this question please assume this is limited to 1D arrays. Also, I'd imagine there is a bit-wise solution that could be more elegant for board sizes that are a power of two, but preferably the solution would work for any board size.
