예제 #1
0
        public void SetUp()
        {
            ISudokuPuzzleStringImporter importer = new SudokuPuzzleImporter();
            puzzle = importer.Import(INPUT_PUZZLE);

            //strategy = new BruteForceSudokuStrategy();
            strategy = new SimpleBruteForceSudokuStrategy();
            //strategy = new SimpleBruteForceSudokuStrategy2();
            //strategy = new CycleFirstBruteForceSudokuStrategy();
        }
예제 #2
0
    private XmlNode CreateEmptySolutionPuzzleNode()
    {
        inputPuzzle = new SudokuPuzzle(PUZZLE_SIZE);
        ISudokuPuzzleXmlExporter exporter = new SudokuPuzzleExporter();
        XmlNode puzzleNode = exporter.Export(inputPuzzle);
        XmlElement inputPuzzleNode = puzzleNode.OwnerDocument.CreateElement("SolutionPuzzle");
        inputPuzzleNode.AppendChild(puzzleNode);

        return inputPuzzleNode;
    }
예제 #3
0
    private XmlNode CreateParsedInputPuzzleNode()
    {
        inputPuzzle = ParseInputPuzzleFromForm();

        ISudokuPuzzleXmlExporter exporter = new SudokuPuzzleExporter();
        XmlNode puzzleNode = exporter.Export(inputPuzzle);
        XmlElement inputPuzzleNode = puzzleNode.OwnerDocument.CreateElement("InputPuzzle");
        inputPuzzleNode.AppendChild(puzzleNode);

        return inputPuzzleNode;
    }
예제 #4
0
    private XmlNode CreateInputPuzzleNode()
    {
        ISudokuPuzzleStringImporter si = new SudokuPuzzleImporter();
        inputPuzzle = si.Import("0,0,0,0,5,1,2,0,0\n2,0,1,0,9,0,0,8,5\n0,9,4,0,0,2,0,0,7\n6,0,0,0,2,0,7,3,0\n9,0,0,0,8,0,0,0,1\n0,3,8,0,7,0,0,0,6\n7,0,0,2,0,0,8,5,0\n3,8,0,0,4,0,1,0,9\n0,0,6,9,3,0,0,0,0");

        //ISudokuPuzzle inputPuzzle = new SudokuPuzzle(PUZZLE_SIZE);
        ISudokuPuzzleXmlExporter exporter = new SudokuPuzzleExporter();
        XmlNode puzzleNode = exporter.Export(inputPuzzle);
        XmlElement inputPuzzleNode = puzzleNode.OwnerDocument.CreateElement("InputPuzzle");
        inputPuzzleNode.AppendChild(puzzleNode);

        return inputPuzzleNode;
    }
예제 #5
0
        private void InitializeRegion(ISudokuPuzzle puzzle, ISudokuPosition position)
        {
            ISudokuPosition currentPosition = new SudokuPosition();
            board = new SimpleSudokuBoard(puzzle.Size);

            for (int i = 0; i < puzzle.Size; i++)
            {
                for (int j = 0; j < puzzle.Size; j++)
                {
                    currentPosition.RowNumber = puzzle.Size * position.RowNumber + i;
                    currentPosition.ColumnNumber = puzzle.Size * position.ColumnNumber + j;
                    board.SetValue(i, j, puzzle.GetValue(currentPosition));
                }
            }
        }
        private void ParsePuzzle(string source, ISudokuPuzzle puzzle)
        {
            ISudokuPosition currentPosition = new SudokuPosition();

            for (int i = 0; i < source.Length; i++)
            {
                if (source[i] == DELIMITER_COL)
                {
                    currentPosition.ColumnNumber++;
                }
                else if (source[i] == DELIMITER_ROW)
                {
                    currentPosition.RowNumber++;
                }
                else
                {
                    puzzle.SetValue(currentPosition, Int32.Parse(source[i].ToString()));
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Verifies the problem represented by <paramref name="theValues"/>.
        /// </summary>
        /// <param name="theValues"></param>
        private void VerifyProblem(int[] theValues)
        {
            ISudokuPuzzle theSolution = null;

            var theProblem = theValues.ToSudokuPuzzle();

            void ReportTheValues()
            {
                string RenderTheValues()
                {
                    return(string.Join(@", ", theValues.Select(x => $"{x}")));
                }

                OutputHelper.WriteLine($"The Values: {RenderTheValues()}");
            }

            ReportTheValues();

            theProblem.PrettyPrint(s => OutputHelper.WriteLine(s));

            //TODO: introduce time outs to the solver ...
            // Should solve and not time out...
            using (var s = new SudokuProblemSolver(theProblem))
            {
                s.Solved += (sender, e) =>
                {
                    var problemSolver = Assert.IsAssignableFrom <SudokuProblemSolver>(sender);
                    Assert.NotNull(problemSolver.Solution);
                    theSolution = problemSolver.Solution;
                };

                Assert.True(s.TryResolve());
            }

            Assert.NotNull(theSolution);
            Assert.NotSame(theProblem, theSolution);
            Assert.True(theSolution.IsSolved);

            theSolution.PrettyPrint(s => OutputHelper.WriteLine(s));
        }
예제 #8
0
        private void AdjustPuzzleToConfig(ISudokuPuzzle sudoku, SudokuDifficulty config, int seed)
        {
            int vals = GetDifficulty(config);

            List <int> locations = new List <int>();
            Random     rand      = new Random(seed);

            for (int i = 0; i < vals; i++)
            {
                int location;
                do
                {
                    location = rand.Next(81);
                } while (locations.Contains(location));
                locations.Add(location);
            }

            foreach (int i in locations)
            {
                sudoku.Cells[i] = new UserCell(i);
            }
        }
        /// <summary>
        /// <see cref="ISearchAgent"/> ProcessVariables event handler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnProcessVariables(object sender, ProcessVariablesEventArgs e)
        {
            var           candidate = new SudokuPuzzle();
            ISudokuPuzzle local     = candidate;

            // In this case we know that there is a Single Aspect.
            var aspect = Aspects.SingleOrDefault();

            Assert.That(aspect, Is.Not.Null);

            const int size = SudokuProblemSolverAspect.Size;

            for (var row = 0; row < size; row++)
            {
                for (var col = 0; col < size; col++)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    local[row, col] = (int)aspect.Cells[row, col].Value();
                }
            }

            /* If we're here processing variables, it should be because we are processing the next
             * solution. However, in the event we still do not have a solution, then simply return. */

            // TODO: TBD: we really shoul never land here I don't think...
            if (!local.IsSolved)
            {
                return;
            }

            Solution = local;

            // False is the default, so only mark whether ShouldBreak when we have one.
            e.ShouldBreak = true;

            base.OnProcessVariables(sender, e);
        }
예제 #10
0
 public SudokuRegion(ISudokuPuzzle puzzle, ISudokuPosition position)
 {
     InitializeRegion(puzzle, position);
 }
 /// <summary>
 /// Default Constructor
 /// </summary>
 /// <param name="puzzle"></param>
 public SudokuProblemSolver(ISudokuPuzzle puzzle)
     : base(@"Sudoku Solver")
 {
     Puzzle = puzzle;
 }
예제 #12
0
 public SudokuSolver(ISudokuPuzzle input, ISudokuValidator validator)
 {
     m_Puzzle    = input;
     m_Validator = validator;
 }
예제 #13
0
 public SudokuSolver(string sudoku, ISudokuValidator validator)
 {
     m_Puzzle    = new SudokuPuzzle(sudoku);
     m_Validator = validator;
 }
예제 #14
0
    private XmlNode CreateSolutionPuzzleNode()
    {
        inputPuzzle = ParseInputPuzzleFromForm();
        //ISudokuStrategy strat = new SimpleBruteForceSudokuStrategy();
        //ISudokuStrategy strat = new BruteForceSudokuStrategy();
        ISudokuStrategy strat = new CycleFirstBruteForceSudokuStrategy();
        int start = Environment.TickCount;
        bool solved = inputPuzzle.Solve(strat);
        int end = Environment.TickCount;
        int tts = end - start;

        ISudokuPuzzleXmlExporter exporter = new SudokuPuzzleExporter();
        XmlNode puzzleNode = exporter.Export(inputPuzzle);
        XmlElement inputPuzzleNode = puzzleNode.OwnerDocument.CreateElement("SolutionPuzzle");
        XmlAttribute attSolved = puzzleNode.OwnerDocument.CreateAttribute("IsSolved");
        XmlAttribute attTts = puzzleNode.OwnerDocument.CreateAttribute("TimeToSolve");

        attSolved.Value = solved.ToString();
        attTts.Value = tts.ToString();
        inputPuzzleNode.AppendChild(puzzleNode);
        inputPuzzleNode.Attributes.Append(attSolved);
        inputPuzzleNode.Attributes.Append(attTts);

        return inputPuzzleNode;
    }
 public void Solve(ISudokuPuzzle puzzle)
 {
     this.puzzle = puzzle;
     FindSolution();
 }
        private void ParsePuzzleXml(XmlNode source, ISudokuPuzzle puzzle)
        {
            int squareSize = puzzle.Size * puzzle.Size;
            ISudokuPosition currentPosition = new SudokuPosition();
            XmlElement elmBoard = source[XML_ELM_BOARD];
            XmlElement elmRow;
            XmlElement elmCell;
            XmlAttribute attRow;
            XmlAttribute attCol;

            foreach (XmlNode row in elmBoard.ChildNodes)
            {
                elmRow = (XmlElement)row;

                foreach (XmlNode cell in elmRow.ChildNodes)
                {
                    elmCell = (XmlElement)cell;
                    attRow = elmCell.Attributes[XML_ATT_ROW];
                    attCol = elmCell.Attributes[XML_ATT_COL];

                    currentPosition.RowNumber = Int32.Parse(attRow.Value);
                    currentPosition.ColumnNumber = Int32.Parse(attCol.Value);

                    puzzle.SetValue(currentPosition, Int32.Parse(elmCell.InnerText));
                }
            }

            //for (int i = 0; i < squareSize; i++)
            //{
            //    currentPosition.RowNumber = i;
            //    elmRow = (XmlElement)elmBoard.ChildNodes[i];

            //    for (int j = 0; j < squareSize; j++)
            //    {
            //        currentPosition.ColumnNumber = j;
            //        elmCell = (XmlElement)elmRow.ChildNodes[j];

            //        puzzle.SetValue(currentPosition, Int32.Parse(elmCell.InnerText));
            //    }
            //}
        }
 public BruteForceSudokuStrategyOLD()
 {
     puzzle = null;
 }
 public SimpleBruteForceSudokuStrategy()
 {
     puzzle = null;
 }
예제 #19
0
 protected SudokuStrategy()
 {
     puzzle = null;
 }
예제 #20
0
 public void Solve(ISudokuPuzzle puzzle)
 {
     this.puzzle = puzzle;
     DoSolve();
 }
예제 #21
0
 /// <summary>
 /// Default Constructor.
 /// </summary>
 /// <param name="puzzle"></param>
 /// <inheritdoc />
 public SudokuProblemSolver(ISudokuPuzzle puzzle)
 {
     Puzzle = puzzle;
 }