public bool SearchSolution(SolutionState initialState) { Ensure.Arg(initialState, nameof(initialState)).IsNotNull(); Reset(); // Берем начальное состояние за текущее CurrentState = initialState; // Бежим по дереву состояний, пока не найдем или оно не кончится while (true) { // Мы пришли или вернулись на некоторое состояние, // надо его проверить и оповестить, а если оно конечное - то все if (CheckCurrentState()) { return(true); } // Если не конечное - идем по дереву, если еще есть куда if (!ProceedToNextState()) { return(false); } } }
public Figure(Color colorOfOriginCell, IEnumerable <Point> otherCellPoints) { Ensure.Arg(otherCellPoints, nameof(otherCellPoints)).IsNotNullOrEmpty(); ColorOfOriginCell = colorOfOriginCell; Cells = new ReadOnlyCollection <FigureCell>(CalculateCells(colorOfOriginCell, otherCellPoints)); }
public static Board CreateByPlacementNewFigure(Board prev, Figure figure, Point point) { Ensure.Arg(prev, nameof(prev)).IsNotNull(); Ensure.Arg(figure, nameof(figure)).IsNotNull(); return(new Board(prev, figure, point)); }
private Board(ICollection <Placement> figures) { Ensure.Arg(figures, nameof(figures)).IsNotNull(); Figures = figures; PointsMap = CalcPointsMap(); OddCellsColor = CalcOddCellsColor(); }
public Placement(Figure figure, Point point, int num) { Ensure.Arg(figure, nameof(figure)).IsNotNull(); Figure = figure; Num = num; Point = point; }
public static Figure Transform(Figure figure, Func <Point, Point> pointTransform) { Ensure.Arg(figure, nameof(figure)).IsNotNull(); Ensure.Arg(pointTransform, nameof(pointTransform)).IsNotNull(); return(new Figure( figure.ColorOfOriginCell, figure.Cells.Skip(1).Select(c => pointTransform(c.RelativePoint)))); }
private SolutionState(Board board, IList <Figure> restFigures, Dictionary <Figure, IList <Figure> > figureTransformations) { Ensure.Arg(board, nameof(board)).IsNotNull(); Ensure.Arg(restFigures, nameof(restFigures)).IsNotNull(); Ensure.Arg(figureTransformations, nameof(figureTransformations)).IsNotNull(); Board = board; RestFigures = new ReadOnlyCollection <Figure>(restFigures); FigureTransformations = figureTransformations; }
public static IList <Figure> GetTransformations(Figure figure) { Ensure.Arg(figure, nameof(figure)).IsNotNull(); return (GetRotates(figure) .Concat(GetRotates(FlipX(figure))) .Distinct(FigureLinearTransitionComparer.Instance) .ToList()); }
public static IEnumerable <Figure> GetRotates(Figure figure) { Ensure.Arg(figure, nameof(figure)).IsNotNull(); var rotate1 = RotateRight(figure); var rotate2 = RotateRight(rotate1); var rotate3 = RotateRight(rotate2); return(new[] { figure, rotate1, rotate2, rotate3 }); }
public bool IsValidPlacement(Figure figure, Point point) { Ensure.Arg(figure, nameof(figure)).IsNotNull(); Color?oddColor = OddCellsColor; return (IsPossiblePlacement(figure, point) && ( oddColor == null || CalculateOddCellsColor(point, figure.ColorOfOriginCell) == oddColor)); }
public bool IsPossiblePlacement(Figure figure, Point point) { Ensure.Arg(figure, nameof(figure)).IsNotNull(); return (figure.Cells.All(fc => { var p = fc.RelativePoint.Shift(point); return IsInRange(p) && !HasCell(p); })); }
public int Compare(FigureCell cellA, FigureCell cellB) { Ensure.Arg(cellA, nameof(cellA)).IsNotNull(); Ensure.Arg(cellB, nameof(cellB)).IsNotNull(); int colorDiff = ((int)cellA.Color).CompareTo((int)cellB.Color); return(colorDiff == 0 ? cellA.RelativePoint.CompareTo(cellB.RelativePoint) : colorDiff); }
private static IList <FigureCell> CalculateCells(Color colorOfOriginCell, IEnumerable <Point> otherCellPoints) { Ensure.Arg(otherCellPoints, nameof(otherCellPoints)).IsNotNull(); return (new[] { Point.Origin } .Concat(otherCellPoints) .Select( p => new FigureCell(p, CalculateColor(colorOfOriginCell, p))) .ToList()); }
public SolutionState CreateNextState(Decision decision) { Ensure.Arg(decision, nameof(decision)).IsNotNull(); var newBoard = Board.CreateByPlacementNewFigure( Board, decision.TransformedFigure, decision.PlacementPoint); return (new SolutionState(newBoard, RestFigures.Where(f => f != decision.SelectedFigure).ToList(), FigureTransformations)); }
public static void Print(Board board) { Ensure.Arg(board, nameof(board)).IsNotNull(); WithSaveAndRestoreColors(() => { foreach (var point in Board.AllPointsOrdered) { Print(board.GetPointInfo(point)); if (point.X == Board.HIGH) { Console.WriteLine(); } } }); }
public static Figure RotateRight(Figure figure) { Ensure.Arg(figure, nameof(figure)).IsNotNull(); return(Transform(figure, RotateRightPoint)); }