Пример #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Please enter number of discs[3-10]: ");
            int numOfDiscs = Int32.Parse(Console.ReadLine());

            Console.WriteLine("Loading simulation...");
            HanoiTowers ht1 = new HanoiTowers(numOfDiscs);

            //Console.WriteLine($"Towers before the play:\n{ht1}");
            HanoiTowersSimulator.Play(ht1);
            //Console.WriteLine($"Towers after the play:\n{ht1}");
        }
Пример #2
0
 public static void Play(HanoiTowers ht)
 {
     amount  = ht.GetNumOfDiscs();
     counter = 0;
     //Console.Clear();
     //Console.WriteLine(ht);
     if (HanoiTowersMove(ht, 0, 1, amount))
     {
         if (ht.IsEmpty(0) && ht.IsEmpty(2) && ht.GetNumOfDiscs(1) == amount)
         {
             Console.WriteLine("Successful Hanoi Towers solution");
             return;
         }
     }
     Console.WriteLine($"Wrong Hanoi Towers solution after {counter} movements");
 }
Пример #3
0
        private static bool HanoiTowersMove(HanoiTowers ht, int from, int to, int count)
        {
            if (count == 1)
            {
                counter++;
                bool check = ht.MoveDisc(from, to);
                //Thread.Sleep(500);
                //Console.SetCursorPosition(0, 0);
                //Console.WriteLine(ht);
                return(check);
            }

            int help = 3 - from - to;

            if (!HanoiTowersMove(ht, from, help, count - 1))
            {
                return(false);
            }
            if (!HanoiTowersMove(ht, from, to, 1))
            {
                return(false);
            }
            return(HanoiTowersMove(ht, help, to, count - 1));
        }