private static bool HasFilledNeighbour(Cell testCell, HexBoard board) { Cell[] neighbours = board.Neighbours(testCell); foreach (Cell neighbour in neighbours) { if (!neighbour.IsEmpty()) { return true; } } Cell[][] neighbours2 = board.Neighbours2(testCell); foreach (Cell[] triple in neighbours2) { if (!triple[0].IsEmpty()) { return true; } } return false; }
private static void DoTestNeighbour(Cell cell, Cell neibCell, HexBoard board) { Assert.IsTrue(cell.Location.ManhattanDistance(neibCell.Location) < 3, "Neigbour is too far away"); var neighboursTest = new HexBoardNeighbours(BoardSize); Assert.IsTrue(neighboursTest.AreNeighbours(cell.Location, neibCell.Location)); // reflexive. If B is a neighbour of A, then B's neighbours must include A var neibs = board.Neighbours(neibCell); int index = Array.IndexOf(neibs, cell); Assert.IsTrue(index >= 0, "Cell is not neighbour's neighbour"); }
private static bool IsIncluded(Cell testCell, HexBoard board) { int boardSize = board.Size; int x = testCell.X; int y = testCell.Y; // corner cells are always in if (((x == 0) || (x == (boardSize - 1))) && ((y == 0) || (y == boardSize - 1))) { return true; } // cells along the edges if ((x == 0) || (x == (boardSize - 1))) { if ((y % EdgeInterval) == 0) { return true; } } if ((y == 0) || (y == (boardSize - 1))) { if ((x % EdgeInterval) == 0) { return true; } } // interior cells if ((x != 0) && (x != (boardSize - 1)) && (y != 0) && (y != (boardSize - 1))) { if (((x - 1) % InternalInterval == 0) && ((y - 1) % InternalInterval == 0)) { return true; } } // are any neighbours filled? var neighbours = board.Neighbours(testCell); foreach (Cell neighbourCell in neighbours) { if (!neighbourCell.IsEmpty()) { return true; } } return false; }
public void BoardNeighboursTest() { HexBoard hexBoard = new HexBoard(BoardSize); for (int x = 0; x < hexBoard.Size; x++) { for (int y = 0; y < hexBoard.Size; y++) { Cell cell = hexBoard.GetCellAt(x, y); var neigbours = hexBoard.Neighbours(cell); Assert.IsNotNull(neigbours); /* all cells have at least 2 neighbours, and at most 6 */ Assert.GreaterOrEqual(neigbours.Length, 2); Assert.LessOrEqual(neigbours.Length, 6); /* cells not on the edge will have 6 neighbours */ if ((x > 0) && (y > 0) && (x < (hexBoard.Size - 1)) && (y < (hexBoard.Size - 1))) { Assert.AreEqual(neigbours.Length, 6); } NoNullsInCellArray(neigbours); foreach (Cell neibCell in neigbours) { DoTestNeighbour(cell, neibCell, hexBoard); } } } }