Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to make a Tower of Hanoi game that means there should be N number of disk and K pegs.The Main problem is trying to change the pegs from 1st to the last one.The radius of the disk will be from 1 to N and the complete disks should be moved from the first peg to the last one(the disks should be piled in the increasing order of Radius size at every stage.
Any one having any Idea??
Posted

Have a look at my article:
Towers of Hanoi[^]
 
Share this answer
 
Hi,

C#
// The Towers Of Hanoi
// C#
// Tested under Visual Studio .NET
//

using System;

namespace Hanoi
{
  class Solution
  {
    public static void doHanoi(int n, string f, string t, string u)
    {
      if (n <= 1) 
      {
        System.Console.WriteLine(f + " --> " + t);
      } 
      else 
      {
        doHanoi(n - 1, f, u, t);
        System.Console.WriteLine(f + " --> " + t);
        doHanoi(n - 1, u, t, f);
      }
    }

    [STAThread]

    static void Main(string[] args)
    {
      if (args.Length != 1) 
      {
        System.Console.WriteLine("usage: hanoi <number>");
        System.Environment.Exit(1);
      }

      int n = System.Convert.ToInt32(args[0]);

      if ((n < 1) || (n > 10)) 
      {
        System.Console.WriteLine("usage: hanoi <number> /* 1 <= number <= 10 */");
        System.Environment.Exit(1);
      }

      doHanoi(n, "1", "3", "2");

      System.Environment.Exit(0);
    }
  }
</number></number>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900