Esempio n. 1
0
        public Board Solve(Board board)
        {
            var possibilityCalculator = new PossibilityCalculator(board);

            // Get the brute force possibilities
            var bruteForceSolvingPossibilities = new List <BruteForceSolvingPossibility>();

            for (var line = 0; line < board.Lines; line++)
            {
                for (var column = 0; column < board.Columns; column++)
                {
                    var value = board.GetBoardValue(line, column);

                    if (!value.IsProvided)
                    {
                        bruteForceSolvingPossibilities.Add(
                            new BruteForceSolvingPossibility()
                        {
                            Column        = column,
                            Line          = line,
                            Possibilities = possibilityCalculator.PossibilitiesFor(line, column)
                        }
                            );
                    }
                }
            }

            // Brute force solve the board
            return(GetSolution(board, bruteForceSolvingPossibilities, 0));
        }
        public void PossibilitiesFor_ExistingValueTest()
        {
            var boardHash =
                "123456789" +
                "234567891" +
                "345678912" +
                "456789123" +
                "567891234" +
                "678912345" +
                "789123456" +
                "891234567" +
                "912345678";

            var board = (new BoardHash()).LoadFromHash(boardHash);
            var possibilityCalculator = new PossibilityCalculator(board);

            for (var line = 0; line < board.Lines; line++)
            {
                for (var column = 0; column < board.Columns; column++)
                {
                    Assert.AreEqual(
                        board.GetBoardValue(line, column).Value,
                        possibilityCalculator.PossibilitiesFor(line, column).First()
                        );
                }
            }
        }
        public void PossibilitiesFor_EmptyFields()
        {
            // Arrange
            var boardHash = new BoardHash();
            var testSet   = GetTestSet();

            foreach (var assert in testSet)
            {
                // Act
                var board = boardHash.LoadFromHash(assert.Hash);
                var possibilityCalculator = new PossibilityCalculator(board);

                var actual = possibilityCalculator.PossibilitiesFor(assert.Line, assert.Column);

                // Assert
                CollectionAssert.AreEqual(assert.ExpectedResult, actual);
            }
        }
Esempio n. 4
0
        // Get the board randomly filled
        private Board GetRandomBoard(string hash)
        {
            var result = _boardHash.LoadFromHash(hash);
            var possibilityCalculator = new PossibilityCalculator(_boardHash.LoadFromHash(hash));

            for (var line = 0; line < result.Lines; line++)
            {
                for (var column = 0; column < result.Columns; column++)
                {
                    var value = result.GetBoardValue(line, column);

                    if (!value.IsProvided)
                    {
                        var possibilities = possibilityCalculator.PossibilitiesFor(line, column);
                        var rnd           = _random.Next(possibilities.Count);
                        var newValue      = possibilities[rnd];
                        result.SetBoardValue(line, column, new BoardValue(newValue));
                    }
                }
            }

            return(result);
        }