static bool ClearLoopsAndCheck(Tile[,] map) { int startX = 0; int startY; for (startY = 0; startY < MAP_HEIGHT; startY++) { Tile tile = map[0, startY]; if (tile.Type == TileType.Start) { break; } } bool homeOnPath = false; void Search(int fromX, int fromY) { Tile tile = map.MaybeGet(fromX, fromY); if (tile == null || tile.OnPath) { return; } tile.OnPath = true; homeOnPath = homeOnPath || tile.Type == TileType.Home; foreach (Direction dir in DirectionUtils.Directions()) { if (tile.Get(dir) == true) { Search(fromX + dir.X(), fromY + dir.Y()); } } } Search(startX, startY); if (!homeOnPath) { return(false); } for (int x = 0; x < MAP_WIDTH; x++) { for (int y = 0; y < MAP_HEIGHT; y++) { var tile = map[x, y]; if (!tile.OnPath) { tile.Clear(); } } } return(true); }