public void TestSolvePerformance() { try { //check samurai level var samuraiSolved = SudokuSolver.GetSolver(_sudokus[Common.Difficulty.Samurai]).Solve(); Assert.IsNotNull(samuraiSolved); //check hard level var hardSolved = SudokuSolver.GetSolver(_sudokus[Common.Difficulty.Hard]).Solve(); Assert.IsNotNull(hardSolved); //check medium level var mediumSolved = SudokuSolver.GetSolver(_sudokus[Common.Difficulty.Medium]).Solve(); Assert.IsNotNull(mediumSolved); //check easy level var easySolved = SudokuSolver.GetSolver(_sudokus[Common.Difficulty.Easy]).Solve(); Assert.IsNotNull(easySolved); } catch (Exception ex) { Assert.Fail(ex.Message); } }
public void SlowPuzzle() { var testGrid = new int[9][] { new int[] { 0, 0, 0, 0, 0, 0, 0, 1, 2 }, new int[] { 0, 0, 0, 0, 3, 5, 0, 0, 0 }, new int[] { 0, 0, 0, 6, 0, 0, 0, 7, 0 }, new int[] { 7, 0, 0, 0, 0, 0, 3, 0, 0 }, new int[] { 0, 0, 0, 4, 0, 0, 8, 0, 0 }, new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 1, 2, 0, 0, 0, 0 }, new int[] { 0, 8, 0, 0, 0, 0, 0, 4, 0 }, new int[] { 0, 5, 0, 0, 0, 0, 6, 0, 0 } }; var expectedSolutionGrid = new int[9][] { new int[] { 6, 7, 3, 8, 9, 4, 5, 1, 2 }, new int[] { 9, 1, 2, 7, 3, 5, 4, 8, 6 }, new int[] { 8, 4, 5, 6, 1, 2, 9, 7, 3 }, new int[] { 7, 9, 8, 2, 6, 1, 3, 5, 4 }, new int[] { 5, 2, 6, 4, 7, 3, 8, 9, 1 }, new int[] { 1, 3, 4, 5, 8, 9, 2, 6, 7 }, new int[] { 4, 6, 9, 1, 2, 8, 7, 3, 5 }, new int[] { 2, 8, 7, 3, 5, 6, 1, 4, 9 }, new int[] { 3, 5, 1, 9, 4, 7, 6, 2, 8 } }; var solution = SudokuSolver.GetSolution(testGrid); foreach (var i in Enumerable.Range(0, Math.Max(testGrid.Length, expectedSolutionGrid.Length))) { CollectionAssert.AreEqual(expectedSolutionGrid[i], solution[i]); } }
/** * Solves current puzzle. * * @see SudokuSolver#findAllSolutions() */ private void solveFindAll() { solver = new SudokuSolver(puzzle); setSolverOptions(); int solutionsNumber = solver.findAllSolutions(); JanetConsole.println(">>>>>>>> Solutions found: " + solutionsNumber); if (solutionsNumber > 0) { List <SudokuBoard> solutions = solver.getAllSolutionsList(); for (int i = 0; i < solutionsNumber; i++) { SudokuBoard solution = solutions[i]; JanetConsole.println(">>>>> Solution nr: " + i + "/" + solutionsNumber); JanetConsole.println(">>>>> Path nr: " + solution.pathNumber); JanetConsole.println(">>>>> Computing time: " + solver.getComputingTime() + " s."); SudokuStore.consolePrintBoard(solution.board); JanetConsole.println(">>>>>"); JanetConsole.println(">>>>> Hit enter o to continue (non empty line will cancel)."); String line = JanetConsole.readLine(); if (line.Length > 0) { break; } } } else { JanetConsole.println(solver.getMessages()); } }
public void Sudoku2x2Solver() { var solver = new SudokuSolver(); var problem = new Square(3); problem.Structure[1, 3] = 7; problem.Structure[1, 5] = 1; problem.Structure[1, 8] = 2; problem.Structure[2, 2] = 9; problem.Structure[2, 7] = 4; problem.Structure[3, 0] = 8; problem.Structure[3, 1] = 1; problem.Structure[3, 5] = 7; problem.Structure[3, 8] = 4; problem.Structure[4, 3] = 2; problem.Structure[4, 4] = 4; problem.Structure[4, 6] = 3; problem.Structure[5, 0] = 4; problem.Structure[5, 5] = 5; problem.Structure[5, 6] = 9; problem.Structure[6, 1] = 7; problem.Structure[6, 3] = 3; problem.Structure[7, 0] = 5; problem.Structure[7, 8] = 8; problem.Structure[8, 1] = 8; problem.Structure[8, 4] = 2; problem.Structure[8, 5] = 9; problem.Structure[8, 7] = 6; var result = solver.Solve(problem); Console.WriteLine(result.Solved ? result.Square.ToString() : "Not solved"); }
public override bool EnforceConstraint(SudokuSolver sudokuSolver, int i, int j, int val) { if (val > 1) { int adjVal = val - 1; foreach (var pair in DiagonalCells(i, j)) { if (!sudokuSolver.ClearValue(pair.Item1, pair.Item2, adjVal)) { return(false); } } } if (val < MAX_VALUE) { int adjVal = val + 1; foreach (var pair in DiagonalCells(i, j)) { if (!sudokuSolver.ClearValue(pair.Item1, pair.Item2, adjVal)) { return(false); } } } return(true); }
public static void Main() { var board = new[, ] { { 0, 0, 0, 2, 0, 0, 3, 4, 0 }, { 0, 0, 0, 0, 8, 6, 0, 9, 2 }, { 0, 0, 0, 0, 7, 0, 0, 0, 1 }, { 1, 0, 0, 0, 0, 8, 0, 7, 0 }, { 0, 0, 8, 0, 2, 0, 1, 0, 0 }, { 0, 9, 0, 1, 0, 0, 0, 0, 8 }, { 7, 0, 0, 0, 3, 0, 0, 0, 0 }, { 5, 1, 0, 8, 4, 0, 0, 0, 0 }, { 0, 6, 4, 0, 0, 0, 0, 0, 0 }, }; var problem = new SudokuBoard(board); var sudokuSolver = new SudokuSolver(); var isSolvable = sudokuSolver.TrySolve(problem, out var solution); Console.WriteLine(isSolvable ? $"Found solution: {solution}" : $"Unfound solution for problem: {problem}"); Console.WriteLine("Press any key to exit program!"); Console.ReadKey(); }
public void GetSolvedState_Should_Throw_PuzzleIsNotYetSolvedException_If_Solve_Has_Not_Been_Called() { SudokuPuzzle sudoku = CreateValidSudokuPuzzle(); SudokuSolver sudokuSolver = new SudokuSolver(sudoku); Assert.ThrowsException <PuzzleIsNotYetSolvedException>(() => sudokuSolver.GetSolvedState()); }
//returns current sudokuGrid public SudokuGrid CurrentGrid() { var cellArray = form1.Controls.OfType <MyCellBox>().ToList(); //if invalid cell, make cell red //SudokuSolver sudokuSolver = new SudokuSolver(); //var cellArray = this.Controls.OfType<MyCellBox>().ToList(); SudokuSolver sudokuSolver = new SudokuSolver(); int[,] intSudokuGrid = new int[9, 9]; for (int row = 0; row < 9; row++) { for (int column = 0; column < 9; column++) { if (sudokuSolver.isDigit(cellArray[row * 9 + column].Text)) { intSudokuGrid[row, column] = int.Parse(cellArray[row * 9 + column].Text); } //intSudokuGrid[row, column] = int.Parse(cellArray[row * 9 + column].Text); } } SudokuGrid mySudoku = new SudokuGrid(); mySudoku = sudokuSolver.FromIntArray(intSudokuGrid); return(mySudoku); }
public void TestInvalidGrid() { var solver = new SudokuSolver(); var s = solver.solve(invalidsudoku); Assert.Null(s); }
public void SerializationCopyTest2() { // arrange var puzzle = @"100008400 020004900 903256000 600000571 410805062 532000004 000582709 001300040 008100005 ".Replace(" ", ""); var sudoku = SudokuFactory.CreateFromString(puzzle); var solver = new SudokuSolver(sudoku); // act solver.Solve(); var sudokuCopy = sudoku.DeepCopy(); sudokuCopy.SetCellValue(0, 1, 0, "manual"); sudokuCopy.UpdatePossibleValuesInAllRegions(); // assert Assert.IsTrue(sudoku.IsSolved()); Assert.IsFalse(sudokuCopy.IsSolved()); }
public void SudokuSolver_WithSolution() { // Arrange var solver = new SudokuSolver(); var initialBoard = new SudokuBoard( "...84...9", "..1.....5", "8...2146.", "7.8....9.", ".........", ".5....3.1", ".2491...7", "9.....5..", "3...84..." ); var solvedBoard = new SudokuBoard( "632845179", "471369285", "895721463", "748153692", "163492758", "259678341", "524916837", "986237514", "317584926" ); // Act var solutions = solver.Solve(initialBoard); // Assert Assert.Single(solutions); Assert.Equal(solvedBoard, solutions.First()); }
public void TestSolve() { SudokuSolver solver = new SudokuSolver(this.Sudoku); solver.Solve(); Assert.AreEqual(this.Sudoku, this.Solution); }
public void ConfigurationTest() { Assert.AreEqual(81, SudokuSolver.squares.Length, "errors in squares"); Assert.AreEqual(27, SudokuSolver._unitList.Count(), "errors in unitList"); foreach (string s in SudokuSolver.squares) { Assert.AreEqual(20, SudokuSolver._peers[s].Count(), "error in peers"); } foreach (string s in SudokuSolver.squares) { Assert.AreEqual(3, SudokuSolver._units[s].Count(), "error in units"); } SudokuSolver.CreateUnitList(); string[] p = { "A2", "B2", "D2", "E2", "F2", "G2", "H2", "I2", "C1", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "A1", "A3", "B1", "B3" }; HashSet <string> toBeChecked = new HashSet <string>(p); //SudokuSolver._peers["C2"].ToList().ForEach(Console.WriteLine); Assert.AreEqual(toBeChecked, SudokuSolver._peers["C2"], "errors in peers composition"); string grid1 = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"; string hard1 = ".....6....59.....82....8....45........3........6..3.54...325..6.................."; Assert.AreNotEqual(null, SudokuSolver.Solve(grid1), "testone"); Assert.AreNotEqual(null, SudokuSolver.Solve(hard1), "testone1"); }
static void Main(string[] args) { SudokuWriter sudokuWriter = new SudokuWriter(); ImmutableSudokuGrid filledGrid = sudokuWriter.CreateFilledGrid(); filledGrid.PrintGrid(); Console.WriteLine("\n"); ImmutableSudokuGrid emptiedGrid = sudokuWriter.EmptyGridForHardSolve(filledGrid); emptiedGrid.PrintGrid(); Console.WriteLine("\n"); SudokuGrid mutableEmptiedGrid = emptiedGrid.MakeMutableCopy(); SudokuSolver sudokuSolver = new SudokuSolver(); sudokuSolver.Solve(mutableEmptiedGrid); mutableEmptiedGrid.PrintGrid(); if (mutableEmptiedGrid.FindAllEmptySquares().Count != 0) { Console.WriteLine("\n"); HarderSudokuSolver harderSolver = new HarderSudokuSolver(); ImmutableSudokuGrid solvedGrid = harderSolver.Solve(emptiedGrid); solvedGrid.PrintGrid(); } }
private static void LeetCodeCases() { int option = 4; switch (option) { case 1: LongestPalindromeString.Tests(); break; case 2: SudokuSolver.Tests(); break; case 3: Test3DArray(); break; case 4: ValidParentheses.Tests(); break; default: break; } }
public static void Main(string[] args) { bool verbose = false; if(args.Length>0) verbose = args[0].Equals("v"); int i = 0; int[][] matrix = new int[9][]; while(i<9) { string s = Console.ReadLine(); string[] line = s.Split(' '); if(line.Length==9) { int[] intArr = new int[9]; for(int j=0;j<9;j++) { int x = int.Parse(line[j]); intArr[j]=x; } //sud.AddRow(intArr); matrix[i] = intArr; i++; } else { //invalid, please retype Console.WriteLine("invalid line, please retype"); continue; } } Console.WriteLine(); PrintMatrix(matrix); DateTime startTime = DateTime.Now; Console.WriteLine ("Started: {0}", startTime); //SudokuSolver_old solver = new SudokuSolver_old(sud); //solver.Iterate(false); SudokuSolver solver = new SudokuSolver(matrix, verbose); solver.Solve(); DateTime stopTime = DateTime.Now; Console.WriteLine ("Stopped: {0}", stopTime); Console.WriteLine ("solution generated: {0}", solver.possibleSolutions.Count); Console.WriteLine ("backtracks: {0}", solver.deadendCount); foreach(int[][] solution in solver.possibleSolutions) { PrintMatrix(solution); } Console.WriteLine(); TimeSpan elapsedTime = stopTime - startTime; Console.WriteLine ("Elapsed: {0}", elapsedTime); Console.WriteLine ("in hours :" + elapsedTime.TotalHours); Console.WriteLine ("in minutes :" + elapsedTime.TotalMinutes); Console.WriteLine ("in seconds :" + elapsedTime.TotalSeconds); Console.WriteLine ("in milliseconds:" + elapsedTime.TotalMilliseconds); }
public void TestEvents() { bool eventSignal = false; this.Sudoku.Changed += new EventHandler <SudokuEventArgs>((sender, e) => eventSignal = true); SudokuSolver.RecursiveSolve(this.Sudoku); Assert.IsTrue(eventSignal); }
public void TEST_GetSolutionThrowsArgumentOutOfRangeException() { List<Cell> cells = new List<Cell>(); cells.Add(new Cell(2, 6) { Value = "2" }); cells.Add(new Cell(7, 2) { Value = "10" }); SudokuSolver solver = new SudokuSolver(cells); solver.GetSolution(); }
private void Test(int[][] testGrid, int[][] expectedSolutionGrid) { var solution = SudokuSolver.GetSolution(testGrid); foreach (var i in Enumerable.Range(0, Math.Max(testGrid.Length, expectedSolutionGrid.Length))) { CollectionAssert.AreEqual(expectedSolutionGrid[i], solution[i]); } }
public SudokuResult PostSudoku(int index, [FromBody] SudokuSettings settings) { var csp = Csp.FromFile(settings); var sudokuSolver = new SudokuSolver(csp.Sudokus[index], settings); var result = sudokuSolver.SolveSudoku(); return(result); }
static void Solve() { var sudokuGrid = new SudokuGrid(grid); sudokuGrid.OnCellChanged += PrintCellChange; var sudokuSolver = new SudokuSolver(sudokuGrid); sudokuSolver.Solve(); }
private static void Main() { // create a sudoku to solve var sudoku = new Sudoku(); // solve the sudoku var solver = new SudokuSolver(); solver.Solve(sudoku); }
public void Solve_On_A_Solved_SudokuPuzzle_Should_Return_True() { SudokuPuzzle sudokuPuzzle = new SudokuGenerator().Generate(); using (SudokuSolver sudokuSolver = new SudokuSolver(sudokuPuzzle)) { sudokuSolver.Solve(); Assert.IsTrue(sudokuSolver.Solve()); } }
public PuzzleSolveDto Solve(SudokuPuzzle puzzle) { using (SudokuSolver sudokuSolver = new SudokuSolver(puzzle)) { if (sudokuSolver.Solve()) { return(new PuzzleSolveDto(true, new List <PuzzleState>(sudokuSolver.PuzzleStates), sudokuSolver.GetSolvedState())); } } return(new PuzzleSolveDto(false, default, default));
public void HardSudokus() { var solver = new SudokuSolver(); foreach (var s in sudokus) { var solved = solver.solve(s); Assert.NotNull(solved); Assert.True(SudokuUtils.isValidSudokuSolution(solved, s)); } }
public void SolveValidString() { string puzzle = "540072009000901000009460007050040020200000040910600785430590200000180056605200100"; ISudokuSolver solver = new SudokuSolver(puzzle, m_Validator); string actual = solver.Solve().ToString(); string expected = "546372819378951462129468537853749621267815943914623785431596278792184356685237194"; Assert.AreEqual(expected, actual); }
private void populate_Click(object sender, EventArgs e) { SudokuSolver sudokuSolver = new SudokuSolver(); SudokuGrid mySudoku = sudokuSolver.FromIntArray(sudokuSolver.input(1)); var cellArray = this.Controls.OfType <MyCellBox>().ToList(); for (int i = 0; i < cellArray.Count; i++) { cellArray[i].Text = mySudoku[i / 9, i % 9].ToStringVal(); } }
public void SolveCompleteString() { string puzzle = "546372819378951462129468537853749621267815943914623785431596278792184356685237194"; ISudokuSolver solver = new SudokuSolver(puzzle, m_Validator); string actual = solver.Solve().ToString(); string expected = "546372819378951462129468537853749621267815943914623785431596278792184356685237194"; Assert.AreEqual(expected, actual); }
public void TEST_Constructor() { List<Cell> cells = new List<Cell>(); cells.Add(new Cell(2, 6) { Value = "2" }); cells.Add(new Cell(7, 2) { Value = "7" }); SudokuSolver solver = new SudokuSolver(null); Assert.IsNotNull(solver); //TODO: Improve test for expected value //Could get solution for a valid sudoku to start with }
public void SolvesSudoku(char[][] board, char[][] expectedBoard) { SudokuSolver.SolveSudoku(board); for (int y = 0; y < expectedBoard.Length; y++) { for (int x = 0; x < expectedBoard[y].Length; x++) { board[y][x].Should().Be(expectedBoard[y][x]); } } }
public void Update(GameTime gameTime) { this.backButton.IsHighlighted = this.backButtonArea.Contains(Mouse.GetState().Position); this.solveButton.IsHighlighted = this.solveButtonArea.Contains(Mouse.GetState().Position); for (int i = 0; i < 9; i++) { if (this.digitAreas[i].Contains(Mouse.GetState().Position) && Mouse.GetState().LeftButton == ButtonState.Pressed) { for (int j = 0; j < 9; j++) { this.digitButtons[j].IsHighlighted = false; } this.currentDigit = i + 1; this.digitButtons[i].IsHighlighted = true; } } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (this.boardAreas[i, j].Contains(Mouse.GetState().Position) && Mouse.GetState().LeftButton == ButtonState.Pressed && this.currentDigit != 0) { this.matrix[i, j] = this.currentDigit; } } } if (this.backButtonArea.Contains(Mouse.GetState().Position) && Mouse.GetState().LeftButton == ButtonState.Pressed) { StateManager.CurrentState = this.callerState; } if (this.solveButtonArea.Contains(Mouse.GetState().Position) && Mouse.GetState().LeftButton == ButtonState.Pressed) { this.solver = new SudokuSolver(this.matrix); try { this.matrix = this.solver.Solve(); this.noValidSolution = false; } catch (InvalidSudokuCombinationException) { this.noValidSolution = true; } } }
public static IEnumerable <Technique> GetTechniques(SudokuSolver solver) { //var techniqueTypes = typeof( Technique ).Assembly.GetTypes().Where( type => type.IsSubclassOf( typeof( Technique ) ) && !type.IsAbstract ); //foreach( var techniqueType in techniqueTypes ) { // yield return (Technique)Activator.CreateInstance( techniqueType, solver ); //} // yield return new NakedSubsetTechnique( solver ); yield return(new TwoDirectionTechnique(solver)); yield return(new SingleOptionTechnique(solver)); }
public void SolveEmpty4x4Test() { var puzzle = @"0000 0000 0000 0000 ".Replace(" ", ""); var sudoku = SudokuFactory.CreateFromString(puzzle); var solver = new SudokuSolver(sudoku); solver.Solve(); Assert.IsTrue(sudoku.IsSolved()); }
public void Solve_2x2Invalid_ThrowsInvalidPuzzleException() { Assert.Catch<InvalidPuzzleException>(() => { var puzzle = SudokuState.Parse(@" .2|.. 2.|.. --+-- ..|.. ..|.."); var solver = new SudokuSolver(SudokuPuzzle.Puzzle2x2); solver.Solve(puzzle); }); }
public void SingleSolutionSudokuProperlySolved() { var TestSudoku = new SudokuGrid(); TestSudoku.LinearArr = new byte[81] { 5, 3, 0, 0, 7, 0, 0, 0, 0, 6, 0, 0, 1, 9, 5, 0, 0, 0, 0, 9, 8, 0, 0, 0, 0, 6, 0, 8, 0, 0, 0, 6, 0, 0, 0, 3, 4, 0, 0, 8, 0, 3, 0, 0, 1, 7, 0, 0, 0, 2, 0, 0, 0, 6, 0, 6, 0, 0, 0, 0, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9 }; var TestSolver = new SudokuSolver(); TestSolver.SetInput(TestSudoku); TestSolver.Solve(); Assert.AreEqual(TestSolver.Solutions.Count, 1); var TestSolution = TestSolver.ConvertSolution(0); byte[] ExpectedSolution = new byte[81] { 5, 3, 4, 6, 7, 8, 9, 1, 2, 6, 7, 2, 1, 9, 5, 3, 4, 8, 1, 9, 8, 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, 8, 5, 6, 9, 6, 1, 5, 3, 7, 2, 8, 4, 2, 8, 7, 4, 1, 9, 6, 3, 5, 3, 4, 5, 2, 8, 6, 1, 7, 9 }; for (int i = 0; i < TestSolution.LinearArr.Length; i++) { Assert.AreEqual(ExpectedSolution[i], TestSolution.LinearArr[i]); } }
public void TEST_GetSolution() { //Set up expected value List<Cell> expected = new List<Cell>(); for (int i = 0; i < 9; i++) { for(int j = 0; j < 9; j++) { expected.Add(new Cell(i, j) { Value = "0" }); } } List<Cell> input = new List<Cell>(); input.Add(new Cell(2, 6) { Value = "2" }); input.Add(new Cell(7, 2) { Value = "7" }); SudokuSolver solver = new SudokuSolver(input); List<Cell> result = solver.GetSolution(); Assert.Equals(expected, result); Assert.AreNotEqual(input, result); }
/** * Solves current puzzle. * * @see SudokuSolver#findAllSolutions() */ private void solveFindAll() { solver = new SudokuSolver(puzzle); setSolverOptions(); int solutionsNumber = solver.findAllSolutions(); JanetConsole.println(">>>>>>>> Solutions found: " + solutionsNumber); if (solutionsNumber > 0) { List<SudokuBoard> solutions = solver.getAllSolutionsList(); for (int i = 0; i < solutionsNumber; i++) { SudokuBoard solution = solutions[i]; JanetConsole.println(">>>>> Solution nr: " + i + "/" + solutionsNumber); JanetConsole.println(">>>>> Path nr: " + solution.pathNumber); JanetConsole.println(">>>>> Computing time: " + solver.getComputingTime() + " s."); SudokuStore.consolePrintBoard(solution.board); JanetConsole.println(">>>>>"); JanetConsole.println(">>>>> Hit enter o to continue (non empty line will cancel)."); String line = JanetConsole.readLine(); if (line.Length > 0) break; } } else { JanetConsole.println(solver.getMessages()); } }
private void Solve(SudokuPuzzle puzzle, string input, string expected) { var state = SudokuState.Parse(input); var solver = new SudokuSolver(puzzle); var sw = Stopwatch.StartNew(); var actual = solver.Solve(state); sw.Stop(); Console.WriteLine("Elapsed: {0:#,##0.#####} ms", sw.Elapsed.TotalMilliseconds); Assert.IsTrue(actual.IsSolved, "The puzzle is not solved."); Assert.AreEqual(SudokuState.Parse(expected), actual); }
/** * Test scenario implementation. * @param testId Test id. * @param threadId Thread id. * @return True is test successful, otherwise false. */ static bool runTest(int testId, int threadId) { bool testResult = true; String testDesc = "", resultDesc = ""; int[,] a = { {0,0,0,8,0,0,0,0,0}, {4,0,0,0,1,5,0,3,0}, {0,2,9,0,4,0,5,1,8}, {0,4,0,0,0,0,1,2,0}, {0,0,0,6,0,2,0,0,0}, {0,3,2,0,0,0,0,9,0}, {6,9,3,0,5,0,8,7,0}, {0,5,0,4,8,0,0,0,1}, {0,0,0,0,0,3,0,0,0} }; switch(testId) { case 0: testDesc = "SudokuSolver.setCell(int, int, int)"; { SudokuSolver s = new SudokuSolver(); s.setCell(0,0,0); s.setCell(1,0,4); s.setCell(2,0,0); s.setCell(3,0,0); s.setCell(4,0,0); s.setCell(5,0,0); s.setCell(6,0,6); s.setCell(7,0,0); s.setCell(8,0,0); s.setCell(0,1,0); s.setCell(1,1,0); s.setCell(2,1,2); s.setCell(3,1,4); s.setCell(4,1,0); s.setCell(5,1,3); s.setCell(6,1,9); s.setCell(7,1,5); s.setCell(8,1,0); s.setCell(0,2,0); s.setCell(1,2,0); s.setCell(2,2,9); s.setCell(3,2,0); s.setCell(4,2,0); s.setCell(5,2,2); s.setCell(6,2,3); s.setCell(7,2,0); s.setCell(8,2,0); s.setCell(0,3,8); s.setCell(1,3,0); s.setCell(2,3,0); s.setCell(3,3,0); s.setCell(4,3,6); s.setCell(5,3,0); s.setCell(6,3,0); s.setCell(7,3,4); s.setCell(8,3,0); s.setCell(0,4,0); s.setCell(1,4,1); s.setCell(2,4,4); s.setCell(3,4,0); s.setCell(4,4,0); s.setCell(5,4,0); s.setCell(6,4,5); s.setCell(7,4,8); s.setCell(8,4,0); s.setCell(0,5,0); s.setCell(1,5,5); s.setCell(2,5,0); s.setCell(3,5,0); s.setCell(4,5,2); s.setCell(5,5,0); s.setCell(6,5,0); s.setCell(7,5,0); s.setCell(8,5,3); s.setCell(0,6,0); s.setCell(1,6,0); s.setCell(2,6,5); s.setCell(3,6,1); s.setCell(4,6,0); s.setCell(5,6,0); s.setCell(6,6,8); s.setCell(7,6,0); s.setCell(8,6,0); s.setCell(0,7,0); s.setCell(1,7,3); s.setCell(2,7,1); s.setCell(3,7,2); s.setCell(4,7,0); s.setCell(5,7,9); s.setCell(6,7,7); s.setCell(7,7,0); s.setCell(8,7,0); s.setCell(0,8,0); s.setCell(1,8,0); s.setCell(2,8,8); s.setCell(3,8,0); s.setCell(4,8,0); s.setCell(5,8,0); s.setCell(6,8,0); s.setCell(7,8,1); s.setCell(8,8,0); int[,] b = s.getBoard(); if ( (SudokuStore.boardsAreEqual(a, b) == true) ) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; SudokuStore.consolePrintln(s.getMessages()); } } break; case 1: testDesc = "SudokuSolver.getCellDigit(int, int)"; { SudokuSolver s1 = new SudokuSolver(a); SudokuSolver s2 = new SudokuSolver(); for (int i = 0; i < SudokuBoard.BOARD_SIZE; i++) for (int j = 0; j < SudokuBoard.BOARD_SIZE; j++) { int d = s1.getCellDigit(i, j); s2.setCell(i, j, d); } int[,] b = s2.getBoard(); if ( (SudokuStore.boardsAreEqual(a, b) == true) ) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 2: testDesc = "SudokuSolver.getBoardCopy()"; { SudokuSolver s = new SudokuSolver(a); int[,] b = s.getBoard(); int[,] c = s.getBoardCopy(); if ( (SudokuStore.boardsAreEqual(b, c) == true) ) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 3: testDesc = "SudokuSolver.getSolutionBoardCells()"; { SudokuSolver s = new SudokuSolver(a); s.solve(); int[,] b = s.getSolvedBoard(); int[,] c = SudokuStore.boardCopy(a); BoardCell[] sol = s.getSolutionBoardCells(); foreach (BoardCell bc in sol) c[bc.rowIndex, bc.colIndex] = bc.digit; if ( (SudokuStore.boardsAreEqual(b, c) == true) ) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 4: testDesc = "SudokuSolver.getAllBoardCells()"; { SudokuSolver s = new SudokuSolver(a); int[,] b = s.getBoardCopy(); int[,] c = new int[SudokuBoard.BOARD_SIZE, SudokuBoard.BOARD_SIZE]; BoardCell[] bc = s.getAllBoardCells(); foreach (BoardCell cell in bc) { c[cell.rowIndex, cell.colIndex] = cell.digit; } if ( (SudokuStore.boardsAreEqual(b, c) == true) ) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 5: testDesc = "SudokuSolver.getAllSolutionsList()"; { SudokuSolver s = new SudokuSolver( SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION ); s.findAllSolutions(); List<SudokuBoard> solList; solList = s.getAllSolutionsList(); foreach (SudokuBoard sb in solList) { if (SudokuStore.checkSolvedBoard( sb.board ) == false) { testResult = false; break; } } if ( testResult == true ) { resultDesc = "Expecting each solution valid - each is valid."; } else { resultDesc = "Expecting each solution valid - found not valid."; } } break; case 6: testDesc = "SudokuGenerator -> generate -> save -> load -> compare"; { String filePath = FileX.getTmpDir() + FileX.genRndFileName(20, "txt"); SudokuGenerator g = new SudokuGenerator(SudokuGenerator.PARAM_GEN_RND_BOARD); int[,] generated = g.generate(); g.saveBoard(filePath, "generated", "saved"); int[,] loaded = SudokuStore.loadBoard(filePath); FileX.removeFile(filePath); if (SudokuStore.boardsAreEqual(generated, loaded) == false) testResult = false; if ( testResult == true ) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; } } break; case 7: testDesc = "SudokuSolver -> solve -> save -> load -> compare"; { String filePath = FileX.getTmpDir() + FileX.genRndFileName(20, "txt"); SudokuSolver s = new SudokuSolver(SudokuStore.getPuzzleExample()); s.solve(); int[,] solved = s.getSolvedBoard(); s.saveSolvedBoard(filePath, "solved", "saved"); int[,] loaded = SudokuStore.loadBoard(filePath); FileX.removeFile(filePath); if (SudokuStore.boardsAreEqual(solved, loaded) == false) testResult = false; if ( testResult == true ) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; } } break; } if (testResult == true) SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " " + testDesc + " " + resultDesc + " >>> ApiTests, result: OK"); else SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " " + testDesc + " " + resultDesc + " >>> ApiTests, result: ERROR"); return testResult; }
public void MultiSolutionSudokuProperlySolved() { var TestSolver = new SudokuSolver(); TestSolver.MaxSolutions = 1; for (int i = 0; i < TestIterations; i++) { var TestSudoku = GenerateRandomSudoku(); TestSolver.SetInput(TestSudoku); TestSolver.Solve(); var TestSolution = TestSolver.ConvertSolution(0); Assert.IsTrue(TestSolution.Valid); } }
/** * Test scenario implementation. * @param testId Test id. * @param threadId Thread id. * @return True is test successful, otherwise false. */ static bool runTest(int testId, int threadId) { SudokuSolver solverGen, solverInit; SudokuGenerator g; int solUnq; int[,] genPuzzle; int[,] solvedGen, solvedInit, initBoard; bool testResult = true; switch(testId) { case 0: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { g = new SudokuGenerator(example, SudokuGenerator.PARAM_DO_NOT_TRANSFORM); genPuzzle = g.generate(); solverGen = new SudokuSolver(genPuzzle); ErrorCodes.consolePrintlnIfError(solverGen.solve()); solverInit = new SudokuSolver(example); ErrorCodes.consolePrintlnIfError(solverInit.solve()); solUnq = solverGen.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); solvedGen = solverGen.getSolvedBoard(); solvedInit = solverInit.getSolvedBoard(); if ( (solUnq == SudokuSolver.SOLUTION_UNIQUE) && ( SudokuStore.boardsAreEqual(solvedGen, solvedInit) == true ) ) { //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example number, example: " + example + ", solution unique and correct: YES, time (g, s1, s2): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., " + solverInit.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example number, example: " + example + ", solution unique and correct: NO, time (g, s1, s2): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., " + solverInit.getComputingTime() + " s."); Console.WriteLine("Initial board"); SudokuStore.consolePrintBoard(SudokuStore.getPuzzleExample(example)); Console.WriteLine("Generated puzzle"); SudokuStore.consolePrintBoard(genPuzzle); Console.WriteLine("Solved initial board"); SudokuStore.consolePrintBoard(solvedInit); Console.WriteLine("Solved generated puzzle"); SudokuStore.consolePrintBoard(solvedGen); } } break; case 1: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { initBoard = SudokuStore.getPuzzleExample(example); g = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_TRANSFORM, SudokuGenerator.PARAM_DO_NOT_SOLVE); genPuzzle = g.generate(); solverGen = new SudokuSolver(genPuzzle); ErrorCodes.consolePrintlnIfError(solverGen.solve()); solverInit = new SudokuSolver(initBoard); ErrorCodes.consolePrintlnIfError(solverInit.solve()); solUnq = solverGen.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); solvedGen = solverGen.getSolvedBoard(); solvedInit = solverInit.getSolvedBoard(); if ( (solUnq == SudokuSolver.SOLUTION_UNIQUE) && ( SudokuStore.boardsAreEqual(solvedGen, solvedInit) == true ) ) { //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example board, example: " + example + ", solution unique and correct: YES, time (g, s1, s2): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., " + solverInit.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example board, example: " + example + ", solution unique and correct: NO, time (g, s1, s2): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., " + solverInit.getComputingTime() + " s."); Console.WriteLine("Initial board"); SudokuStore.consolePrintBoard(initBoard); Console.WriteLine("Generated puzzle"); SudokuStore.consolePrintBoard(genPuzzle); Console.WriteLine("Solved initial board"); SudokuStore.consolePrintBoard(solvedInit); Console.WriteLine("Solved generated puzzle"); SudokuStore.consolePrintBoard(solvedGen); } } break; case 2: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { g = new SudokuGenerator(example); genPuzzle = g.generate(); solverGen = new SudokuSolver(genPuzzle); ErrorCodes.consolePrintlnIfError(solverGen.solve()); solUnq = solverGen.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); solvedGen = solverGen.getSolvedBoard(); if ( (solUnq == SudokuSolver.SOLUTION_UNIQUE) && ( SudokuStore.checkPuzzle(genPuzzle) == true ) ) { //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example number, example: " + example + ", solution unique and correct: YES, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., "); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example number, example: " + example + ", solution unique and correct: NO, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., "); Console.WriteLine("Generated puzzle"); SudokuStore.consolePrintBoard(genPuzzle); Console.WriteLine("Solved generated puzzle"); SudokuStore.consolePrintBoard(solvedGen); } } break; case 3: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { initBoard = SudokuStore.getPuzzleExample(example); g = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_SOLVE); genPuzzle = g.generate(); solverGen = new SudokuSolver(genPuzzle); ErrorCodes.consolePrintlnIfError(solverGen.solve()); solUnq = solverGen.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); solvedGen = solverGen.getSolvedBoard(); if ( (solUnq == SudokuSolver.SOLUTION_UNIQUE) && ( SudokuStore.checkPuzzle(genPuzzle) == true ) ) { //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example board, example: " + example + ", solution unique and correct: YES, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., "); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by example board, example: " + example + ", solution unique and correct: NO, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., "); Console.WriteLine("Generated puzzle"); SudokuStore.consolePrintBoard(genPuzzle); Console.WriteLine("Solved initial board"); SudokuStore.consolePrintBoard(solvedGen); } } break; case 5: initBoard = SudokuPuzzles.PUZZLE_EMPTY; g = new SudokuGenerator(initBoard); genPuzzle = g.generate(); solverGen = new SudokuSolver(genPuzzle); ErrorCodes.consolePrintlnIfError(solverGen.solve()); solUnq = solverGen.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); solvedGen = solverGen.getSolvedBoard(); if ( (solUnq == SudokuSolver.SOLUTION_UNIQUE) && ( SudokuStore.checkPuzzle(genPuzzle) == true ) ) { //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: YES, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., "); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: NO, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., "); Console.WriteLine("Generated puzzle"); SudokuStore.consolePrintBoard(genPuzzle); Console.WriteLine("Solved initial board"); SudokuStore.consolePrintBoard(solvedGen); } break; case 6: initBoard = SudokuPuzzles.PUZZLE_EMPTY; g = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_SOLVE); if ( g.getGeneratorState() == SudokuGenerator.GENERATOR_INIT_FAILED ) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: YES, time (g, s): "); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: NO, time (g, s): "); SudokuStore.consolePrintln("Generator state: " + g.getGeneratorState()); Console.WriteLine(g.getMessages()); } break; case 7: initBoard = SudokuPuzzles.PUZZLE_ERROR; g = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_SOLVE); if ( g.getGeneratorState() == SudokuGenerator.GENERATOR_INIT_FAILED ) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by PUZZLE_ERROR + PARAM_DO_NOT_SOLVE, init failed: YES."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by PUZZLE_ERROR + PARAM_DO_NOT_SOLVE, init failed: NO."); SudokuStore.consolePrintln("Generator state: " + g.getGeneratorState()); Console.WriteLine(g.getMessages()); } break; case 8: initBoard = SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION; g = new SudokuGenerator(initBoard); genPuzzle = g.generate(); solverGen = new SudokuSolver(genPuzzle); ErrorCodes.consolePrintlnIfError(solverGen.solve()); solUnq = solverGen.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); solvedGen = solverGen.getSolvedBoard(); if ( (solUnq == SudokuSolver.SOLUTION_UNIQUE) && ( SudokuStore.checkPuzzle(genPuzzle) == true ) ) { //SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: YES, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., "); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by empty board, solution unique and correct: NO, time (g, s): " + g.getComputingTime() + " s., " + solverGen.getComputingTime() + " s., "); Console.WriteLine("Generated puzzle"); SudokuStore.consolePrintBoard(genPuzzle); Console.WriteLine("Solved initial board"); SudokuStore.consolePrintBoard(solvedGen); } break; case 9: initBoard = SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION; g = new SudokuGenerator(initBoard, SudokuGenerator.PARAM_DO_NOT_SOLVE); if ( g.getGeneratorState() == SudokuGenerator.GENERATOR_INIT_FAILED ) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by PUZZLE_NON_UNIQUE_SOLUTION + PARAM_DO_NOT_SOLVE, init failed: YES."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", generate by PUZZLE_NON_UNIQUE_SOLUTION + PARAM_DO_NOT_SOLVE, init failed: NO."); SudokuStore.consolePrintln("Generator state: " + g.getGeneratorState()); Console.WriteLine(g.getMessages()); } break; } if (testResult == true) SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuGenerator, result: OK"); else SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuGenerator, result: ERROR"); return testResult; }
/** * Start the Janet-Sudoku Tutorial code. * @param args No arguments are considered. */ public static void Start() { String tmpDir = FileX.getTmpDir(); { /* * Simple sudoku generation. */ SudokuStore.consolePrintln(""); SudokuStore.consolePrintln("Simple sudoku generation."); SudokuGenerator sg = new SudokuGenerator(); int[,] puzzle = sg.generate(); SudokuStore.consolePrintBoard(puzzle); } { /* * Simple sudoku generation + parameters. */ SudokuStore.consolePrintln(""); SudokuStore.consolePrintln("Simple sudoku generation + parameters."); SudokuGenerator sg = new SudokuGenerator(SudokuGenerator.PARAM_GEN_RND_BOARD); int[,] puzzle = sg.generate(); SudokuStore.consolePrintBoard(puzzle); } { /* * Simple sudoku generation + puzzle rating. */ SudokuStore.consolePrintln(""); SudokuStore.consolePrintln("Simple sudoku generation + puzzle rating."); SudokuGenerator sg = new SudokuGenerator(); int[,] puzzle = sg.generate(); int rating = SudokuStore.calculatePuzzleRating(puzzle); SudokuStore.consolePrintBoard(puzzle); SudokuStore.consolePrintln("Puzzle rating: " + rating); } { /* * Solving sudoku example. */ SudokuStore.consolePrintln(""); SudokuStore.consolePrintln("Solving sudoku example."); SudokuSolver ss = new SudokuSolver(SudokuPuzzles.PUZZLE_EXAMPLE_001); SudokuStore.consolePrintBoard(ss.getBoard()); ss.solve(); SudokuStore.consolePrintBoard(ss.getSolvedBoard()); } { /* * Saving board examples */ SudokuStore.consolePrintln(""); SudokuStore.consolePrintln("Saving board examples " + tmpDir); SudokuStore.saveBoard(SudokuPuzzles.PUZZLE_EXAMPLE_001, tmpDir + "sudoku-board-ex-1.txt"); SudokuStore.saveBoard(SudokuPuzzles.PUZZLE_EXAMPLE_001, tmpDir + "sudoku-board-ex-2.txt", "This is a head comment"); SudokuStore.saveBoard(SudokuPuzzles.PUZZLE_EXAMPLE_001, tmpDir + "sudoku-board-ex-3.txt", "This is a head comment", "And a tail comment"); SudokuSolver ss = new SudokuSolver(1); ss.solve(); ss.saveSolvedBoard(tmpDir + "sudoku-board-ex-sol.txt", "Solution for the PUZZLE_EXAMPLE_001"); } { /* * Puzzle modification - i.e. rotation */ SudokuStore.consolePrintln("----------------------------------------------------"); SudokuStore.consolePrintln(">>>>>>>> Puzzle modification"); int[,] puzzle = SudokuStore.getPuzzleExample(0); int[,] puzzleRotated = SudokuStore.rotateClockWise(puzzle); SudokuStore.consolePrintBoard(puzzle); SudokuStore.consolePrintBoard(puzzleRotated); } /* * And many other staff provided by the library :-) */ }
/** * Solves current puzzle. * * @see SudokuSolver#solve() * @see SudokuSolver#getSolvedBoard() * @see SudokuSolver#getSolvingState() */ private void solveFindFirst() { solver = new SudokuSolver(puzzle); setSolverOptions(); solver.solve(); if (solver.getSolvingState() == SudokuSolver.SOLVING_STATE_SOLVED) { trackPuzzleUndo(); puzzle = solver.getSolvedBoard(); JanetConsole.println("Path leading to the solution:"); JanetConsole.println(solver.solutionPathToString()); JanetConsole.println(">>>>> Computing time: " + solver.getComputingTime() + " s."); JanetConsole.println(">>>>> Closed routes: " + solver.getClosedRoutesNumber() + " s."); } else { JanetConsole.println(solver.getMessages()); } }
/** * Test scenario implementation. * @param testId Test id. * @param threadId Thread id. * @return True is test successful, otherwise false. */ static bool runTest(int testId, int threadId) { SudokuSolver s; int sol; int solNum; int solUnq; int solvingState; int boardState; int[,] solution; int[,] puzzle; bool solCorr; bool testResult = true; switch(testId) { case 0: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { s = new SudokuSolver(example); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum != 1) testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", findAllSolutions, example: " + example + ", solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); } break; case 1: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { s = new SudokuSolver(example); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq != SudokuSolver.SOLUTION_UNIQUE) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: " + example + ", is solution unique: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: " + example + ", is solution unique: YES, time: " + s.getComputingTime() + " s."); } } break; case 2: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { s = new SudokuSolver(example); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); solution = s.getSolvedBoard(); solCorr = SudokuStore.checkSolvedBoard(solution); if (solCorr != true) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: " + example + ", is solution correct: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: " + example + ", is solution correct: YES, time: " + s.getComputingTime() + " s."); } } break; case 3: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { puzzle = SudokuStore.seqOfRandomBoardTransf( SudokuStore.getPuzzleExample(example) ); s = new SudokuSolver(puzzle); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum != 1) testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + findAllSolutions, example: " + example + ", solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); } break; case 4: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { puzzle = SudokuStore.seqOfRandomBoardTransf( SudokuStore.getPuzzleExample(example) ); s = new SudokuSolver(puzzle); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq != SudokuSolver.SOLUTION_UNIQUE) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: " + example + ", is solution unique: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: " + example + ", is solution unique: YES, time: " + s.getComputingTime() + " s."); } } break; case 5: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { puzzle = SudokuStore.seqOfRandomBoardTransf( SudokuStore.getPuzzleExample(example) ); s = new SudokuSolver(puzzle); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); solution = s.getSolvedBoard(); solCorr = SudokuStore.checkSolvedBoard(solution); if (solCorr != true) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: " + example + ", is solution correct: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: " + example + ", is solution correct: YES, time: " + s.getComputingTime() + " s."); } } break; case 6: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum <= 1) testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", findAllSolutions, example: non unique, solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); break; case 7: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq != SudokuSolver.SOLUTION_NON_UNIQUE) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: non unique, is solution unique: YES, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: non unique, is solution unique: NO, time: " + s.getComputingTime() + " s."); } break; case 8: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); solution = s.getSolvedBoard(); solCorr = SudokuStore.checkSolvedBoard(solution); if (solCorr != true) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: non unique, is solution correct: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: non unique, is solution correct: YES, time: " + s.getComputingTime() + " s."); } break; case 9: puzzle = SudokuStore.seqOfRandomBoardTransf( SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION ); s = new SudokuSolver(puzzle); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum <= 1) testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + findAllSolutions, example: non unique, solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); break; case 10: puzzle = SudokuStore.seqOfRandomBoardTransf( SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION ); s = new SudokuSolver(puzzle); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq != SudokuSolver.SOLUTION_NON_UNIQUE) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: non unique, is solution unique: YES, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: non unique, is solution unique: NO, time: " + s.getComputingTime() + " s."); } break; case 11: puzzle = SudokuStore.seqOfRandomBoardTransf( SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION ); s = new SudokuSolver(puzzle); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); solution = s.getSolvedBoard(); solCorr = SudokuStore.checkSolvedBoard(solution); if (solCorr != true) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: non unique, is solution correct: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: non unique, is solution correct: YES, time: " + s.getComputingTime() + " s."); } break; case 12: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NO_SOLUTION); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum != 0) testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", findAllSolutions, example: no solution, solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); break; case 13: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NO_SOLUTION); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq == SudokuSolver.SOLUTION_NOT_EXISTS) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: no solution, no solutions found: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: no solution, no solutions found: NO, time: " + s.getComputingTime() + " s."); } break; case 14: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NO_SOLUTION); sol = s.solve(); solution = s.getSolvedBoard(); if (s.getSolvingState() == ErrorCodes.SUDOKUSOLVER_SOLVE_SOLVING_FAILED) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: no solution, solving failed: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: no solution, solving failed: NO, time: " + s.getComputingTime() + " s."); } break; case 15: s = new SudokuSolver(SudokuPuzzles.PUZZLE_ERROR); solvingState = s.solve(); boardState = s.getBoardState(); if ( (boardState == SudokuBoard.BOARD_STATE_ERROR) && (solvingState == ErrorCodes.SUDOKUSOLVER_SOLVE_SOLVING_NOT_STARTED) ) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: board with error, board state error and solving not started: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: board with error, board state error and solving not started: NO, time: " + s.getComputingTime() + " s."); } break; case 16: s = new SudokuSolver(SudokuPuzzles.PUZZLE_REGTESTS); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); if ( SudokuStore.boardsAreEqual(s.getSolvedBoard(), SudokuPuzzles.PUZZLE_REGTESTS_SOLUTION) ) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: with solution, solutions are equal: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: with solution, solutions are equal: NO, time: " + s.getComputingTime() + " s."); } break; case 17: s = new SudokuSolver(SudokuPuzzles.PUZZLE_EMPTY); sol = s.solve(); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(sol); ErrorCodes.consolePrintlnIfError(solUnq); if ( ( SudokuStore.checkSolvedBoard(s.getSolvedBoard()) == true ) & (solUnq == SudokuSolver.SOLUTION_NON_UNIQUE) ) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: empty puzzle, found solution and solution non unique: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: empty puzzle, found solution and solution non unique: NO, time: " + s.getComputingTime() + " s."); } break; } if (testResult == true) SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuSolver, result: OK"); else SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuSolver, result: ERROR"); return testResult; }
/** * Verifies solution existence * * @see SudokuSolver#checkIfUniqueSolution() */ private void evaluateSolutions() { solver = new SudokuSolver(puzzle); int solutionsInfo = solver.checkIfUniqueSolution(); JanetConsole.println(">>>"); if (solutionsInfo == SudokuSolver.SOLUTION_UNIQUE) JanetConsole.println(">>> Solution exists and is unique!"); else if (solutionsInfo == SudokuSolver.SOLUTION_NON_UNIQUE) JanetConsole.println(">>> Solution exists but is non-unique!"); else if (solutionsInfo == SudokuSolver.SOLUTION_NOT_EXISTS) JanetConsole.println(">>> Solution does not exists."); else JanetConsole.println(solver.getMessages()); JanetConsole.println(">>> Computing time: " + solver.getComputingTime() + " s."); }