Schultz's profileSchultzpacePhotosBlogNetwork Tools Help
    May 02

    Tower of Hanoi in C#

    using System;

    using System.Collections.Generic;

    using System.Text;

     

    namespace Hanoi

    {

        class Program

        {

            static void Main(string[] args)

            {

                int count = 3;

                if (args.Length != 0 && !Int32.TryParse(args[0], out count))

                {

                    count = 3;

                }

     

                for (int i = 0; i < 3; i++)

                {

                    pinsAndDisks[i] = new Stack<int>();

                }

     

                for (int i = 0; i < count; i++)

                {

                    pinsAndDisks[0].Push(count - i);

                }

     

                Hanoi(0, 2, 1, count);

     

                Console.WriteLine("Total number of calls: {0}.", depth);

            }

     

     

            static int depth;

            static Stack<int>[] pinsAndDisks = new Stack<int>[3];

     

            static void Hanoi(int source, int dest, int misc, int count)

            {

                depth++;

     

                if (count == 1)

                {

                    MoveAndPrint(source, dest);

                }

                else

                {

                    Hanoi(source, misc, dest, count - 1);

                    MoveAndPrint(source, dest);

                    Hanoi(misc, dest, source, count - 1);

                }

            }

     

            static void MoveAndPrint(int source, int dest)

            {

                int disk = pinsAndDisks[source].Pop();

                pinsAndDisks[dest].Push(disk);

                for (int pin = 0; pin < 3; pin++)

                {

                    if (pinsAndDisks[pin].Count == 0)

                    {

                        Console.WriteLine("|-----");

                    }

                    else

                    {

                        string pinView = "";

                        Stack<int>.Enumerator e = pinsAndDisks[pin].GetEnumerator();

                        while (e.MoveNext())

                        {

                            pinView = e.Current + "-" + pinView;

                        }

                        Console.WriteLine("|-" + pinView);

                    }

                }

                Console.WriteLine();

            }

        }

    }

     

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    ARSINEwrote:
    Thank you very very much for the code Schult !
    The result is great ! but I didn't actually understand the whole code ..
    Can you write comment statements in each step please ?
    Thanks again really .. good luck .
    May 9

    Trackbacks

    The trackback URL for this entry is:
    http://schultz9999.spaces.live.com/blog/cns!CF947AA877D8D439!242.trak
    Weblogs that reference this entry
    • None