public void Update_UpdatesSpecifiedColumn() { var puzzle = new PuzzleWithPossibleValues(new int?[][] { new int?[] { 1, null /* 4 */, null /* 3 */, 2 }, new int?[] { null /* 2 */, null /* 3 */, null /* 1 */, 4 }, new int?[] { null /* 4 */, 1, null /* 2 */, 3 }, new int?[] { 3, null /* 2 */, null /* 4 */, 1 } }); var rule = new ColumnUniquenessRule(); Assert.True(rule.TryInit(puzzle, puzzle.UniquePossibleValues)); var coordTracker = new CoordinateTracker(puzzle.Size); var coord = new Coordinate(1, 1); int val = 3; rule.Update(coord, val, coordTracker); Assert.Equal( new HashSet <Coordinate> { new Coordinate(0, 1), new Coordinate(3, 1) }, new HashSet <Coordinate>(coordTracker.TrackedCoords.ToArray())); Assert.Equal(new BitVector(0b10100), rule.GetPossibleValues(new Coordinate(0, 0))); Assert.Equal(new BitVector(0b10100), rule.GetPossibleValues(new Coordinate(0, 1))); Assert.Equal(new BitVector(0b11110), rule.GetPossibleValues(new Coordinate(0, 2))); Assert.Equal(new BitVector(0b00000), rule.GetPossibleValues(new Coordinate(0, 3))); }
public void TrySolve_MegaPuzzle_Solves() { var puzzle = new PuzzleWithPossibleValues(new int?[][] { new int?[] { null, null, null, null, 10, 1, null, 8, null, 15, 3, 11, null, 2, 16, null }, new int?[] { 14, null, 2, null, null, 4, 3, null, null, 13, 8, null, null, 12, null, null }, new int?[] { null, null, null, 12, null, null, null, 15, null, null, null, 7, null, null, 9, 10 }, new int?[] { 1, 10, 15, null, 6, null, null, null, null, 14, null, null, null, null, null, 11 }, new int?[] { null, 11, 14, 6, null, null, null, 9, 13, 8, null, null, null, null, 2, 3 }, new int?[] { 12, null, null, null, 4, null, 7, 3, 11, 6, null, null, 16, null, 5, null }, new int?[] { 13, 16, null, 2, null, null, null, 1, null, null, 5, null, 10, 9, null, null }, new int?[] { null, 4, null, null, 13, null, 2, null, null, null, 16, 3, 11, null, null, null }, new int?[] { null, null, null, 10, 3, 6, null, null, null, 9, null, 12, null, null, 4, null }, new int?[] { null, null, 12, 15, null, 9, null, null, 7, null, null, null, 1, null, 3, 14 }, new int?[] { null, 1, null, 4, null, null, 5, 12, 3, 10, null, 8, null, null, null, 2 }, new int?[] { 3, 6, null, null, null, null, 15, 10, 4, null, null, null, 12, 5, 7, null }, new int?[] { 2, null, null, null, null, null, 4, null, null, null, null, 15, null, 16, 11, 9 }, new int?[] { 4, 14, null, null, 16, null, null, null, 2, null, null, null, 6, null, null, null }, new int?[] { null, null, 16, null, null, 7, 8, null, null, 4, 10, null, null, 14, null, 5 }, new int?[] { null, 3, 6, null, 9, 12, 14, null, 8, null, 13, 16, null, null, null, null } }); var rowRule = new RowUniquenessRule(); var columnRule = new ColumnUniquenessRule(); var boxRule = new BoxUniquenessRule(); var diagonalRule = new DiagonalUniquenessRule(); var ruleKeeper = new DynamicRuleKeeper( new IRule[] { rowRule, columnRule, boxRule, diagonalRule }); var heuristic = new StandardHeuristic( rowRule, columnRule, boxRule); var solver = new PuzzleSolver <PuzzleWithPossibleValues>(ruleKeeper, heuristic); Assert.True(solver.TrySolve(puzzle)); _AssertMegaPuzzleSolved(puzzle); }
public void Revert_RevertsSpecifiedColumn() { var puzzle = new PuzzleWithPossibleValues(new int?[][] { new int?[] { 1, null /* 4 */, null /* 3 */, 2 }, new int?[] { null /* 2 */, null /* 3 */, null /* 1 */, 4 }, new int?[] { null /* 4 */, 1, null /* 2 */, 3 }, new int?[] { 3, null /* 2 */, null /* 4 */, 1 } }); var rule = new ColumnUniquenessRule(); Assert.True(rule.TryInit(puzzle, puzzle.UniquePossibleValues)); IList <BitVector> initialPossibleValuesByColumn = _GetPossibleValuesByColumn(puzzle.Size, rule); var updatedCoordTracker = new CoordinateTracker(puzzle.Size); var coord = new Coordinate(1, 1); int val = 3; rule.Update(coord, val, updatedCoordTracker); var revertedCoordTracker = new CoordinateTracker(puzzle.Size); rule.Revert(coord, val, revertedCoordTracker); Assert.Equal( updatedCoordTracker.TrackedCoords.ToArray(), revertedCoordTracker.TrackedCoords.ToArray()); for (int column = 0; column < initialPossibleValuesByColumn.Count; column++) { Assert.Equal( initialPossibleValuesByColumn[column], rule.GetMissingValuesForColumn(column)); } }
public void Constructor_AcceptsValidPuzzleSizes(int size) { int?[,] matrix = new int?[size, size]; var puzzle = new Puzzle(matrix); var rule = new ColumnUniquenessRule(puzzle, _GetAllPossibleValues(puzzle.Size)); Assert.NotNull(rule); }
private IList <BitVector> _GetPossibleValuesByColumn(int numColumns, ColumnUniquenessRule rule) { var possibleColumnValues = new List <BitVector>(); for (int column = 0; column < numColumns; column++) { possibleColumnValues.Add(rule.GetMissingValuesForColumn(column)); } return(possibleColumnValues); }
public void TryInit_WithDuplicateValueInColumn_Fails() { var puzzle = new PuzzleWithPossibleValues( new int?[][] { new int?[] { 1, null /* 4 */, null /* 3 */, 2 }, new int?[] { /* INCORRECT */ 1, null /* 3 */, null /* 1 */, 4 }, new int?[] { null /* 4 */, 1, null /* 2 */, 3 }, new int?[] { 3, null /* 2 */, null /* 4 */, 1 } }); var rule = new ColumnUniquenessRule(); Assert.False(rule.TryInit(puzzle, puzzle.UniquePossibleValues)); }
public bool SudokuSpiceDynamicMultiple(PuzzleSample puzzle) { var p = new PuzzleWithPossibleValues(puzzle.NullableJaggedMatrix); var rowRule = new RowUniquenessRule(); var columnRule = new ColumnUniquenessRule(); var boxRule = new BoxUniquenessRule(); var ruleKeeper = new DynamicRuleKeeper( new IRule[] { rowRule, columnRule, boxRule }); var heuristic = new StandardHeuristic( rowRule, columnRule, boxRule); var solver = new RuleBased.PuzzleSolver <PuzzleWithPossibleValues>(ruleKeeper, heuristic); var result = solver.Solve(p, randomizeGuesses: true); return(result.NumEmptySquares == 0); }
public void Constructor_FiltersCorrectly() { var puzzle = new Puzzle(new int?[, ] { { 1, null /* 4 */, null /* 3 */, 2 }, { null /* 2 */, null /* 3 */, null /* 1 */, 4 }, { null /* 4 */, 1, null /* 2 */, 3 }, { 3, null /* 2 */, null /* 4 */, 1 } }); var rule = new ColumnUniquenessRule(puzzle, _GetAllPossibleValues(puzzle.Size)); Assert.Equal(new BitVector(0b10100), rule.GetPossibleValues(new Coordinate(0, 0))); Assert.Equal(new BitVector(0b11100), rule.GetPossibleValues(new Coordinate(0, 1))); Assert.Equal(new BitVector(0b11110), rule.GetPossibleValues(new Coordinate(0, 2))); Assert.Equal(new BitVector(0b00000), rule.GetPossibleValues(new Coordinate(0, 3))); }
public void TryInit_FiltersCorrectly() { var puzzle = new PuzzleWithPossibleValues(new int?[][] { new int?[] { 1, null /* 4 */, null /* 3 */, 2 }, new int?[] { null /* 2 */, null /* 3 */, null /* 1 */, 4 }, new int?[] { null /* 4 */, 1, null /* 2 */, 3 }, new int?[] { 3, null /* 2 */, null /* 4 */, 1 } }); var rule = new ColumnUniquenessRule(); Assert.True(rule.TryInit(puzzle, puzzle.UniquePossibleValues)); Assert.Equal(new BitVector(0b10100), rule.GetPossibleValues(new Coordinate(0, 0))); Assert.Equal(new BitVector(0b11100), rule.GetPossibleValues(new Coordinate(0, 1))); Assert.Equal(new BitVector(0b11110), rule.GetPossibleValues(new Coordinate(0, 2))); Assert.Equal(new BitVector(0b00000), rule.GetPossibleValues(new Coordinate(0, 3))); }
public void Constructor_WithDuplicateValueInColumn_Throws() { ArgumentException ex = Assert.Throws <ArgumentException>(() => { var rule = new ColumnUniquenessRule( new Puzzle( new int?[, ] { { 1, null /* 4 */, null /* 3 */, 2 }, { /* INCORRECT */ 1, null /* 3 */, null /* 1 */, 4 }, { null /* 4 */, 1, null /* 2 */, 3 }, { 3, null /* 2 */, null /* 4 */, 1 } }), BitVector.CreateWithSize(4)); }); Assert.Contains("Puzzle has duplicate value in column", ex.Message); }
public void Solve_ValidPuzzle_SolvesPuzzle(Puzzle puzzle) { var possibleValues = new PossibleValues(puzzle); var rowRule = new RowUniquenessRule(puzzle, possibleValues.AllPossible); var columnRule = new ColumnUniquenessRule(puzzle, possibleValues.AllPossible); var boxRule = new BoxUniquenessRule(puzzle, possibleValues.AllPossible, true); var ruleKeeper = new DynamicRuleKeeper( puzzle, possibleValues, new List <ISudokuRule> { rowRule, columnRule, boxRule }); var heuristic = new StandardHeuristic( puzzle, possibleValues, rowRule, columnRule, boxRule); var solver = new PuzzleSolver(puzzle, possibleValues, ruleKeeper, heuristic); solver.Solve(); _AssertPuzzleSolved(puzzle); }
public bool SudokuSpiceDynamicMultiple(PuzzleSample puzzle) { var p = new Puzzle(puzzle.NullableMatrix); var possibleValues = new PossibleValues(p); var rowRule = new RowUniquenessRule(p, possibleValues.AllPossible); var columnRule = new ColumnUniquenessRule(p, possibleValues.AllPossible); var boxRule = new BoxUniquenessRule(p, possibleValues.AllPossible, true); var ruleKeeper = new DynamicRuleKeeper( p, possibleValues, new List <ISudokuRule> { rowRule, columnRule, boxRule }); var heuristic = new StandardHeuristic( p, possibleValues, rowRule, columnRule, boxRule); var solver = new PuzzleSolver(p, possibleValues, ruleKeeper, heuristic); solver.SolveRandomly(); return(p.NumEmptySquares == 0); }
public void GetPossibleValues_MatchesGetPossibleColumnValues() { var puzzle = new Puzzle(new int?[, ] { { 1, null /* 4 */, null /* 3 */, 2 }, { null /* 2 */, null /* 3 */, null /* 1 */, 4 }, { null /* 4 */, 1, null /* 2 */, 3 }, { 3, null /* 2 */, null /* 4 */, 1 } }); var rule = new ColumnUniquenessRule(puzzle, _GetAllPossibleValues(puzzle.Size)); IList <BitVector> possibleValuesByColumn = _GetPossibleValuesByColumn(puzzle.Size, rule); for (int column = 0; column < possibleValuesByColumn.Count; column++) { for (int row = 0; row < puzzle.Size; row++) { Assert.Equal( possibleValuesByColumn[column], rule.GetPossibleValues(new Coordinate(row, column))); } } }
public void CopyWithNewReference_CreatesDeepCopy() { var puzzle = new PuzzleWithPossibleValues(new int?[][] { new int?[] { 1, null /* 4 */, null /* 3 */, 2 }, new int?[] { null /* 2 */, null /* 3 */, null /* 1 */, 4 }, new int?[] { null /* 4 */, 1, null /* 2 */, 3 }, new int?[] { 3, null /* 2 */, null /* 4 */, 1 } }); var rule = new ColumnUniquenessRule(); Assert.True(rule.TryInit(puzzle, puzzle.UniquePossibleValues)); var puzzleCopy = new PuzzleWithPossibleValues(puzzle); IRule ruleCopy = rule.CopyWithNewReference(puzzleCopy); int val = 4; var coord = new Coordinate(3, 2); ruleCopy.Update(coord, val, new CoordinateTracker(puzzle.Size)); Assert.NotEqual(rule.GetPossibleValues(coord), ruleCopy.GetPossibleValues(coord)); puzzleCopy[coord] = val; var secondCoord = new Coordinate(2, 2); int secondVal = 2; var coordTracker = new CoordinateTracker(puzzle.Size); ruleCopy.Update(secondCoord, secondVal, coordTracker); var originalCoordTracker = new CoordinateTracker(puzzle.Size); rule.Update(secondCoord, secondVal, originalCoordTracker); Assert.Equal( new HashSet <Coordinate> { new Coordinate(0, 2), new Coordinate(1, 2) }, new HashSet <Coordinate>(coordTracker.TrackedCoords.ToArray())); Assert.Equal( new HashSet <Coordinate> { new Coordinate(0, 2), new Coordinate(1, 2), new Coordinate(3, 2) }, new HashSet <Coordinate>(originalCoordTracker.TrackedCoords.ToArray())); }
public void Solve_MegaPuzzle_Solves() { var puzzle = new Puzzle(new int?[, ] { { null, null, null, null, 10, 1, null, 8, null, 15, 3, 11, null, 2, 16, null }, { 14, null, 2, null, null, 4, 3, null, null, 13, 8, null, null, 12, null, null }, { null, null, null, 12, null, null, null, 15, null, null, null, 7, null, null, 9, 10 }, { 1, 10, 15, null, 6, null, null, null, null, 14, null, null, null, null, null, 11 }, { null, 11, 14, 6, null, null, null, 9, 13, 8, null, null, null, null, 2, 3 }, { 12, null, null, null, 4, null, 7, 3, 11, 6, null, null, 16, null, 5, null }, { 13, 16, null, 2, null, null, null, 1, null, null, 5, null, 10, 9, null, null }, { null, 4, null, null, 13, null, 2, null, null, null, 16, 3, 11, null, null, null }, { null, null, null, 10, 3, 6, null, null, null, 9, null, 12, null, null, 4, null }, { null, null, 12, 15, null, 9, null, null, 7, null, null, null, 1, null, 3, 14 }, { null, 1, null, 4, null, null, 5, 12, 3, 10, null, 8, null, null, null, 2 }, { 3, 6, null, null, null, null, 15, 10, 4, null, null, null, 12, 5, 7, null }, { 2, null, null, null, null, null, 4, null, null, null, null, 15, null, 16, 11, 9 }, { 4, 14, null, null, 16, null, null, null, 2, null, null, null, 6, null, null, null }, { null, null, 16, null, null, 7, 8, null, null, 4, 10, null, null, 14, null, 5 }, { null, 3, 6, null, 9, 12, 14, null, 8, null, 13, 16, null, null, null, null } }); var possibleValues = new PossibleValues(puzzle); var rowRule = new RowUniquenessRule(puzzle, possibleValues.AllPossible); var columnRule = new ColumnUniquenessRule(puzzle, possibleValues.AllPossible); var boxRule = new BoxUniquenessRule(puzzle, possibleValues.AllPossible, true); var diagonalRule = new DiagonalUniquenessRule(puzzle, possibleValues.AllPossible); var ruleKeeper = new DynamicRuleKeeper( puzzle, possibleValues, new List <ISudokuRule> { rowRule, columnRule, boxRule, diagonalRule }); var heuristic = new StandardHeuristic( puzzle, possibleValues, rowRule, columnRule, boxRule); var solver = new PuzzleSolver(puzzle, possibleValues, ruleKeeper, heuristic); solver.Solve(); _AssertMegaPuzzleSolved(puzzle); }
public void Revert_WithoutAffectedCoordsList_RevertsSpecifiedColumn() { var puzzle = new Puzzle(new int?[, ] { { 1, null /* 4 */, null /* 3 */, 2 }, { null /* 2 */, null /* 3 */, null /* 1 */, 4 }, { null /* 4 */, 1, null /* 2 */, 3 }, { 3, null /* 2 */, null /* 4 */, 1 } }); var rule = new ColumnUniquenessRule(puzzle, _GetAllPossibleValues(puzzle.Size)); IList <BitVector> initialPossibleValuesByColumn = _GetPossibleValuesByColumn(puzzle.Size, rule); var coord = new Coordinate(1, 1); int val = 3; rule.Update(coord, val, new CoordinateTracker(puzzle.Size)); rule.Revert(coord, val); for (int column = 0; column < initialPossibleValuesByColumn.Count; column++) { Assert.Equal( initialPossibleValuesByColumn[column], rule.GetMissingValuesForColumn(column)); } }