/// <summary> /// Sets up the game board and solution with the given puzzle. /// </summary> /// <param name="puzzle">The sudoku puzzle to be entered and solved.</param> /// <returns>The setup board along with the solution.</returns> private static GameBoardLogic FillNewPuzzle(string puzzle) { GameBoardLogic result = new GameBoardLogic(); result.EmptyBoxes = 0; List<SquareViewLogic> squares = new List<SquareViewLogic>(); foreach (char s in puzzle.ToCharArray()) { SquareViewLogic square = new SquareViewLogic(); if (s != '0' && s != '.') { square.Value = int.Parse(s.ToString()); square.IsEditable = false; } else { square.IsEditable = true; result.EmptyBoxes++; } squares.Add(square); } result.GameArray = LoadFromSquareList(squares); RuleBasedSolver solv = new RuleBasedSolver(puzzle); solv.RunStep(10000); result.solution = solv.board.ToString(); return result; }
private void TryGeneratePuzzle(int clueN) { if (clueN < 17 || clueN > 80) { throw new ArgumentException(); } ClueNumer = clueN; Random rand = new Random(); solver = new RuleBasedSolver(new Board()); solver.board.AnalyzePossibilities(); int row, col; int fill = 81; List<int> entries = new List<int>(); int whileCount = 0; int ifCount = 0; while (fill > 0) { whileCount++; row = rand.Next(0, 9); col = rand.Next(0, 9); if (solver.board.board[row, col][0] == 0) { int rPossibNumber = rand.Next(1, solver.board.board[row, col].Count); if (solver.board.SetCell(row, col, solver.board.board[row, col][rPossibNumber]) == false) { ifCount++; solver.board.board[row, col][0] = 0; int r = rand.Next(0, entries.Count); row = entries[r] / 9; col = entries[r] % 9; entries.Remove(entries[r]); solver.board.board[row, col][0] = 0; fill++; } else { entries.Add(row * 9 + col); fill--; } solver.board.AnalyzePossibilities(); } } for (int i = 0; i < (81 - clueN); i++) { row = rand.Next(0, 9); col = rand.Next(0, 9); if (solver.board.board[row, col][0] != 0) { solver.board.board[row, col][0] = 0; } else { i--; } } solver.board.AnalyzePossibilities(); }
private void GeneratePuzzle() { Random rand = new Random(); solver = new RuleBasedSolver(new Board()); solver.board.AnalyzePossibilities(); for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { if (solver.board.board[row, col][0] == 0) { int rPossibNumber = rand.Next(1, solver.board.board[row, col].Count); if (solver.board.SetCell(row, col, solver.board.board[row, col][rPossibNumber]) == false) { solver.board.board[row, col][0] = 0; solver.board.AnalyzePossibilities(); col -= 2; } } } } }
private void ConsumeSolve() { int solutions = 0; string candidate = ""; RuleBasedSolver rbs = new RuleBasedSolver(); while (solutions != 1) { Monitor.Enter(candidateList); if (candidateList.Count > 0) { candidate = String.Copy(candidateList[0]); candidateList.RemoveAt(0); Monitor.Exit(candidateList); rbs.board.SetBoard(candidate); rbs.StopOnMultipleSolutions = true; solutions = rbs.RunStep(solve_time); } else { Monitor.Exit(candidateList); Thread.Sleep(50); } } gen_puzzle = candidate; Difficulty = rbs.CalculateDifficulty(); }
/// <summary> /// Guesses the content of one cell by using a brute force algorithm and continues with standard solving. /// </summary> /// <returns>Retuns the number of valid solutions.</returns> public int Guess() { //Find square with least possibilities. int[] min = { 100, 0, 0 }; // [min , i , j ]; int solutions = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (board.board[i, j][0] == 0 && min[0] > board.board[i, j].Count) { min[0] = board.board[i, j].Count; min[1] = i; min[2] = j; } } } if (min[0] == 100) { return 1; } if (board.board[min[1], min[2]].Count == 1) { return 0; } List<Board> CorrectGuesses = new List<Board>(); for (int g_index = 1; g_index < board.board[min[1], min[2]].Count; g_index++) { int g = board.board[min[1], min[2]][g_index]; Board tmp = new Board(board); List<int> tmpList = tmp.board[min[1], min[2]]; tmpList.Clear(); tmpList.Add(g); tmp.UpdateCellPossibilities(min[1], min[2]); RuleBasedSolver solver = new RuleBasedSolver(tmp); if (StopOnMultipleSolutions) { solver.StopOnMultipleSolutions = true; } solutions = solver.RunStep(EndTime); this.SingleCount += solver.SingleCount; this.NakedCount += solver.SingleCount; this.GuessCount += solver.GuessCount; if (StopOnMultipleSolutions && solutions > 1) { return solutions; } while (solutions > 0) { CorrectGuesses.Add(solver.board); solutions--; } } if (CorrectGuesses.Count == 0) { return 0; } else { GuessCount++; board = new Board(CorrectGuesses[0]); return CorrectGuesses.Count; } }