public static string Legallity(int index, int value) //Checks whether the value is correct and if so implements { if (Unsolved.initialBoard[index] != 0) //Ensures a starting number isnt selected as they cant be changed { if (Unsolved.zeroes.Contains(index)) //checks if cell is empty { if (Logic.PossList(index, Unsolved).Contains(value)) //checks if there is a clash between a number in a row, column, or square, and the selected value { if (value == Logic.Solved.board[index]) //checks if value is correct { Unsolved.Reconstruct(index, value); Unsolved.PrintBoard(); return("That value is correct"); } else { return("" + value + " is not the correct value."); } } else { return("There is a " + value + " in the same square, column, or row."); } } else { return("That cell int empty"); } } else { return("Original numbers cant be changed."); } }
public static void Main(string[] args) { Console.WriteLine("Enter a sudoku board line by line with comma seperated numbers, and a zero for an empty square: "); string line = Console.ReadLine();//collects user input int[] input = line.Split(',').Select(n => Convert.ToInt32(n)).ToArray(); Solved.Fill(input); Game.Unsolved.Fill(input); Solved.PrintBoard(); while (Solved.zeroes.Count() != 0) { int initial = Solved.zeroes.Count(); for (int i = 0; i < Solved.zeroes.Count(); i++) { int boardIndex = Solved.zeroes.ElementAt(i); List <int> currPoss = PossList(boardIndex, Solved); if (currPoss.Count() == 1 && Solved.zeroes.Contains(boardIndex))//if there is only one possible number for that cell, enter it { int num = currPoss.ElementAt(0); Solved.Reconstruct(boardIndex, num); } else { /*return from zero list matching row, column, and square find unique possibility, implrement, repeat for squ and col * In other words, if a row needs 3 numbers, and one of those numbers only has one place it could go, * regardless of whether you record it as the only possible option or not, it must be the solution and is entered */ int row = boardIndex / 9; int col = boardIndex % 9; int squ = (3 * (row / 3)) + (col / 3); int[] rowIndx = new int[9]; int[] colIndx = new int[9]; int[] squIndx = new int[9]; for (int a = 0; a < 9; a++)//populates the index lists for the current row and column { rowIndx[a] = (row * 9) + a; colIndx[a] = col + (9 * a); } for (int c = 0; c < 3; c++)//populates the index lists for the current square { for (int d = 0; d < 3; d++) { squIndx[(c * 3) + (d)] = (((squ - squ % 3) + c) * 9) + ((squ % 3) * 3) + d; } } List <int> zRow = new List <int>();//lists that will hold the indexes of all the empty or 'zero' values in the row column or square List <int> zCol = new List <int>(); List <int> zSqu = new List <int>(); for (int iter = 0; iter < 9; iter++)//populates zero lists { int r1 = rowIndx[iter]; int c = colIndx[iter]; int s = squIndx[iter]; if (Solved.zeroes.Contains(r1)) { zRow.Add(r1); } if (Solved.zeroes.Contains(c)) { zCol.Add(c); } if (Solved.zeroes.Contains(s)) { zSqu.Add(s); } } IndexLogic(zRow);//applies aforementioned logic through the IndexLogic function to the applicable row, column, and square IndexLogic(zCol); IndexLogic(zSqu); } } if (initial == Solved.zeroes.Count()) { Console.WriteLine("true"); break; } } Console.WriteLine("Do you wish to solve the puzzle (y/n)?: ");//sets up to pass control over to Game to run the play if (Console.ReadLine() == "y") { Game.Play(); } else { Solved.PrintBoard(); } Console.Read(); }