Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        /// <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;
        }
Esempio n. 3
0
        /// <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;
            }
        }