# Detect a triangle of 6 in hexagonal grid

Hey there. I want to detect a triangle of 6 around a tile that I’ve just placed in a hexagonal grid.

Since my grid is pointed top a triangle of 6 tiles can only go up or down.

I want to detect all the tiles with the same owner (player) that are around the tile that I’ve just placed and that are useable to make a triangle of 6. First I thought this wouldn’t be so hard, but it really is harder than it might look at first.

My last attempt looks like this:

``````public static class Course {
public static class TriangleUp {
public static int[] forward = new int[] { 1, 1, 3, 3, 5, 5 };
public static int[] backward = new int[] { 2, 2, 0, 0, 4, 4 };
}
public static class TriangleDown {
public static int[] forward = new int[] { 2, 2, 4, 4, 0, 0 };
public static int[] backward = new int[] { 3, 3, 1, 1, 5, 5 };
}
}

public static Tile[] WalkTriangle(Tile from, int step, bool back = false, bool down = false) {
List<Tile> result = new List<Tile>();
int[] course;
if (!down) {
if (!back) course = Course.TriangleUp.forward;
else course = Course.TriangleUp.backward;
}
else {
if (!back) course = Course.TriangleDown.forward;
else course = Course.TriangleDown.backward;
}
int i = 0;
Tile tile;
while ((tile = from.GetNeighbour(Wheel<int>.SpinArray(course, step, i))) != null) {
if (tile.Owner != result[0].Owner) break;
if (result.Count == 6) break;
i++;
from = tile;
}

return result.ToArray();
}
public static Tile[] LongestTriangle(Tile from) {
List<Tile>[] upf = new List<Tile>[6];
int uflv = 0;
int ufli = 0;
for (int i = 0; i < 6; i++) {
upf[i] = new List<Tile>();
if (upf[i].Count > uflv) {
uflv = upf[i].Count;
ufli = i;
}
}
if (uflv == 6) return upf[ufli].ToArray();
List<Tile>[] upb = new List<Tile>[6];
int ublv = 0;
int ubli = 0;
for (int i = 0; i < 6; i++) {
upb[i] = new List<Tile>();
if (upb[i].Count > ublv) {
ublv = upb[i].Count;
ubli = i;
}
}
if (ublv == 6) return upb[ubli].ToArray();
List<Tile>[] downf = new List<Tile>[6];
int dflv = 0;
int dfli = 0;
for (int i = 0; i < 6; i++) {
downf[i] = new List<Tile>();
if (downf[i].Count > dflv) {
dflv = downf[i].Count;
dfli = i;
}
}
if (dflv == 6) return downf[dfli].ToArray();
List<Tile>[] downb = new List<Tile>[6];
int dblv = 0;
int dbli = 0;
for (int i = 0; i < 6; i++) {
downb[i] = new List<Tile>();
if (downb[i].Count > dblv)
{
dblv = downb[i].Count;
dbli = i;
}
}
if (dblv == 6) return downb[dbli].ToArray();
if (uflv >= ublv && uflv >= dflv && uflv >= dblv) {
return upf[ufli].ToArray();
}
if (ublv >= uflv && ublv >= dflv && ublv >= dblv) {
return upb[ubli].ToArray();
}
if (dflv >= uflv && dflv >= ublv && dflv >= dblv) {
return downf[dfli].ToArray();
}
else return downb[dbli].ToArray();
}
``````

This doesn’t work for all possibilities.
Like this one for example wich should count 4 tiles:

Or this one wich also should count 4 tiles:

I’m having a really hard time getting around this logic s***. Maybe some of you masterminds can help me out a bit?

What do you mean by “triangle around a tile”? In your first example, the triangle shape isn’t around any 1 tile, but in-between 3 different tiles. In your second example, there’s no triangle at all.

Do you just want to find all the neighbours around one single tile?