public void Clear() { foreach (Direction dir in DirectionUtils.Directions()) { Set(dir, false); } }
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); }
static void Propogate(Tile[,] map, int fromX, int fromY) { var tile = map[fromX, fromY]; foreach (Direction dir in DirectionUtils.Directions()) { bool?valueInDir = tile.Get(dir); if (valueInDir == null) { continue; } (Tile neighbor, int neighborX, int neighborY) = map.ValueInDirection(fromX, fromY, dir); if (neighbor != null) { if (neighbor.TryResolve(dir.Opposite(), valueInDir.Value)) { Propogate(map, neighborX, neighborY); } } } }
private Direction[] UnresolvedDirections() => DirectionUtils.Directions() .Where(dir => Get(dir) == null) .ToArray();
public int Count(Func <bool?, bool> predicate) => DirectionUtils .Directions() .Select(dir => predicate(Get(dir))) .Count(hasValue => hasValue);