Ejemplo n.º 1
0
        /// <summary>
        /// 플레이어가 셀이 저장하려는 값의 유효성을 검사합니다.
        /// </summary>
        private void DataGridSudoku_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            var  cell      = e.EditingElement as TextBox;
            byte cellValue = 0;

            var row = (byte)e.Row.GetIndex();
            var col = (byte)e.Column.DisplayIndex;

            if (cell.Text != string.Empty)
            {
                bool isCellValueValid = false;
                if (byte.TryParse(cell.Text, out cellValue))
                {
                    if (0 < cellValue && cellValue < 10)
                    {
                        var sudokuBoard = SudokuUtils.GenerateSudokuBoardFromGrid(currentSudokuGrid.ToArray());
                        if (sudokuSolver.IsNewCellValid(sudokuBoard, row, col, cellValue))
                        {
                            isCellValueValid = true;
                        }

                        playerActions.Push(new FillCellAction(row, col, cellValue));
                    }
                }

                if (isCellValueValid)
                {
                    if (IsUnvalidCellValueAdded)
                    {
                        IsUnvalidCellValueAdded = false;
                        UnvalidCellValueRemoved?.Invoke(sender, e);
                    }
                }
                else
                {
                    IsUnvalidCellValueAdded = true;
                    UnvalidCellValueAdded?.Invoke(sender, e);

                    e.Cancel             = true;
                    cell.BorderBrush     = Brushes.Red;
                    cell.BorderThickness = new Thickness(2);
                }
            }
            else
            {
                IsUnvalidCellValueAdded = false;
                UnvalidCellValueRemoved?.Invoke(sender, e);

                e.Cancel = true;
                currentSudokuGrid[row][col].Value = null;
                cell.BorderBrush     = Brushes.Black;
                cell.BorderThickness = new Thickness(0);
            }
        }
Ejemplo n.º 2
0
        private SudokuRow[] SolveSudoku(SudokuRow[] sudokuGrid)
        {
            var  sudokuBoard = SudokuUtils.GenerateSudokuBoardFromGrid(sudokuGrid);
            bool isSolvable  = this.sudokuSolver.SolveSudoku(sudokuBoard);

            if (isSolvable)
            {
                var solvedSudokuGrid = SudokuUtils.GenerateSudokuGridFromBoard(sudokuBoard);
                return(solvedSudokuGrid);
            }
            else
            {
                return(null);
            }
        }