示例#1
0
 public void PrintSudoku(Sudoku_Model inputSudoku)
 {
     for (int i = 8; i >= 0; i--)
     {
         for (int j = 0; j < 9; j++)
         {
             Console.Write(inputSudoku.Sudoku[i, j].ToString() + " ");
         }
         Console.WriteLine();
     }
     Console.WriteLine();
 }
示例#2
0
        static void Main(string[] args)
        {
            Sudoku_Model sudoku     = new Sudoku_Model();
            Controller   controller = new Controller();

            //int[,] inputSudokuArray = new int[9, 9]
            //{
            //    {4, 5, 9, 0, 0, 0, 1, 7, 8},
            //    {6, 7, 0, 0, 9, 0, 0, 0, 0},
            //    {0, 0, 0, 7, 0, 4, 9, 0, 0},
            //    {9, 0, 3, 4, 0, 0, 0, 8, 0},
            //    {0, 8, 0, 0, 0, 6, 7, 0, 1},
            //    {0, 0, 7, 5, 2, 8, 6, 0, 0},
            //    {0, 0, 6, 8, 0, 2, 0, 0, 0},
            //    {7, 0, 4, 0, 0, 0, 8, 9, 3},
            //    {0, 0, 0, 0, 0, 0, 0, 0, 7}
            //};

            int[,] inputSudokuArray = new int[9, 9]
            {
                { 0, 5, 0, 0, 7, 1, 0, 8, 0 },
                { 0, 7, 0, 0, 0, 0, 0, 2, 0 },
                { 0, 0, 9, 8, 0, 5, 4, 0, 0 },
                { 0, 0, 7, 6, 0, 0, 0, 0, 4 },
                { 9, 0, 0, 0, 0, 4, 3, 0, 0 },
                { 0, 0, 3, 0, 0, 0, 8, 0, 0 },
                { 0, 0, 2, 5, 0, 8, 6, 0, 0 },
                { 0, 1, 0, 0, 9, 7, 0, 5, 0 },
                { 0, 3, 0, 0, 0, 0, 0, 4, 0 }
            };


            sudoku.Sudoku = inputSudokuArray;

            //controller.PrintSudoku(sudoku);
            Console.WriteLine();

            if ((controller.SolveSudokuComplete(sudoku)))
            {
                Console.WriteLine("Complete!");
                Console.WriteLine();
                controller.PrintSudoku(controller.FinalSudoku());
            }
            else
            {
                Console.WriteLine("Cannot be resolved!");
            }


            Console.ReadLine();
        }
示例#3
0
 public int GetSquareWithLeastCandidates(Sudoku_Model inputSudoku)
 {
     int minPossibilities = 9;
     int squareWithMinPossibilites = 0;
     foreach (KeyValuePair<int, List<int>> kvp in inputSudoku.Candidates)
     {
         if (minPossibilities > kvp.Value.Count)
         {
             minPossibilities = kvp.Value.Count;
             squareWithMinPossibilites = kvp.Key;
         }
     }
     return squareWithMinPossibilites;
 }
示例#4
0
 public bool IsSudokuComplete(Sudoku_Model inputSudoku)
 {
     for (int i = 0; i < 9; i++)
     {
         for (int j = 0; j < 9; j++)
         {
             if (inputSudoku.Sudoku[i, j] == 0)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
示例#5
0
        static void Main(string[] args)
        {
            Sudoku_Model sudoku = new Sudoku_Model();
            Controller controller = new Controller();

            //int[,] inputSudokuArray = new int[9, 9]
            //{
            //    {4, 5, 9, 0, 0, 0, 1, 7, 8},
            //    {6, 7, 0, 0, 9, 0, 0, 0, 0},
            //    {0, 0, 0, 7, 0, 4, 9, 0, 0},
            //    {9, 0, 3, 4, 0, 0, 0, 8, 0},
            //    {0, 8, 0, 0, 0, 6, 7, 0, 1},
            //    {0, 0, 7, 5, 2, 8, 6, 0, 0},
            //    {0, 0, 6, 8, 0, 2, 0, 0, 0},
            //    {7, 0, 4, 0, 0, 0, 8, 9, 3},
            //    {0, 0, 0, 0, 0, 0, 0, 0, 7}
            //};

            int[,] inputSudokuArray = new int[9, 9]
            {
                {0, 5, 0, 0, 7, 1, 0, 8, 0},
                {0, 7, 0, 0, 0, 0, 0, 2, 0},
                {0, 0, 9, 8, 0, 5, 4, 0, 0},
                {0, 0, 7, 6, 0, 0, 0, 0, 4},
                {9, 0, 0, 0, 0, 4, 3, 0, 0},
                {0, 0, 3, 0, 0, 0, 8, 0, 0},
                {0, 0, 2, 5, 0, 8, 6, 0, 0},
                {0, 1, 0, 0, 9, 7, 0, 5, 0},
                {0, 3, 0, 0, 0, 0, 0, 4, 0}
            };

            sudoku.Sudoku = inputSudokuArray;

            //controller.PrintSudoku(sudoku);
            Console.WriteLine();

            if ((controller.SolveSudokuComplete(sudoku)))
            {
                Console.WriteLine("Complete!");
                Console.WriteLine();
                controller.PrintSudoku(controller.FinalSudoku());
            }
            else
            {
                Console.WriteLine("Cannot be resolved!");
            }

            Console.ReadLine();
        }
示例#6
0
        public int GetSquareWithLeastCandidates(Sudoku_Model inputSudoku)
        {
            int minPossibilities          = 9;
            int squareWithMinPossibilites = 0;

            foreach (KeyValuePair <int, List <int> > kvp in inputSudoku.Candidates)
            {
                if (minPossibilities > kvp.Value.Count)
                {
                    minPossibilities          = kvp.Value.Count;
                    squareWithMinPossibilites = kvp.Key;
                }
            }
            return(squareWithMinPossibilites);
        }
示例#7
0
        public bool SolveSudokuBasic(Sudoku_Model inputSudoku)
        {
            bool stillChanging = true;

            while (stillChanging)
            {
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (inputSudoku.Sudoku[i, j] != 0)
                        {
                            continue;
                        }
                        List <int> tmpCandidates = new List <int>();
                        tmpCandidates = GetNumbersAllowed(i, j, inputSudoku.Sudoku);
                        if (tmpCandidates.Count == 1)
                        {
                            inputSudoku.Sudoku[i, j] = tmpCandidates[0];
                            stillChanging            = true;
                        }
                        else if (tmpCandidates.Count > 1)
                        {
                            if (!inputSudoku.Candidates.ContainsKey(i * 10 + j))
                            {
                                inputSudoku.Candidates.Add(i * 10 + j, tmpCandidates);
                            }

                            stillChanging = false;
                        }
                        else
                        {
                            //this.PrintSudoku(inputSudoku);
                            return(false);
                        }
                    }
                }

                if (IsSudokuComplete(inputSudoku))
                {
                    return(true);
                }
            }
            return(true);
        }
示例#8
0
        public bool SolveSudokuComplete(Sudoku_Model inputSudoku)
        {
            if (SolveSudokuBasic(inputSudoku))
            {
                PrintSudoku(inputSudoku);
                if (IsSudokuComplete(inputSudoku))
                {
                    SudokuStack.Push(inputSudoku);
                    return(true);
                }
                else
                {
                    int squareWithMinPossibilites = 0;
                    squareWithMinPossibilites = GetSquareWithLeastCandidates(inputSudoku);
                    foreach (int element in inputSudoku.Candidates[squareWithMinPossibilites])
                    {
                        Sudoku_Model guessSudoku = new Sudoku_Model();
                        Array.Copy(inputSudoku.Sudoku, guessSudoku.Sudoku, inputSudoku.Sudoku.Length);
                        guessSudoku.Sudoku[squareWithMinPossibilites / 10, squareWithMinPossibilites % 10] = element;
                        SudokuStack.Push(guessSudoku);
                        Console.WriteLine("Trying: " + squareWithMinPossibilites.ToString() + " with " + element.ToString());
                        if (SolveSudokuComplete(guessSudoku))
                        {
                            return(true);
                        }
                        else
                        {
                            SudokuStack.Pop();
                            continue;
                        }
                    }
                }
            }

            return(false);
        }
示例#9
0
 public bool IsSudokuComplete(Sudoku_Model inputSudoku)
 {
     for (int i = 0; i < 9; i++)
     {
         for (int j = 0; j < 9; j++)
         {
             if (inputSudoku.Sudoku[i, j] == 0)
             {
                 return false;
             }
         }
     }
     return true;
 }
示例#10
0
        public bool SolveSudokuComplete(Sudoku_Model inputSudoku)
        {
            if (SolveSudokuBasic(inputSudoku))
            {
                PrintSudoku(inputSudoku);
                if (IsSudokuComplete(inputSudoku))
                {
                    SudokuStack.Push(inputSudoku);
                    return true;
                }
                else
                {
                    int squareWithMinPossibilites = 0;
                    squareWithMinPossibilites = GetSquareWithLeastCandidates(inputSudoku);
                    foreach (int element in inputSudoku.Candidates[squareWithMinPossibilites])
                    {
                        Sudoku_Model guessSudoku = new Sudoku_Model();
                        Array.Copy(inputSudoku.Sudoku, guessSudoku.Sudoku, inputSudoku.Sudoku.Length);
                        guessSudoku.Sudoku[squareWithMinPossibilites / 10, squareWithMinPossibilites % 10] = element;
                        SudokuStack.Push(guessSudoku);
                        Console.WriteLine("Trying: " + squareWithMinPossibilites.ToString() + " with " + element.ToString());
                        if (SolveSudokuComplete(guessSudoku))
                        {
                            return true;
                        }
                        else
                        {
                            SudokuStack.Pop();
                            continue;
                        }
                    }

                }
            }

            return false;
        }
示例#11
0
        public bool SolveSudokuBasic(Sudoku_Model inputSudoku)
        {
            bool stillChanging = true;

            while (stillChanging)
            {
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (inputSudoku.Sudoku[i, j] != 0)
                        {
                            continue;
                        }
                        List<int> tmpCandidates = new List<int>();
                        tmpCandidates = GetNumbersAllowed(i, j, inputSudoku.Sudoku);
                        if (tmpCandidates.Count == 1)
                        {
                            inputSudoku.Sudoku[i, j] = tmpCandidates[0];
                            stillChanging = true;
                        }
                        else if (tmpCandidates.Count > 1)
                        {
                            if (!inputSudoku.Candidates.ContainsKey(i * 10 + j))
                            {
                                inputSudoku.Candidates.Add(i * 10 + j, tmpCandidates);
                            }

                            stillChanging = false;
                        }
                        else
                        {
                            //this.PrintSudoku(inputSudoku);
                            return false;
                        }

                    }
                }

                if (IsSudokuComplete(inputSudoku))
                {
                    return true;
                }
            }
            return true;
        }
示例#12
0
 public void PrintSudoku(Sudoku_Model inputSudoku)
 {
     for (int i = 8; i >= 0; i--)
     {
         for (int j = 0; j < 9; j++)
         {
             Console.Write(inputSudoku.Sudoku[i, j].ToString() + " ");
         }
         Console.WriteLine();
     }
     Console.WriteLine();
 }