Ejemplo n.º 1
0
        /// <summary>
        /// Main logic to solve the sudoku puzzle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void Solve(bool step)
        {
            //(row, col, possible numbers)
            bool done   = false;
            int  safety = 0;
            int  row    = 0;
            int  col    = 0;


            Cell.SetColour(prevRow, prevCol, false);


            //Cell.checkPossibleNums(row, col);
            int curFilledCells = Board.filledCells;

            ////loop until game is done
            while (done == false && safety < 1000)
            {
                //check possible numbers of the current Cell
                for (row = 0; row < Board.kRowMax; ++row)
                {
                    for (col = 0; col < Board.kColMax; ++col)
                    {
                        if (Board.cell[row, col].GetAnswer() == 0)
                        {
                            checkPossibleNums(row, col);
                            if (Board.cell[row, col].FillCell(row, col) == true)
                            {
                                Board.filledCells++;
                            }

                            //Exit from the function when one step has been calculated
                            if (curFilledCells != Board.filledCells && step)
                            {
                                //Cell.SetColour(row, col, true);
                                prevRow = row;
                                prevCol = col;
                                done    = true;
                                break;
                            }
                        }
                    }
                    if (done == true)
                    {
                        break;
                    }
                }

                //fill in answer if its the only possible number
                if (Board.filledCells >= 81)
                {
                    done = true;
                }
                ++safety;
            }
            MessageBox.Show("Number of pass throughs: " + safety);
        }