Esempio n. 1
0
        public MainPageVM()
        {
            AvailableOperations = new List <AvailableOperationVM>
            {
                new AvailableOperationVM("Add +",
                                         vm => !vm.TryParseValue1(out var value1)
                        ? null
                        : new ArithmeticOperation(value1, ArithmeticOperationType.Add), () => new SingleItemView()),
                new AvailableOperationVM("Subtract -",
                                         vm => !vm.TryParseValue1(out var value1)
                        ? null
                        : new ArithmeticOperation(value1, ArithmeticOperationType.Subtract),
                                         () => new SingleItemView()),
                new AvailableOperationVM("Multiply x",
                                         vm => !vm.TryParseValue1(out var value1)
                        ? null
                        : new ArithmeticOperation(value1, ArithmeticOperationType.Multiply),
                                         () => new SingleItemView()),
                new AvailableOperationVM("Divide /",
                                         vm => !vm.TryParseValue1(out var value1)
                        ? null
                        : new ArithmeticOperation(value1, ArithmeticOperationType.Divide), () => new SingleItemView()),
                new AvailableOperationVM("Power x^n",
                                         vm => !vm.TryParseValue1(out var value1)
                        ? null
                        : new ArithmeticOperation(value1, ArithmeticOperationType.Pow), () => new SingleItemView()),
                new AvailableOperationVM("Append",
                                         vm => !vm.TryParseValue1(out var value1) ? null : new AppendOperation(value1),
                                         () => new SingleItemView()),
                new AvailableOperationVM("Replace =>", delegate(AvailableOperationVM vm)
                {
                    if (!vm.TryParseValue1(out _))
                    {
                        return(null);
                    }
                    if (!vm.TryParseValue2(out _))
                    {
                        return(null);
                    }
                    return(new ReplaceOperation(vm.Value1, vm.Value2));
                }, () => new ReplaceOperationView()),
                new AvailableOperationVM("Invert sign +/-", _ => new InvertSignOperation()),
                new AvailableOperationVM("Remove last <<", _ => new RemoveLastOperation()),
                new AvailableOperationVM("Reverse", _ => new ReverseOperation()),
                new AvailableOperationVM("Sum", _ => new SumOperation()),
                new AvailableOperationVM("< Shift", _ => new ShiftOperation(false)),
                new AvailableOperationVM("Shift >", _ => new ShiftOperation(true)),
                new AvailableOperationVM("Mirror", _ => new MirrorOperation()),
                new AvailableOperationVM("Changer [+]",
                                         vm => !vm.TryParseValue1(out var value1) ? null : new ChangerOperation(value1),
                                         () => new SingleItemView()),
                new AvailableOperationVM("Store", _ => new StoreOperation()),
                new AvailableOperationVM("Inv10", _ => new Inv10Operation()),
            };

            Operations = new ObservableCollection <OperationVM>();

            CommandAddOperation = new Command((Action) delegate
            {
                var operation = CurrentOperation?.CreateOperationFunc(CurrentOperation);
                if (operation != null)
                {
                    Operations.Add(new OperationVM(operation));
                }
            });
            CommandClear = new Command((Action) delegate
            {
                Moves   = null;
                Initial = null;
                Goal    = null;
                Operations.Clear();
                CurrentOperation = null;
                IsSolutionFound  = false;
                PortalIn         = null;
                PortalOut        = null;
            });
            CommandSolveIt = new Command((Action)async delegate
            {
                if (!int.TryParse(Moves, out var moves) || moves <= 0)
                {
                    return;
                }
                if (!int.TryParse(Goal, out var goal))
                {
                    return;
                }
                if (!int.TryParse(Initial, out var current))
                {
                    return;
                }
                if (Operations.Count == 0)
                {
                    return;
                }
                int portalIn = 0, portalOut = 0;
                if (!string.IsNullOrEmpty(PortalIn) && !int.TryParse(PortalIn, out portalIn))
                {
                    return;
                }
                if (!string.IsNullOrEmpty(PortalOut) && !int.TryParse(PortalOut, out portalOut))
                {
                    return;
                }

                Busy.IsBusy = true;
                var res     = await Task.Run(delegate
                {
                    var solver = new Solver.Solver(Operations.Select(_ => _.Operation).ToList(), goal, portalIn, portalOut);
                    var result = solver.Solve(current, moves);
                    return(result);
                });

                if (res != null)
                {
                    Operations.Clear();
                    foreach (var operation in res)
                    {
                        Operations.Add(new OperationVM(operation));
                    }

                    IsSolutionFound = true;
                }
                else
                {
                    DisplayAlert("Error", "Solution is not found!", "OK");
                }

                Busy.IsBusy = false;
            });