Esempio n. 1
0
        static void Main(string[] args)
        {
            int         row, column, index, number, maxNumberOfAttemptsToSolveSudoku = 100, numberOfAttemptsToSolveSudoku = 0;
            bool        atLeastOneSimulation = false, stateHasBeenSaved = false, numbersAddedWithCertaintyAndThenNoCandidates = false;
            SudokuBoard sudokuBoard = new SudokuBoard();
            Candidates  candidates  = new Candidates();

            string msg = sudokuBoard.Init(args);

            if (msg != null)
            {
                PrintResult(false, msg);
                return;
            }

            msg = sudokuBoard.Validate();

            if (msg != null)
            {
                PrintResult(false, msg);
                return;
            }

            if (sudokuBoard.CellsRemainToSet == 0)
            {
                PrintResult(false, "A complete sudoku was given as input. There is nothing to solve.");
                return;
            }

            candidates.Init(sudokuBoard);

            if (!candidates.HasCandidates)
            {
                PrintResult(false, "It is not possible to add any number to the sudoku.");
                return;
            }

            while (numberOfAttemptsToSolveSudoku < maxNumberOfAttemptsToSolveSudoku && !sudokuBoard.SudokuSolved && !numbersAddedWithCertaintyAndThenNoCandidates)
            {
                if (numberOfAttemptsToSolveSudoku > 0)
                {
                    sudokuBoard.RestoreState();
                    candidates.RestoreState();
                }

                while (candidates.HasCandidates)
                {
                    number = 0;
                    sudokuBoard.ResetList();

                    while (number == 0 && sudokuBoard.NextCellInList())
                    {
                        number = candidates.TryFindNumberToSetInCellWithCertainty(sudokuBoard.CurrentRow, sudokuBoard.CurrentColumn);
                    }

                    if (number == 0)
                    {
                        candidates.SimulateOneNumber(sudokuBoard, out row, out column, out index, out number);
                        atLeastOneSimulation = true;

                        if (!stateHasBeenSaved)
                        {
                            sudokuBoard.SaveState();
                            candidates.SaveState();
                            stateHasBeenSaved = true;
                        }
                    }
                    else
                    {
                        row    = sudokuBoard.CurrentRow;
                        column = sudokuBoard.CurrentColumn;
                        index  = sudokuBoard.CurrentListIndex;
                    }

                    sudokuBoard.SetNumber(row, column, index, number);
                    candidates.UpdateCandidates(row, column, number);
                }

                if (!sudokuBoard.SudokuSolved)
                {
                    if (!atLeastOneSimulation)
                    {
                        numbersAddedWithCertaintyAndThenNoCandidates = true;
                    }
                    else
                    {
                        sudokuBoard.CheckIfCanUpdateBestSoFarSudokuBoard();
                        numberOfAttemptsToSolveSudoku++;
                    }
                }
            }

            PrintResult(true, null, args, sudokuBoard);
        }
Esempio n. 2
0
        static string Run(string[] sudokuArray)
        {
            int         row, column, index, number, maxNumberOfAttemptsToSolveSudoku = 1000, numberOfAttemptsToSolveSudoku = 0;
            bool        atLeastOneSimulation = false, stateHasBeenSaved = false, numbersAddedWithCertaintyAndThenNoCandidates = false;
            SudokuBoard sudokuBoard = new SudokuBoard();
            Candidates  candidates = new Candidates();
            int         currentSudokuToSolve, numberOfSudokusToSolve = sudokuArray.Length;
            string      result = "S";

            sudokuBoard.Init();
            candidates.Init();

            currentSudokuToSolve = 1;

            while (currentSudokuToSolve <= numberOfSudokusToSolve && result == "S")
            {
                numberOfAttemptsToSolveSudoku = 0;
                atLeastOneSimulation          = false;
                stateHasBeenSaved             = false;
                numbersAddedWithCertaintyAndThenNoCandidates = false;
                sudokuBoard.SetWorkingSudokuBoard(currentSudokuToSolve - 1, sudokuArray);
                candidates.SetCandidates(sudokuBoard);

                while (numberOfAttemptsToSolveSudoku < maxNumberOfAttemptsToSolveSudoku && !sudokuBoard.SudokuSolved && !numbersAddedWithCertaintyAndThenNoCandidates)
                {
                    if (numberOfAttemptsToSolveSudoku > 0)
                    {
                        sudokuBoard.RestoreState();
                        candidates.RestoreState();
                    }

                    while (candidates.HasCandidates)
                    {
                        number = 0;
                        sudokuBoard.ResetList();

                        while (number == 0 && sudokuBoard.NextCellInList())
                        {
                            number = candidates.TryFindNumberToSetInCellWithCertainty(sudokuBoard.CurrentRow, sudokuBoard.CurrentColumn);
                        }

                        if (number == 0)
                        {
                            candidates.SimulateOneNumber(sudokuBoard, out row, out column, out index, out number);
                            atLeastOneSimulation = true;

                            if (!stateHasBeenSaved)
                            {
                                sudokuBoard.SaveState();
                                candidates.SaveState();
                                stateHasBeenSaved = true;
                            }
                        }
                        else
                        {
                            row    = sudokuBoard.CurrentRow;
                            column = sudokuBoard.CurrentColumn;
                            index  = sudokuBoard.CurrentListIndex;
                        }

                        sudokuBoard.SetNumber(row, column, index, number);
                        candidates.UpdateCandidates(row, column, number);
                    }

                    if (!sudokuBoard.SudokuSolved)
                    {
                        if (!atLeastOneSimulation)
                        {
                            numbersAddedWithCertaintyAndThenNoCandidates = true;
                        }
                        else
                        {
                            sudokuBoard.CheckIfCanUpdateBestSoFarSudokuBoard();
                            numberOfAttemptsToSolveSudoku++;
                        }
                    }
                }

                result = ProcessResult(sudokuBoard, currentSudokuToSolve - 1, sudokuArray);

                if ((currentSudokuToSolve % 500) == 0)
                {
                    Console.Write("\r" + currentSudokuToSolve.ToString());
                }

                currentSudokuToSolve++;
            }

            return(result);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            int         row, column, index, number, maxNumberOfAttemptsToSolveSudoku = 100, numberOfAttemptsToSolveSudoku = 0;
            bool        atLeastOneSimulation = false, stateHasBeenSaved = false, numbersAddedWithCertaintyAndThenNoCandidates = false;
            SudokuBoard sudokuBoard = new SudokuBoard();
            Candidates  candidates = new Candidates();
            ArrayList   debugTotalCellsAdded = new ArrayList();
            string      debugDirectory, debugString, debugFileNameFullPath;

            string[] debugCategory = new string[1];
            string[] debugInfo = new string[1];
            int      debugTry, debugAddNumber, debugSquare;

            int[][][] squareCellToRowColumnMapper;

            squareCellToRowColumnMapper = ReturnSquareCellToRowColumnMapper();

            string msg = sudokuBoard.Init(args);

            if (msg != null)
            {
                PrintResult(false, msg);
                return;
            }

            msg = sudokuBoard.Validate();

            if (msg != null)
            {
                PrintResult(false, msg);
                return;
            }

            if (sudokuBoard.CellsRemainToSet == 0)
            {
                PrintResult(false, "A complete sudoku was given as input. There is nothing to solve.");
                return;
            }

            candidates.Init(sudokuBoard);

            if (!candidates.HasCandidates)
            {
                PrintResult(false, "It is not possible to add any number to the sudoku.");
                return;
            }

            debugDirectory = DebugCreateAndReturnDebugDirectory();
            debugTry       = 0;

            while (numberOfAttemptsToSolveSudoku < maxNumberOfAttemptsToSolveSudoku && !sudokuBoard.SudokuSolved && !numbersAddedWithCertaintyAndThenNoCandidates)
            {
                debugTry      += 1;
                debugAddNumber = 0;
                debugTotalCellsAdded.Clear();

                if (numberOfAttemptsToSolveSudoku > 0)
                {
                    sudokuBoard.RestoreState();
                    candidates.RestoreState();
                }

                while (candidates.HasCandidates)
                {
                    number = 0;
                    sudokuBoard.ResetList();

                    while (number == 0 && sudokuBoard.NextCellInList())
                    {
                        number = candidates.TryFindNumberToSetInCellWithCertainty(sudokuBoard.CurrentRow, sudokuBoard.CurrentColumn, debugCategory);
                    }

                    if (number == 0)
                    {
                        candidates.SimulateOneNumber(sudokuBoard, debugInfo, out row, out column, out index, out number);
                        atLeastOneSimulation = true;

                        if (!stateHasBeenSaved)
                        {
                            sudokuBoard.SaveState();
                            candidates.SaveState();
                            stateHasBeenSaved = true;
                        }

                        debugCategory[0] = "Simulated";
                    }
                    else
                    {
                        row    = sudokuBoard.CurrentRow;
                        column = sudokuBoard.CurrentColumn;
                        index  = sudokuBoard.CurrentListIndex;
                    }

                    debugTotalCellsAdded.Add(new int[] { row, column });

                    debugSquare  = 1 + (3 * ((row - 1) / 3)) + ((column - 1) / 3);
                    debugString  = "(row, column, square, number, category) = (" + row.ToString() + ", " + column.ToString() + ", " + debugSquare.ToString() + ", " + number.ToString() + ", " + debugCategory[0] + ")\r\n\r\n";
                    debugString += "Total cells added (" + debugTotalCellsAdded.Count.ToString() + " cells): " + DebugReturnCells(debugTotalCellsAdded) + "\r\n\r\n";

                    if (debugCategory[0] == "Simulated")
                    {
                        debugString += debugInfo[0] + "\r\n\r\n";
                    }

                    debugString += "Data before update:\r\n\r\n" + DebugReturnInfo(sudokuBoard._workingSudokuBoard, sudokuBoard._cellsRemainToSet, ReturnNumberOfCandidates(candidates._candidates, true, numberOfAttemptsToSolveSudoku, debugAddNumber), candidates._candidates, squareCellToRowColumnMapper);

                    sudokuBoard.SetNumber(row, column, index, number);
                    candidates.UpdateCandidates(row, column, number);

                    debugString += "\r\nData after update:\r\n\r\n" + DebugReturnInfo(sudokuBoard._workingSudokuBoard, sudokuBoard._cellsRemainToSet, ReturnNumberOfCandidates(candidates._candidates, false, numberOfAttemptsToSolveSudoku, debugAddNumber), candidates._candidates, squareCellToRowColumnMapper);

                    debugAddNumber       += 1;
                    debugFileNameFullPath = debugDirectory + "\\" + DebugReturnFileName(debugTry, debugAddNumber);
                    File.WriteAllText(debugFileNameFullPath, debugString);
                }

                if (!sudokuBoard.SudokuSolved)
                {
                    if (!atLeastOneSimulation)
                    {
                        numbersAddedWithCertaintyAndThenNoCandidates = true;
                    }
                    else
                    {
                        sudokuBoard.CheckIfCanUpdateBestSoFarSudokuBoard();
                        numberOfAttemptsToSolveSudoku++;
                    }
                }
            }

            PrintResult(true, null, args, sudokuBoard);
        }