예제 #1
0
        private void solveGrid_Click(object sender, EventArgs e)
        {
            SudokuSolver sudokuSolver = new SudokuSolver();
            var          cellArray    = this.Controls.OfType <MyCellBox>().ToList();

            int[,] intSudokuGrid = new int[9, 9];
            for (int row = 0; row < 9; row++)
            {
                for (int column = 0; column < 9; column++)
                {
                    if (sudokuSolver.isDigit(cellArray[row * 9 + column].Text))
                    {
                        intSudokuGrid[row, column] = int.Parse(cellArray[row * 9 + column].Text);
                    }
                    //intSudokuGrid[row, column] = int.Parse(cellArray[row * 9 + column].Text);
                }
            }


            SudokuGrid mySudoku = new SudokuGrid();

            mySudoku = sudokuSolver.FromIntArray(intSudokuGrid);


            if (mySudoku.IsSolved())
            {
                infoBox.Text = "the sudoku inputted is already solved";
            }

            else if (mySudoku.IsValid())
            {
                sudokuSolver.Solve(mySudoku, true);

                for (int i = 0; i < cellArray.Count; i++)
                {
                    cellArray[i].Text = "" + mySudoku[i / 9, i % 9].ToStringVal();
                }
            }
            else
            {
                infoBox.Text = "the sudoku inputted was invalid";
            }
        }
예제 #2
0
        private void button5_Click(object sender, EventArgs e)
        {
            MyCellBox    myCellBox    = new MyCellBox(this, 100, 100);
            SudokuSolver sudokuSolver = new SudokuSolver();
            SudokuGrid   mySudoku     = myCellBox.CurrentGrid();

            if (mySudoku.IsValid())
            {
                int difficulty = sudokuSolver.RateDifficulty(mySudoku);
                infoBox.Text = $"Current sudoku is {difficulty} stars";
                if (difficulty == 6)
                {
                    infoBox.Text = "Current sudoku was solved with brute force";
                }
            }
            else
            {
                infoBox.Text = "Current Sudoku is invalid";
            }
        }
예제 #3
0
        public void CheckIsValidMethod()
        {
            //1 to 23, inclusive
            //23 broke
            for (int i = 1; i <= 23; i++)
            {
                SudokuLogic.SudokuSolver sudokuSolver = new SudokuLogic.SudokuSolver();
                SudokuLogic.sudokCell    sudokCell    = new SudokuLogic.sudokCell();
                //inputted sudoku
                int[,] sudokuInputted = input(i);

                SudokuGrid mySudoku = new SudokuGrid();

                //my sudoku to be worked with
                for (int row = 0; row < 9; row++)
                {
                    for (int column = 0; column < 9; column++)
                    {
                        mySudoku[row, column] = new sudokCell(sudokuInputted[row, column]);
                    }
                }

                Assert.IsTrue(mySudoku.IsValid(), $"{i} was said to be not valid");
            }

            SudokuGrid notValid1 = new SudokuGrid();

            //my sudoku to be worked with
            for (int row = 0; row < 9; row++)
            {
                for (int column = 0; column < 9; column++)
                {
                    notValid1[row, column] = new sudokCell();
                }
            }
            notValid1[1, 1].solve(2);
            Assert.IsFalse(notValid1.IsValid(), "IsValid said a nonValid Sudoku is validx");
        }