Beispiel #1
0
 /// <summary>
 /// Creates a deep copy of this tracker in its current state.
 /// </summary>
 internal SquareTracker(SquareTracker <TPuzzle> existing)
 {
     _puzzle    = existing._puzzle.DeepCopy();
     _setCoords = new Stack <Coordinate>(existing._setCoords);
     if (existing._coordsThatUsedHeuristics is not null)
     {
         _coordsThatUsedHeuristics = new Stack <Coordinate>(existing._coordsThatUsedHeuristics !);
     }
     _ruleKeeper = existing._ruleKeeper.CopyWithNewReferences(_puzzle);
     _heuristic  = existing._heuristic?.CopyWithNewReferences(
         _puzzle, _ruleKeeper.GetRules());
 }
        public void CopyWithNewReferences_CreatesDeepCopy()
        {
            var puzzle = new PuzzleWithPossibleValues(new int?[][] {
                new int?[] { 1, null /* 4 */, null /* 3 */, 2 },
                new int?[] { null /* 2 */, null /* 3 */, 1, null /* 4 */ },
                new int?[] { null /* 4 */, null /* 1 */, null /* 2 */, null /* 3 */ },
                new int?[] { 3, 2, 4, 1 }
            });
            var rules = new IRule[]
            {
                new RowUniquenessRule(),
                new ColumnUniquenessRule(),
                new BoxUniquenessRule()
            };
            var ruleKeeper = new DynamicRuleKeeper(rules);

            Assert.True(ruleKeeper.TryInit(puzzle));

            var         puzzleCopy     = new PuzzleWithPossibleValues(puzzle);
            IRuleKeeper ruleKeeperCopy = ruleKeeper.CopyWithNewReferences(puzzleCopy);

            var rulesCopy = ruleKeeperCopy.GetRules();

            Assert.Equal(rules.Length, rulesCopy.Length);
            for (int i = 0; i < rules.Length; i++)
            {
                Assert.NotSame(rules[i], rulesCopy[i]);
                Type originalType = rules[i].GetType();
                Type copiedType   = rulesCopy[i].GetType();
                Assert.Equal(originalType, copiedType);
            }
            var coord = new Coordinate(0, 1);
            int val   = 4;

            Assert.True(ruleKeeperCopy.TrySet(coord, val));
            Assert.Equal(new BitVector(0b11000), puzzle.GetPossibleValues(new Coordinate(1, 1)));
            Assert.Equal(new BitVector(0b01000), puzzleCopy.GetPossibleValues(new Coordinate(1, 1)));
        }