/// <summary>Override; see base.</summary> public override ConstraintResult Process(SolverState state) { for (var cellIx = 0; cellIx < (state.LastPlacedCell != null ? 1 : AffectedCells != null ? AffectedCells.Length : state.GridSize); cellIx++) { var cell = state.LastPlacedCell ?? (AffectedCells != null ? AffectedCells[cellIx] : cellIx); if (state[cell] == null || (AffectedValues != null && !AffectedValues.Contains(state[cell].Value))) { continue; } foreach (var relatedCell in AdjacentCells(cell, GridWidth, GridHeight, IncludeDiagonals)) { if (EnforcedCells == null || (EnforcedCellsOnly ? (EnforcedCells.Contains(cell) && EnforcedCells.Contains(relatedCell)) : (EnforcedCells.Contains(cell) || EnforcedCells.Contains(relatedCell)))) { if (state[cell].Value > state.MinValue && (AffectedValues == null || AffectedValues.Contains(state[cell].Value - 1))) { state.MarkImpossible(relatedCell, state[cell].Value - 1); } if (state[cell].Value < state.MaxValue && (AffectedValues == null || AffectedValues.Contains(state[cell].Value + 1))) { state.MarkImpossible(relatedCell, state[cell].Value + 1); } } } } return(null); }
/// <summary>Override; see base.</summary> public override sealed ConstraintResult Process(SolverState state) { for (var cellIx = 0; cellIx < (state.LastPlacedCell != null ? 1 : AffectedCells != null ? AffectedCells.Length : state.GridSize); cellIx++) { var cell = state.LastPlacedCell ?? (AffectedCells != null ? AffectedCells[cellIx] : cellIx); if (state[cell] == null || (AffectedValues != null && !AffectedValues.Contains(state[cell].Value))) { continue; } foreach (var relatedCell in getRelatedCells(cell)) { if (EnforcedCells == null || EnforcedCells.Contains(relatedCell) || EnforcedCells.Contains(cell)) { state.MarkImpossible(relatedCell, state[cell].Value); } } } return(null); }
/// <summary> /// Constructor.</summary> /// <param name="gridWidth"> /// See <see cref="GridWidth"/>.</param> /// <param name="gridHeight"> /// See <see cref="GridHeight"/>.</param> /// <param name="includeDiagonals"> /// See <see cref="IncludeDiagonals"/>.</param> /// <param name="affectedValues"> /// See <see cref="AffectedValues"/>.</param> /// <param name="enforcedCells"> /// See <see cref="EnforcedCells"/>. If <c>null</c>, the default is to enforce the entire grid.</param> public NoConsecutiveConstraint(int gridWidth, int gridHeight, bool includeDiagonals, int[] affectedValues = null, IEnumerable <int> enforcedCells = null, bool enforcedCellsOnly = false) : base(null) { GridWidth = gridWidth; GridHeight = gridHeight; IncludeDiagonals = includeDiagonals; AffectedValues = affectedValues; EnforcedCells = enforcedCells?.ToArray(); EnforcedCellsOnly = enforcedCellsOnly; AffectedCells = EnforcedCells == null ? null : EnforcedCellsOnly ? EnforcedCells : EnforcedCells.SelectMany(cell => AdjacentCells(cell, gridWidth, gridHeight, includeDiagonals)).Distinct().ToArray(); }