Esempio n. 1
0
        public Puzzle(IList <int> nodeValues, IList <int> expectedValues)
        {
            if (nodeValues == null)
            {
                throw new ArgumentNullException(nameof(nodeValues));
            }
            if (expectedValues == null)
            {
                throw new ArgumentNullException(nameof(expectedValues));
            }
            if (nodeValues.Count != expectedValues.Count)
            {
                throw new ArgumentException(
                          $"Expected {expectedValues.Count} " +
                          $"input values for DiamondPuzzle initialization. Actual: {nodeValues.Count}.");
            }

            MoveList = new List <int>();
            Nodes    = new PuzzleNodeList();
            for (int i = 0; i < nodeValues.Count; i++)
            {
                Nodes.Add(
                    new PuzzleNode(nodeValues[i], expectedValues[i]));
            }

            if (Nodes.Count(node => node.IsEmpty()) != 1)
            {
                throw new InvalidOperationException(
                          $"Invalid puzzle configuration: '{Nodes}'. Puzzle must have exactly one open site.");
            }

            ConnectNodes(Connections, Nodes);
        }
Esempio n. 2
0
 private void ConnectNodes(KeyValueList <int, int> connections, PuzzleNodeList nodes)
 {
     foreach ((int left, int right) in connections)
     {
         Connect(nodes.GetNodeByExpectedValue(left), nodes.GetNodeByExpectedValue(right));
     }
 }