Reputation: 67
So i have a working getNeighbors function:
(its static based on how large my map is, havent made it dynamic yet.. although that shouldnt be an issue)
its 19x12
public int[] getNeighbors(int i) {
int[] neighbors = new int[6]; if (((i % xlength) % 2) == 0) { neighbors[0] = (i - 19); neighbors[1] = (i - 18); neighbors[2] = (i + 1); neighbors[3] = (i + 19); neighbors[4] = (i - 1); neighbors[5] = (i - 20); } else { neighbors[0] = (i - 19); neighbors[1] = (i + 1); neighbors[2] = (i + 20); neighbors[3] = (i + 19); neighbors[4] = (i + 18); neighbors[5] = (i - 1); }
}
It works all fine and dandy, but hangs when i am on the edges.
I dont know if i should do a case-by-case basis, or if i am overthinking it. Right now i have an automated script for moving dots randomly from hex to hex, and sometimes it treats it as a torus grid and hops over to the other side (not what i want, but i expected it with the current code) or just hangs in an infinite loop that im searching for.
Upvotes: 3
Views: 921
Reputation: 425198
In cases like this where your algorithm has problems, it can be a lot simpler to make the board one tile larger all the way around the edge. You would of course restrict them being used for positions, but your neighbours logic then doesn't need to be different at the edges.
Upvotes: 4