public CalculationContext Create(CalculationContext source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(Create(source.Grid)); }
private Boolean ProcessSquares(CalculationContext context) { foreach (GridPoint cell in Defs.Squares) { if (!Process(_cellsInfoFactory.CreateForSquare(context.Grid, cell), context)) { return(false); } } return(true); }
private Boolean ProcessColumns(CalculationContext context) { foreach (Int32 column in Enumerable.Range(1, Defs.GridSide)) { if (!Process(_cellsInfoFactory.CreateForColumn(context.Grid, column), context)) { return(false); } } return(true); }
private Boolean Process(CellsInfo cellsInfo, CalculationContext context) { NumbersInfo numbersInfo = _numbersInfoFactory.Create(cellsInfo); Tuple <GridPoint, Int32> data = ChooseCell(cellsInfo, numbersInfo); while (data != null) { context.Grid[data.Item1] = data.Item2; context.EmptyCount--; UseCell(cellsInfo, numbersInfo, data); if (!_checker.Check(cellsInfo, numbersInfo)) { return(false); } data = ChooseCell(cellsInfo, numbersInfo); } return(true); }
public CalculationResultData Process(CalculationContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } CalculationContinuation continuation; do { Int32 emptyCount = context.EmptyCount; continuation = ExecuteProcessors(context, ProcessRows, ProcessColumns, ProcessSquares); if (context.EmptyCount == 0) { continuation = CalculationContinuation.Finish; } if (emptyCount == context.EmptyCount) { continuation = CalculationContinuation.NeedPrediction; } } while (continuation == CalculationContinuation.Continue); return(CreateCalculationResultData(continuation, context)); }
private CalculationContinuation ExecuteProcessors(CalculationContext context, params Func <CalculationContext, Boolean>[] processors) { return(processors.Any(processor => !processor(context)) ? CalculationContinuation.Stop : CalculationContinuation.Continue); }
private CalculationResultData CreateCalculationResultData(CalculationContinuation continuation, CalculationContext context) { switch (continuation) { case CalculationContinuation.Finish: return(new CalculationResultData(CalculationResult.Finish, context)); case CalculationContinuation.NeedPrediction: return(new CalculationResultData(CalculationResult.NeedPrediction, context)); case CalculationContinuation.Stop: return(new CalculationResultData(CalculationResult.Stop, null)); default: throw new InvalidOperationException(); } }