Hexagons grid - return neighbors

10

I'm writing a game simulator called Iso-Path. It consists of a hexagonal grid, being itself formed of hexagons

You can see what I've already done on this link

But I packed the time to pick up the neighbors of a certain cell.

For example, cell 5 has 6 neighbors (0, 1, 4, 6, 10, 11)

Solet'sgotothecode

vartabuleiro="4567654";

podeMover(5, 0); // => true
podeMover(5, 1); // => true
podeMover(5, 4); // => true
podeMover(5, 7); // => false (Não são vizinhos)

One cell on the horizontal border would also have the cell of the other horizontal end. Soon ...

podeMover(22, 27); // true

Imightaswellcreateanarraywitheachneighbormanually,butI'dliketoknowhowtodothisprocedurally.

Theboardwouldalwaysbeahexagonmadeupofsmallerhexagons,sovalidtrayswouldberepresentedbystringsas(whereeachnumberdescribesthenumberofsmallerhexagonsinalineofthelargerhexagon,indicatedfromlefttorightastheyappearfromabovelowinfigure):

"3456543"
"4567654" // Padrão Iso-path
"567898765" // ... etc
    
asked by anonymous 29.08.2018 / 16:51

1 answer

-1

I think there are two possible solutions;

1) Find a mathematical formula that, based on the size of the super-hexagon, returns the numbers of neighboring hexagons. It would be very fast, but not very flexible.

2) Use a structure similar to a double-chained list, but with 6 links instead of two:

class Hexagono {
     int numero;
     Hexagono *uma_hora;
     Hexagono *tres_horas;
     Hexagono *cinco_horas;
     Hexagono *sete_horas;
     Hexagono *nove_horas;
     Hexagono *onze_horas;
}

(I used C ++ to illustrate the fact that Hexagon is an object and that links from the hexagon to neighbors should be references, not values, but the idea is readily applicable to any other language.)

It would give some work to create the hexagons, because you would have to assign your "neighbors", but once created, it is easy to know how many neighbors each one has.     

01.10.2018 / 04:40