Last active
October 20, 2021 07:04
-
-
Save SudiDav/1561d1589f3ed255ba52b6c372f13921 to your computer and use it in GitHub Desktop.
Tower Of Hanoi game using recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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