private void MenuItemGenerateWithTechniqueFiltering_Click(object sender, RoutedEventArgs e)
#endif
        {
#if DEBUG
            await internalOperation();

            async Task internalOperation()
            {
                DisableGeneratingControls();

                Puzzle = new UndoableGrid(
                    await Task.Run(() =>
                                   new TechniqueFilteringPuzzleGenerator().Generate(
                                       new TechniqueCodeFilter {
                    TechniqueCode.AlmostLockedPair
                })));

                EnableGeneratingControls();
                SwitchOnGeneratingComboBoxesDisplaying();
                ClearItemSourcesWhenGeneratedOrSolving();
                UpdateImageGrid();
            }
#else
            Messagings.NotSupportedWhileGeneratingWithFilter();
#endif
        }
        private void MenuItemEditRecomputeCandidates_Click(object sender, RoutedEventArgs e)
        {
            int[] z = new int[81];
            for (int cell = 0; cell < 81; cell++)
            {
                z[cell] = _puzzle[cell] + 1;
            }

            var grid = SudokuGrid.CreateInstance(z);

            if (new BitwiseSolver().Solve(grid.ToString(), null, 2) == 0)
            {
                Messagings.SukakuCannotUseThisFunction();

                e.Handled = true;
                return;
            }

            _puzzle = new UndoableGrid(grid);
            _puzzle.Unfix();
            _puzzle.ClearStack();

            UpdateImageGrid();
            UpdateUndoRedoControls();
        }
        private async void MenuItemGenerateHardPattern_Click(object sender, RoutedEventArgs e)
        {
            await internalOperation();

            async Task internalOperation()
            {
                if (_database is null || Messagings.AskWhileGeneratingWithDatabase() == MessageBoxResult.Yes)
                {
                    // Disable relative database controls.
                    Settings.CurrentPuzzleDatabase = _database = null;
                    Settings.CurrentPuzzleNumber   = -1;
                    UpdateDatabaseControls(false, false, false, false);
                    _labelPuzzleNumber.ClearValue(ContentProperty);

                    DisableGeneratingControls();

                    int depth = _comboBoxBackdoorFilteringDepth.SelectedIndex;
                    Puzzle = new UndoableGrid(await Task.Run(() => new HardPatternPuzzleGenerator().Generate(depth - 1)));

                    EnableGeneratingControls();
                    SwitchOnGeneratingComboBoxesDisplaying();
                    ClearItemSourcesWhenGeneratedOrSolving();
                    UpdateImageGrid();
                }
            }
        }
        private async void MenuItemGenerateWithSymmetry_Click(object sender, RoutedEventArgs e)
        {
            await internalOperation();

            async Task internalOperation()
            {
                #region Obsolete code
                //if (_comboBoxDifficulty.SelectedIndex == 0)
                //{
                //	MessageBox.Show(
                //		"We may not allow you to generate the puzzle whose difficulty is unknown.", "Warning");
                //
                //	e.Handled = true;
                //	return;
                //}
                //
                //if (_comboBoxDifficulty.SelectedIndex >= 5
                //	&& MessageBox.Show(
                //		"You selected a difficulty that generate a puzzle will be too slow " +
                //		"and you may not cancel the operation when generating. " +
                //		"Would you like to generate anyway?", "Info", MessageBoxButton.YesNo
                //	) != MessageBoxResult.Yes)
                //{
                //	e.Handled = true;
                //	return;
                //}
                #endregion

                if (_database is null || Messagings.AskWhileGeneratingWithDatabase() == MessageBoxResult.Yes)
                {
                    // Disable relative database controls.
                    Settings.CurrentPuzzleDatabase = _database = null;
                    Settings.CurrentPuzzleNumber   = -1;
                    UpdateDatabaseControls(false, false, false, false);

                    DisableGeneratingControls();

                    // These two value should be assigned first, rather than
                    // inlining in the asynchronized environment.
                    var symmetry = ((PrimaryElementTuple <string, SymmetryType>)_comboBoxSymmetry.SelectedItem).Value2;
                    //var diff = (DifficultyLevel)_comboBoxDifficulty.SelectedItem;
                    Puzzle = new UndoableGrid(await Task.Run(() => new BasicPuzzleGenerator().Generate(33, symmetry)));

                    EnableGeneratingControls();
                    SwitchOnGeneratingComboBoxesDisplaying();
                    ClearItemSourcesWhenGeneratedOrSolving();
                    UpdateImageGrid();
                }
            }
        }
        private void MenuItemEditReset_Click(object sender, RoutedEventArgs e)
        {
            _layerCollection.Add(
                new ValueLayer(
                    _pointConverter, Settings.ValueScale, Settings.CandidateScale,
                    Settings.GivenColor, Settings.ModifiableColor, Settings.CandidateColor,
                    Settings.GivenFontName, Settings.ModifiableFontName, Settings.CandidateFontName,
                    _puzzle = new UndoableGrid(_initialPuzzle), Settings.ShowCandidates));
            _layerCollection.Remove <ViewLayer>();

            UpdateImageGrid();
            _listBoxPaths.ClearValue(ItemsControl.ItemsSourceProperty);
            _listViewSummary.ClearValue(ItemsControl.ItemsSourceProperty);
        }
        private void ListBoxPaths_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_listBoxPaths.SelectedIndex == -1)
            {
                _puzzle = new UndoableGrid(_initialPuzzle);
                UpdateImageGrid();

                e.Handled = true;
                return;
            }

            if (sender is ListBox b && b.SelectedItem is ListBoxItem listBoxItem &&
                listBoxItem.Content is PrimaryElementTuple <int, TechniqueInfo> pair)
            {
                var(n, s) = pair;
                var techniqueInfo = _analyisResult !.SolvingSteps ![n];
        private void MenuItemAnalyzeSolve_Click(object sender, RoutedEventArgs e)
        {
            if (!applyNormal() && !applySukaku())
            {
                Messagings.FailedToApplyPuzzle();
                e.Handled = true;
                return;
            }

            bool applySukaku()
            {
                var sb = new StringBuilder(SudokuGrid.EmptyString);

                if (new SukakuBitwiseSolver().Solve(
                        _puzzle.ToString($"~{(Settings.TextFormatPlaceholdersAreZero ? "0" : ".")}"), sb, 2) != 1)
                {
                    return(!(e.Handled = true));
                }

                Puzzle = new UndoableGrid(SudokuGrid.Parse(sb.ToString()));
                UpdateImageGrid();
                return(true);
            }

            bool applyNormal()
            {
                var sb = new StringBuilder(SudokuGrid.EmptyString);

                for (int cell = 0; cell < 81; cell++)
                {
                    sb[cell] += (char)(_puzzle[cell] + 1);
                }

                if (new BitwiseSolver().Solve(sb.ToString(), sb, 2) != 1)
                {
                    return(!(e.Handled = true));
                }

                Puzzle = new UndoableGrid(SudokuGrid.Parse(sb.ToString()));
                UpdateImageGrid();
                return(true);
            }
        }
        private void MenuItemEditPasteAsSukaku_Click(object sender, RoutedEventArgs e)
        {
            string puzzleStr = Clipboard.GetText();

            if (!(puzzleStr is null))
            {
                try
                {
                    Puzzle = new UndoableGrid(SudokuGrid.Parse(puzzleStr, GridParsingOption.Sukaku));

                    _menuItemEditUndo.IsEnabled = _menuItemEditRedo.IsEnabled = false;
                    UpdateImageGrid();
                }
                catch (ArgumentException)
                {
                    Messagings.FailedToPasteText();
                }

                _listBoxPaths.ClearValue(ItemsControl.ItemsSourceProperty);
                _listViewSummary.ClearValue(ItemsControl.ItemsSourceProperty);
                _listBoxTechniques.ClearValue(ItemsControl.ItemsSourceProperty);
            }
        }
        private void MenuItemEditClear_Click(object sender, RoutedEventArgs e)
        {
            Puzzle = new UndoableGrid(SudokuGrid.Empty);

            UpdateImageGrid();
        }
        private void MenuItemFileLoadPicture_Click(object sender, RoutedEventArgs e)
#endif
        {
#if SUDOKU_RECOGNIZING
            await internalOperation();

            async Task internalOperation()
            {
                if (_recognition is null)
                {
                    Messagings.FailedToLoadPicture();

                    e.Handled = true;
                    return;
                }

                var dialog = new OpenFileDialog
                {
                    AddExtension = true,
                    DefaultExt   = "png",
                    Filter       = "PNG file|*.png|JPEG file|*.jpg;*.jpeg|BMP file|*.bmp|GIF file|*.gif",
                    Multiselect  = false,
                    Title        = "Load picture from..."
                };

                if (dialog.ShowDialog() is true)
                {
                    try
                    {
                        if (_recognition.ToolIsInitialized)
                        {
                            if (Messagings.AskWhileLoadingPicture() == MessageBoxResult.Yes)
                            {
                                _textBoxInfo.Text =
                                    "Load picture successfully, now grab all digits in the picture, please wait...";
                                using (var bitmap = new Bitmap(dialog.FileName))
                                {
                                    var grid = (await Task.Run(() => _recognition.Recorgnize(bitmap))).ToMutable();
                                    grid.Fix();
                                    Puzzle = new UndoableGrid(grid);
                                }

                                UpdateUndoRedoControls();
                                UpdateImageGrid();
                            }
                        }
                        else
                        {
                            Messagings.FailedToLoadPictureDueToNotHavingInitialized();
                        }
                    }
                    catch (Exception ex)
                    {
                        Messagings.ShowExceptionMessage(ex);
                    }

                    _textBoxInfo.ClearValue(TextBox.TextProperty);
                }
            }
#else
            Messagings.NotSupportedForLoadingPicture();
#endif
        }
Example #11
0
 /// <summary>
 /// Undo the step to the grid.
 /// </summary>
 /// <param name="grid">The grid.</param>
 public abstract void UndoStepTo(UndoableGrid grid);