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(); }
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(); }
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; }
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); }
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(); }
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); }
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); }
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); }
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; }
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; }
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; }
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(); }