Exemplo n.º 1
0
        static void Main(string[] args)
        {
            NotSolveDesc desc      = new NotSolveDesc();
            SolveDesc    solvedesc = new SolveDesc();

            desc.DescNotSolvedSudoku();
            FileHandler filehandler = new FileHandler();

            SolveSudoku solvesudoku = new SolveSudoku();

            filehandler.ConvertToInt();
            filehandler.ShowNumbers();

            solvesudoku.sudoku        = filehandler.boardintarray;
            solvesudoku.currentsudoku = solvesudoku.sudoku;
            Console.WriteLine();
            solvedesc.DescSolvedSudoku();
            if (solvesudoku.solve(0, 0))
            {
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        Console.Write(solvesudoku.currentsudoku[i, j]);
                    }
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("Sudoku can not be solved!");
            }
            filehandler.CloseFile();

            Console.ReadKey();
        }
Exemplo n.º 2
0
        private void buttonRun_Click(object sender, EventArgs e)
        {
            string      debugDirectory, errorMessage, sudoku = Utility.ReturnSudokuString(_sudokuCells);
            ArrayList   listBoxEntries;
            int         i, indexShowFirst;
            string      maxTriesString;
            int         maxNumberOfTries;
            SudokuBoard sudokuBoard;

            debugDirectory = ConfigurationManager.AppSettings["DebugDirectory"];
            if (debugDirectory == null)
            {
                MessageBox.Show("The key DebugDirectory is not given in App.config!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!Directory.Exists(debugDirectory))
            {
                MessageBox.Show("The given DebugDirectory \"" + debugDirectory + "\" does not exist!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            maxTriesString = ConfigurationManager.AppSettings["MaxNumberOfTries"];
            if (maxTriesString == null)
            {
                MessageBox.Show("The key MaxNumberOfTries is not given in App.config!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!int.TryParse(maxTriesString, out maxNumberOfTries))
            {
                MessageBox.Show("The value \"" + maxTriesString + "\" to key MaxNumberOfTries is not a valid integer!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if ((maxNumberOfTries < 1) || (maxNumberOfTries > 100))
            {
                MessageBox.Show("The value \"" + maxTriesString + "\" to key MaxNumberOfTries must be between 1 and 100!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            sudokuBoard = Utility.ReturnSudokuBoard(sudoku, out _originalData, out errorMessage);

            if (errorMessage == null)
            {
                SolveSudoku solveSudoku = new SolveSudoku(debugDirectory, maxNumberOfTries, sudokuBoard, _originalData, true);
                solveSudoku.Process(out _resultStrings, out _sudokuBoardStrings, out listBoxEntries, out indexShowFirst);

                for (i = 0; i < _resultStrings.Count; i++)
                {
                    this.listBox1.Items.Add((string)listBoxEntries[i]);
                }

                this.buttonNew.Enabled  = true;
                this.buttonRun1.Enabled = false;
                this.buttonRun2.Enabled = false;

                Utility.UpdateSudokuCells(_sudokuCells, _originalData, (string)_sudokuBoardStrings[indexShowFirst], false);

                if (sudokuBoard.SudokuIsSolved)
                {
                    MessageBox.Show("Sudoku solved!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Unable to solve sudoku!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
                this.listBox1.SelectedIndex         = indexShowFirst;
            }
            else
            {
                MessageBox.Show("The sudoku board is incorrect! Error message: " + errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.buttonNew.Focus();
        }