diff --git a/src/Sudoku.Console/Supporting Files/Node.cs b/src/Sudoku.Console/Supporting Files/Node.cs index 56bbb60..f640434 100644 --- a/src/Sudoku.Console/Supporting Files/Node.cs +++ b/src/Sudoku.Console/Supporting Files/Node.cs @@ -4,12 +4,12 @@ public class Node { private readonly List _children = []; - private readonly Node _parent; - private readonly int[] _puzzleState = new int[81]; public Move Move { get; } + public Node Parent { get; } + public IReadOnlyList Children => _children; public Node(int[] puzzleState) @@ -24,7 +24,7 @@ private Node(Move move, Node parent) { Move = move; - _parent = parent; + Parent = parent; for (var i = 0; i < 81; i++) { @@ -36,13 +36,12 @@ private Node(Move move, Node parent) public int this[int index] => _puzzleState[index]; - public void AddChild(Move child) + public Node AddChild(Move child) { - _children.Add(new Node(child, this)); - } + var node = new Node(child, this); + + _children.Add(node); - public Node Parent() - { - return _parent; + return node; } } \ No newline at end of file diff --git a/src/Sudoku.Console/TreeGenerator.cs b/src/Sudoku.Console/TreeGenerator.cs index 6fc6234..17dc67f 100644 --- a/src/Sudoku.Console/TreeGenerator.cs +++ b/src/Sudoku.Console/TreeGenerator.cs @@ -33,7 +33,14 @@ private void GenerateNodes(int[] puzzle, SudokuResult result) foreach (var move in result.History) { + if (move.Type == MoveType.Backtrack) + { + node = node.Parent; + + continue; + } + node = node.AddChild(move); } } } \ No newline at end of file