public static void MoveDisks(int height, TowerOfHanoi originTower, TowerOfHanoi bufferTower, TowerOfHanoi destinationTower) { if (height > 0) { MoveDisks(height - 1, originTower, destinationTower, bufferTower); Console.WriteLine("Moving disk {0} from tower {1} to tower {2}", height, originTower.PoleIndex, destinationTower.PoleIndex); MoveTopTo(originTower, destinationTower); MoveDisks(height - 1, bufferTower, originTower, destinationTower); } }
// Each disk is represented by an int denoting its size. public static void AddToTower(TowerOfHanoi tower, int disk) { if (tower.Disks.Count != 0 && tower.Disks.Peek() <= disk) { Console.WriteLine("Error adding disk {0}", disk); } else { tower.Disks.Push(disk); } }
public static void MoveTopTo(TowerOfHanoi origin, TowerOfHanoi tower) { if (origin.Disks.Count != 0) { int topDisk = origin.Disks.Pop(); AddToTower(tower, topDisk); Console.WriteLine("Moving disk {0} from Tower {1} to Tower {2}", topDisk, origin.PoleIndex, tower.PoleIndex); } }