Skip to content

Instantly share code, notes, and snippets.

@SudiDav
Last active October 20, 2021 07:04
Show Gist options
  • Save SudiDav/1561d1589f3ed255ba52b6c372f13921 to your computer and use it in GitHub Desktop.
Save SudiDav/1561d1589f3ed255ba52b6c372f13921 to your computer and use it in GitHub Desktop.
Tower Of Hanoi game using recursion
using System;
namespace TowerOfHanoi
{
internal class Program
{
static int counter = 0;
static void Main(string[] args)
{
var discs = 0;
Console.WriteLine("============================Sudi David==============================");
Console.WriteLine("Please enter the number of discs: ");
discs = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your combinations are :");
Tower(discs,1,2,3);
Console.ReadLine();
}
static void Tower(int n, int SourcePeg, int destinationPeg, int sparePeg)
{
if (n==1)
{
Console.WriteLine(counter + " " + SourcePeg + "->" + destinationPeg);
counter++;
}
else
{
Tower(n-1, SourcePeg, sparePeg, destinationPeg);
Console.WriteLine(counter + " " + SourcePeg + "->" + destinationPeg);
counter++;
Tower(n - 1, sparePeg ,destinationPeg, SourcePeg);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment