public List <Node> GetNeighbours(Node node, bool useDiagnonal) { var neighbours = new List <Node>(); var rightCell = CellPos.FirstOrDefault(nodec => nodec.X == node.X + 1 && nodec.Y == node.Y); var leftCell = CellPos.FirstOrDefault(nodec => nodec.X == node.X - 1 && nodec.Y == node.Y); var bottomCell = CellPos.FirstOrDefault(nodec => nodec.X == node.X && nodec.Y == node.Y + 1); var topCell = CellPos.FirstOrDefault(nodec => nodec.X == node.X && nodec.Y == node.Y - 1); if (rightCell != null) { neighbours.Add(rightCell); } if (leftCell != null) { neighbours.Add(leftCell); } if (bottomCell != null) { neighbours.Add(bottomCell); } if (topCell != null) { neighbours.Add(topCell); } if (!useDiagnonal) { return(neighbours); } var topLeftCell = CellPos.FirstOrDefault(nodec => nodec.X == node.X - 1 && nodec.Y == node.Y - 1); var bottomRightCell = CellPos.FirstOrDefault(nodec => nodec.X == node.X + 1 && nodec.Y == node.Y + 1); var bottomLeftCell = CellPos.FirstOrDefault(nodec => nodec.X == node.X - 1 && nodec.Y == node.Y + 1); var topRightCell = CellPos.FirstOrDefault(nodec => nodec.X == node.X + 1 && nodec.Y == node.Y - 1); if (topLeftCell != null) { neighbours.Add(topLeftCell); } if (bottomRightCell != null) { neighbours.Add(bottomRightCell); } if (bottomLeftCell != null) { neighbours.Add(bottomLeftCell); } if (topRightCell != null) { neighbours.Add(topRightCell); } return(neighbours); }