12
I am writing a simulator of a game called Iso-Path. It consists of a hexagonal grid, being itself formed of hexagons
You can see what I’ve done on this link
But I stopped in time to catch the neighbors of a certain cell.
For example, cell 5 has 6 neighbors (0, 1, 4, 6, 10, 11)
So let’s go to the code
var tabuleiro = "4567654";
podeMover(5, 0); // => true
podeMover(5, 1); // => true
podeMover(5, 4); // => true
podeMover(5, 7); // => false (Não são vizinhos)
A cell on the horizontal edge would also have as neighbor the cell on the other horizontal edge. Soon...
podeMover(22, 27); // true
I might as well create an array with each neighbor manually, but I would like to know how to do this procedurally.
The board would always be a hexagon composed of smaller hexagons, so valid boards would be represented by strings such as (where each number describes the amount of smaller hexagons in a row of the larger hexagon, indicated from left to right as they appear from top to bottom in the figure):
"3456543"
"4567654" // Padrão Iso-path
"567898765" // ... etc
I know it’s not the answer you’re looking for, but when I’ve been reading a little bit about this topic, I found this site very useful, a lot of information about game development, sending the link to the neighbors part, might have something to help you find the https solution://www.redblobgames.com/grids/hexagons/#Neighbors (in English)
– Leite
If you can tell me what the structure that stores the hexagon looks like, I might even try to help. For example, if it is line x position, if it is left or center aligned. etc.(remembering that in most cases a map of hexagons is the same thing as a square, with the only difference that at each row or column an axis is moved in 50% of the unit)
– Bacco
@But in this case each line has a size. And as the goal is to catch the neighbors, I believe it is important that hexagons have more edges (therefore, more neighbors).
– bfavaretto
@bfavaretto example of checkered with 6 neighbors, coordinates X and Y only: https://i.stack.Imgur.com/Twfvy.png (the "secret" is the offset). So I asked for the structure, because depending on the way it stores it has different ways of doing it. Depending on the case the length of the line can make a difference, depending not. Now, of course I also find important an answer that considers only the number of the square, for it to be canonical (still, in this ideal case, somewhere we have to store the size of the hexagon, either by the side).
– Bacco
These rules hinder a little, because we do not know if it is only horizontal, vertical, and runs away from what would be natural (go to the opposite side, and not "mirror"): "A cell on the horizontal edge would also have the cell on the other horizontal edge. Soon..."
– Bacco