Ejemplo n.º 1
0
        private static void Recursion(int n)
        {
            Stack <int>[] towers = new Stack <int> [3];
            for (int i = 0; i < towers.Length; i++)
            {
                towers[i] = new Stack <int>();
            }

            for (int i = n; i > 0; i--)
            {
                towers[0].Push(i);
            }

            TowersOfHanoi.MoveTower(towers, 0, 2, 1, n);

            Assert.IsTrue(towers[0].IsEmpty);
            Assert.IsTrue(towers[1].IsEmpty);
            Assert.IsFalse(towers[2].IsEmpty);

            for (int i = 1; i <= n; i++)
            {
                int ring = towers[2].Pop();
                Assert.AreEqual(i, ring);
            }
        }
Ejemplo n.º 2
0
 public void TowersOfHanoiTest()
 {
     for (int i = 1; i <= 7; i++)
     {
         TowersOfHanoi.Recursion(i);
     }
 }
Ejemplo n.º 3
0
 private static void MoveTower(Stack <int>[] towers, int from, int to, int other, int depth)
 {
     if (depth > 0)
     {
         TowersOfHanoi.MoveTower(towers, from, other, to, depth - 1);
         towers[to].Push(towers[from].Pop());
         TowersOfHanoi.MoveTower(towers, other, to, from, depth - 1);
     }
 }