private bool HasDestinationToMove(IChainLink <IMainFieldSquare, Direction> item, out int destination) { destination = -1; if (!item.Value.State.IsActive || item.Value.State.Direction.IsNone()) { return(false); } var direction = item.Value.State.Direction; // look for an active square; var nextItem = item.Next(direction.Value); while (nextItem != null && !nextItem.Value.State.IsActive) { nextItem = nextItem.Next(direction.Value); } if (nextItem != null && nextItem != item.Next(direction.Value)) { // destination is last inactive square (before first active square!) on the way. var prevItem = nextItem.Next(direction.Opposite).Value; destination = direction.IsHorzOrient() ? (int)prevItem.X : (int)prevItem.Y; } else if (nextItem == null || CanIgnoreObstacles(item)) { // if there aren't any obstacle or they can be ignored, // so destination is the last square on the way destination = (int)GetLastIndexInDirection(direction.Value); } return(destination != -1); }
private void CountCombinationSize(IChainLink <IMainFieldSquare, Direction> item, SquareCombinationCounter counter = null) { if (item == null) { return; } var value = item.Value; // skip if item is not active or it was already counted as part of combination if (!value.State.IsActive || _matrixCounterRefs[value.X, value.Y] != null) { return; } if (counter == null) { counter = new SquareCombinationCounter(value.Color); _counters.Add(counter); } if (counter.Color != value.Color) { return; } counter.Squares.Add(value); _matrixCounterRefs[value.X, value.Y] = counter; CountCombinationSize(item.Next(Direction.Left), counter); CountCombinationSize(item.Next(Direction.Down), counter); CountCombinationSize(item.Next(Direction.Right), counter); CountCombinationSize(item.Next(Direction.Up), counter); }
/// <summary> /// Find out if squares on the way can be ignored. /// There are two consecutive conditions: /// 1) all active squares on the way move opposite direction; /// 2) initial obstacle that the square adjacent to was destroyed and /// all squares on the way are adjacent to the same initial obstacle; /// /// If the first case is false so the second one should be false as well to /// return negative result otherwise there is positive result. /// </summary> private bool CanIgnoreObstacles(IChainLink <IMainFieldSquare, Direction> item) { var direction = item.Value.State.Direction; var obstacle = item.Value.State.Obstacle; var nextItem = item.Next(direction.Value); var initialObstacle = _initialObstacles[item.Value.State.Obstacle]; var obstaclesIgnored = !this[initialObstacle.X, initialObstacle.Y].Value.State.IsActive; while (nextItem != null) { var square = nextItem.Value; if (square.State.IsActive) { obstaclesIgnored = obstaclesIgnored && square.State.Obstacle == obstacle; if ((square.State.Direction.Value != direction.Opposite) && !obstaclesIgnored) { return(false); } } nextItem = nextItem.Next(direction.Value); } return(true); }