# Condense ridiculous nested if statement

Started off with a course from StackSkills I got for free and expanding on it to make an actual game. I’ve got everything working the way I want it, but I feel like the code is an absolute mess and I’m not sure how to condense it down. It highlights tiles that you can build on, max distance of 2 away and taking walls/corners into account.

``````// displays the tiles which we can place a building on
public void EnableUsableTiles ()
{
foreach(Building building in buildings)
{
//loop to check in a 2 radius square around the building
for (int i=-2; i<=2; i++)
{
for (int j=-2; j<=2; j++)
{
//only check 2 tiles away from the building, cutting off corners from the loop
if (Mathf.Abs(i)+Mathf.Abs(j)<=2)
{
Tile highlightTile = GetTileAtPosition(building.transform.position + new Vector3(i, j, 0));
if (Mathf.Abs(i) == 2)
{
//check if wall is between building and tile 2 spaces away in the up/down direction
Tile checkTile = GetTileAtPosition(building.transform.position + new Vector3(i/2, j, 0));
if (checkTile && !checkTile.isImpassable)
{
highlightTile?.ToggleHighlight(true);
}
}
else if ((Mathf.Abs(j) == 2))
{
//check if wall is between building and tile 2 spaces away in the left/right direction
Tile checkTile = GetTileAtPosition(building.transform.position + new Vector3(i, j/2, 0));
if (checkTile && !checkTile.isImpassable)
{
highlightTile?.ToggleHighlight(true);
}
}
else if ((Mathf.Abs(i) == 1) && (Mathf.Abs(j) == 1))
{
//check if we can build in a corner next to the building
Tile checkTile1 = GetTileAtPosition(building.transform.position + new Vector3(i, 0, 0));
Tile checkTile2 = GetTileAtPosition(building.transform.position + new Vector3(0, j, 0));
if (checkTile1 && checkTile2)
{
if (!checkTile1.isImpassable && !checkTile2.isImpassable)
{
//if both tiles adjacent to the building are open
highlightTile?.ToggleHighlight(true);
}
else if (checkTile1.isImpassable && !checkTile2.isImpassable)
{
//if tile adjacent to building in the up/down direction is open
highlightTile?.ToggleHighlight(true);
}
else if (!checkTile1.isImpassable && checkTile2.isImpassable)
{
//if tile adjacent to building in the left/right direction is open
highlightTile?.ToggleHighlight(true);
}
}
}
else
{
highlightTile?.ToggleHighlight(true);
}
}
}
}
}
}
``````

# CODE NOT TESTED

``````// displays the tiles which we can place a building on
public void EnableUsableTiles ()
{
foreach(Building building in buildings)
EnableUsableTimesAroundBuilding(building.transform.position);
}

private void EnableUsableTimesAroundPosition(Vector3 position)
{
//loop to check in a 2 radius square around the building
for (int i = -2; i <= 2; i++)
{
int absI = Mathf.Abs(i);
for (int j = -2; j <= 2; j++)
{
int absJ = Mathf.Abs(j);

//only check 2 tiles away from the building, cutting off corners from the loop
if(absI + absJ > 2)
continue;

Tile highlightTile = GetTileAtPosition(position + new Vector3(i, j, 0));
if(highlightTile != null)
{
//check if wall is between building and tile 2 spaces away in the up/down direction
if(absI == 2 && TilesArePassable(position + new Vector3(i/2, j, 0)))
highlightTile.ToggleHighlight(true);

//check if wall is between building and tile 2 spaces away in the left/right direction
else if(absJ == 2 && TilesArePassable(position + new Vector3(i, j/2, 0)))
highlightTile.ToggleHighlight(true);

//if both tiles adjacent to the building are open
else if(absI == 1 && absJ == 1 && TilesArePassable(position + new Vector3(i, 0, 0), position + new Vector3(0, j, 0)))
highlightTile.ToggleHighlight(true);
}
}
}
}

private bool TilesArePassable(params Vector3[] tilePositions)
{
for(int tileIndex = 0 ; tileIndex < tilePositions.Length ; ++tileIndex)
{
Tile tile = GetTileAtPosition(tilePositions[tileIndex]);
if(tile == null || tile.isImpassable)
return false;
}
return true;
}
``````