コード例 #1
0
        public void SolveTest()
        {
            List<Node> solveTestNodes = new List<Node>();
            for(int i = 0; i < 9; i++)
            {
                solveTestNodes.Add(new Node());

            }
            solveTestNodes[0].Possibilities = new List<int>() { 1 };
            solveTestNodes[1].Possibilities = new List<int>() { 1, 2 };
            solveTestNodes[2].Possibilities = new List<int>() { 1, 2, 3 };
            solveTestNodes[3].Possibilities = new List<int>() { 1, 3, 4 };
            solveTestNodes[4].Possibilities = new List<int>() { 1, 4, 5 };
            solveTestNodes[5].Possibilities = new List<int>() { 2, 5, 6 };
            solveTestNodes[6].Possibilities = new List<int>() { 2, 6, 7 };
            solveTestNodes[7].Possibilities = new List<int>() { 2, 7, 8 };
            solveTestNodes[8].Possibilities = new List<int>() { 2, 8, 9 };

            NodeGroup solveTest = new NodeGroup(solveTestNodes);
            solveTest.Solve();
            Assert.AreEqual(1, solveTest.Nodes[0].Value);
            Assert.AreEqual(2, solveTest.Nodes[1].Value);
            Assert.AreEqual(3, solveTest.Nodes[2].Value);
            Assert.AreEqual(4, solveTest.Nodes[3].Value);
            Assert.AreEqual(5, solveTest.Nodes[4].Value);
            Assert.AreEqual(6, solveTest.Nodes[5].Value);
            Assert.AreEqual(7, solveTest.Nodes[6].Value);
            Assert.AreEqual(8, solveTest.Nodes[7].Value);
            Assert.AreEqual(9, solveTest.Nodes[8].Value);
        }
コード例 #2
0
ファイル: Puzzle.cs プロジェクト: gjjeffers/SudokuSolver2
 public Puzzle(string startingPuzzle)
 {
     // any starting string not 81 characters long is not a valid puzzle
     if (startingPuzzle.Length != 81)
     {
         Status = "Invalid Starting Puzzle String";
         return;
     }
     Grid = new List<Node>();
     Sections = new List<NodeGroup>();
     SetupPuzzle();
     for(int i = 1; i < 10; i++)
     {
         NodeGroup newRow = new NodeGroup(Grid.Where(n => n.Row == i).ToList());
         newRow.PropertyChanged += TrackActivity;
         Sections.Add(newRow);
         NodeGroup newColumn = new NodeGroup(Grid.Where(n => n.Column == i).ToList());
         newColumn.PropertyChanged += TrackActivity;
         Sections.Add(newColumn);
         NodeGroup newBlock = new NodeGroup(Grid.Where(n => n.Block == i).ToList());
         newBlock.PropertyChanged += TrackActivity;
         Sections.Add(newBlock);
     }
     InitializeValues(startingPuzzle);
 }
コード例 #3
0
ファイル: Puzzle.cs プロジェクト: gjjeffers/SudokuSolver2
 public Puzzle()
 {
     /* main object. contains the grid of nodes in a list. contains all NodeGroups in a list.
        validates and determines completeness of puzzles.
        solves puzzles through deductive logic contained in NodeGroup until no singluar values remain.
        if deductive solving stops without a completed puzzle, solving "guesses" using the first possibility 
        of the first available node, if that node fails to resolve into a valid and complete puzzle, the possibility
        is removed and the "guess" attempts the same with the next available possibility.
        if no solution can be found, the last good state of the puzzle is returned with 0 at indeterminate positions
     */
     Grid = new List<Node>();
     Sections = new List<NodeGroup>();
     SetupPuzzle();
     // add each row, column, and block to Sections and functions to listen for activity
     for (int i = 1; i < 10; i++)
     {
         NodeGroup newRow = new NodeGroup(Grid.Where(n => n.Row == i).ToList());
         newRow.PropertyChanged += TrackActivity;
         Sections.Add(newRow);
         NodeGroup newColumn = new NodeGroup(Grid.Where(n => n.Column == i).ToList());
         newColumn.PropertyChanged += TrackActivity;
         Sections.Add(newColumn);
         NodeGroup newBlock = new NodeGroup(Grid.Where(n => n.Block == i).ToList());
         newBlock.PropertyChanged += TrackActivity;
         Sections.Add(newBlock);
     }
 }
コード例 #4
0
        public void ValueSetEventTest()
        {
            List<Node> valueTestNodes = new List<Node>();
            valueTestNodes.Add(new Node());
            valueTestNodes.Add(new Node());

            NodeGroup valueTest = new NodeGroup(valueTestNodes);
            valueTest.Nodes[0].SetValue(1);
            Assert.IsFalse(valueTest.Nodes[0].Possibilities.Contains(1));
            Assert.IsFalse(valueTest.Nodes[1].Possibilities.Contains(1));
        }
コード例 #5
0
        public void PossibilityCountTest()
        {
            List<Node> possTestNodes = new List<Node>();
            possTestNodes.Add(new Node());
            possTestNodes.Add(new Node());
            possTestNodes.Add(new Node());

            NodeGroup possTest = new NodeGroup(possTestNodes);
            possTest.Nodes[0].SetValue(1);
            Assert.AreEqual(0, possTest.PossibilityCount(1));
            Assert.AreEqual(2, possTest.PossibilityCount(2));

        }
コード例 #6
0
ファイル: Puzzle.cs プロジェクト: gjjeffers/SudokuSolver2
        public object Clone()
        {
            var clonedPuzzle = new Puzzle();
            clonedPuzzle.Grid = new List<Node>();
            foreach(Node n in Grid)
            {
                clonedPuzzle.Grid.Add((Node)n.Clone());
            }
            for (int i = 1; i < 10; i++)
            {
                NodeGroup newRow = new NodeGroup(clonedPuzzle.Grid.Where(n => n.Row == i).ToList());
                newRow.PropertyChanged += TrackActivity;
                clonedPuzzle.Sections.Add(newRow);
                NodeGroup newColumn = new NodeGroup(clonedPuzzle.Grid.Where(n => n.Column == i).ToList());
                newColumn.PropertyChanged += TrackActivity;
                clonedPuzzle.Sections.Add(newColumn);
                NodeGroup newBlock = new NodeGroup(clonedPuzzle.Grid.Where(n => n.Block == i).ToList());
                newBlock.PropertyChanged += TrackActivity;
                clonedPuzzle.Sections.Add(newBlock);
            }

            return clonedPuzzle;
        }