Ejemplo n.º 1
0
        public bool SearchSolution(SolutionState initialState)
        {
            Ensure.Arg(initialState, nameof(initialState)).IsNotNull();

            Reset();

            // Берем начальное состояние за текущее
            CurrentState = initialState;

            // Бежим по дереву состояний, пока не найдем или оно не кончится
            while (true)
            {
                // Мы пришли или вернулись на некоторое состояние,
                // надо его проверить и оповестить, а если оно конечное - то все
                if (CheckCurrentState())
                {
                    return(true);
                }

                // Если не конечное - идем по дереву, если еще есть куда
                if (!ProceedToNextState())
                {
                    return(false);
                }
            }
        }
Ejemplo n.º 2
0
        public Figure(Color colorOfOriginCell, IEnumerable <Point> otherCellPoints)
        {
            Ensure.Arg(otherCellPoints, nameof(otherCellPoints)).IsNotNullOrEmpty();

            ColorOfOriginCell = colorOfOriginCell;
            Cells             = new ReadOnlyCollection <FigureCell>(CalculateCells(colorOfOriginCell, otherCellPoints));
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        private Board(ICollection <Placement> figures)
        {
            Ensure.Arg(figures, nameof(figures)).IsNotNull();
            Figures = figures;

            PointsMap     = CalcPointsMap();
            OddCellsColor = CalcOddCellsColor();
        }
Ejemplo n.º 5
0
            public Placement(Figure figure, Point point, int num)
            {
                Ensure.Arg(figure, nameof(figure)).IsNotNull();

                Figure = figure;
                Num    = num;
                Point  = point;
            }
Ejemplo n.º 6
0
        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))));
        }
Ejemplo n.º 7
0
        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;
        }
Ejemplo n.º 8
0
        public static IList <Figure> GetTransformations(Figure figure)
        {
            Ensure.Arg(figure, nameof(figure)).IsNotNull();

            return
                (GetRotates(figure)
                 .Concat(GetRotates(FlipX(figure)))
                 .Distinct(FigureLinearTransitionComparer.Instance)
                 .ToList());
        }
Ejemplo n.º 9
0
        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 });
        }
Ejemplo n.º 10
0
        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));
        }
Ejemplo n.º 11
0
        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);
            }));
        }
Ejemplo n.º 12
0
                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);
                }
Ejemplo n.º 13
0
        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());
        }
Ejemplo n.º 14
0
        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));
        }
Ejemplo n.º 15
0
        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();
                    }
                }
            });
        }
Ejemplo n.º 16
0
        public static Figure RotateRight(Figure figure)
        {
            Ensure.Arg(figure, nameof(figure)).IsNotNull();

            return(Transform(figure, RotateRightPoint));
        }