Esempio n. 1
0
        /// <summary>
        /// Finds the path of least resistance.
        /// </summary>
        /// <param name="water">The water.</param>
        /// <returns></returns>
        public PathResults FindPathOfLeastResistance(Water water)
        {
            List<int> pathsTaken = new List<int>();
            bool didWaterFlowThroughEntireGrid = true;
            int lastPathTakenIndex = -1;

            try
            {
                foreach (int[] paths in _grid)
                {
                    int[] pathOptions = this.GetPathOptions(paths, lastPathTakenIndex);

                    int valueChosen = water.Flow(pathOptions);

                    int pathOptionChosenIndex = Array.IndexOf<int>(pathOptions, valueChosen);
                    lastPathTakenIndex = this.GetIndexOfValueChosen(pathOptionChosenIndex, lastPathTakenIndex, paths.Length);
                    pathsTaken.Add(lastPathTakenIndex + 1); //Add one to the Path because the path taken is 1 based
                }
            }
            catch (ResistanceTooHighException rthe)
            {
                didWaterFlowThroughEntireGrid = false;
            }

            return new PathResults() { PathTaken = pathsTaken, DidWaterFlowThroughEntireGrid = didWaterFlowThroughEntireGrid, TotalResistance = water.TotalResistance };
        }
 public void WaterThrowsResistanceTooHighExceptionWhenWatersResistanceIsTooHigh()
 {
     Water water = new Water();
     water.Flow(new int[] { 27, 28, 29 });
     Assert.Catch<ResistanceTooHighException>(delegate { water.Flow(new int[] { 27, 28, 29 }); });
 }
 public void GivenThreePathsWaterWillTakeThePathOfLeastResistance()
 {
     Water water = new Water();
     int path = water.Flow(2, 15, 1);
     Assert.AreEqual(1, path, "Water failed to pick the path of least resistance");
 }