コード例 #1
0
        private void StartReplacement(int size)
        {
            Disk[] disks = new Disk[size];
            int    num   = size;

            for (int i = 0; i < size; i++)
            {
                disks[i] = new Disk(num);
                num--;
            }

            A = new Stick('A', disks);
            B = new Stick(size, 'B');
            C = new Stick(size, 'C');

            DoTowers(size, A, B, C);
        }
コード例 #2
0
 private void DoTowers(int topN, Stick from, Stick inner, Stick to)
 {
     if (topN == 1)
     {
         if (writeActions)
         {
             Console.WriteLine("Disk 1 from " + from + " to " + to);
         }
         var disk = from.TakeDisk();
         to.PlaceDisk(disk);
     }
     else
     {
         DoTowers(topN - 1, from, to, inner);
         if (writeActions)
         {
             Console.WriteLine("Disk " + topN + " from " + from + " to " + to);
         }
         var disk = from.TakeDisk();
         to.PlaceDisk(disk);
         DoTowers(topN - 1, inner, from, to);
     }
 }