/// <summary> /// Creates towers with no disks at start. /// </summary> /// <param name="currentTower">Name of empty towers (at start).</param> private void CreateOtherTower(HanoiDisk[] currentTower) { indexInt = 0; while (indexInt < currentTower.Length) { currentTower[indexInt] = new HanoiDisk(0); indexInt++; } }
/// <summary> /// Updates the contents of each array for when disks are moved. /// </summary> /// <param name="source">The tower a disk is comming off of.</param> /// <param name="destination">The tower a disk is being added to.</param> private void UpdateArray(HanoiDisk[] source, HanoiDisk[] destination) { int sourceIndex = GetTopDisk(source); int destinationIndex = GetBottomBlank(destination); tempDisk = destination[destinationIndex]; destination[destinationIndex] = source[sourceIndex]; source[sourceIndex] = tempDisk; }
/// <summary> /// Creates tower with all the disks at start. /// </summary> /// <param name="currentTower">Name of full tower (at start).</param> private void CreateStartTower(HanoiDisk[] currentTower) { indexInt = numberOfDisksInt; while (indexInt > 0) { currentTower[indexInt - 1] = new HanoiDisk(indexInt, numberOfDisksInt); indexInt--; } }
/// <summary> /// Creates tower with all the disks at start. /// </summary> /// <param name="currentTower">Name of full tower (at start).</param> private void CreateStartTower(HanoiDisk[] currentTower) { indexInt = numberOfDisksInt; while (indexInt > 0) { currentTower[indexInt - 1] = new HanoiDisk(indexInt); indexInt--; } }
/// <summary> /// Method that actually solves the problem. /// If n == 1, then updates the towers (visually) and displays result. /// Otherwise, recursively calls itself 3 times to take a step further into problem. /// </summary> /// <param name="n">Current n. Initially is the total number of disks.</param> /// <param name="source">The "source" tower. Initally is the tower with disks, or first tower.</param> /// <param name="auxilary">The "auxilary" tower. Initially is the first empty tower.</param> /// <param name="destination">the "destination" tower. Initially is the second empty tower.</param> private void MoveDisk(int n, HanoiDisk[] source, HanoiDisk[] auxilary, HanoiDisk[] destination) { // Base Case to exit recursive loop. if (n == 1) { Task.Delay(waitTimerInt).Wait(); UpdateArray(source, destination); DisplayAllTowers(); } else { // Attempt to move (n-1) from source location to auxilary location, using destination as temp. MoveDisk(n - 1, source, destination, auxilary); // Move source to destination. MoveDisk(1, source, auxilary, destination); // Attempt to move (n-1) from auxilary location to destination location, using source as temp. MoveDisk(n-1, auxilary, source, destination); } }
/// <summary> /// Determines the index of the top-most disk in specified tower. /// </summary> /// <param name="currentTower">The tower to determine the disk index of.</param> /// <returns>The index which matches the top disk in the tower.</returns> private int GetTopDisk(HanoiDisk[] currentTower) { indexInt = 0; while (indexInt < numberOfDisksInt - 1) { if (currentTower[indexInt].DiskPresent) { return indexInt; } indexInt++; } return indexInt; }