I'm still a beginner in C# and I would like some help. My function works for 3 disks but from 4 it no longer works and I don't understand why. I hope someone could help me.
`public static string HanoiTower(int n)
{
string res = "Moves for " + n.ToString() + " disks\n";
string depart = "1";
string aux = "2";
string dest = "3";
string lDisk = "1";
if (n % 2 == 0)
{
dest = aux;
aux = "3";
}
for (int i = 1; i < Lessons.MyPow(2, n); i++)
{
if (i % 3 == 1)
{
if (lDisk == dest)
{
res += dest + " -> " + depart + "\n";
lDisk = depart;
}
else
{
if (lDisk == depart)
lDisk = dest;
res += depart + " -> " + dest + "\n";
}
}
else if (i % 3 == 2)
{
if (lDisk == aux)
{
res += aux + " -> " + depart + "\n";
lDisk = depart;
}
else
{
if (lDisk == depart)
lDisk = aux;
res += depart + " -> " + aux + "\n";
}
}
else
{
if (lDisk == dest)
{
res += dest + " -> " + aux + "\n";
lDisk = aux;
}
else
{
if (lDisk == aux)
lDisk = dest;
res += aux + " -> " + dest + "\n";
}
}
}
return res.Substring(0,res.Length-1);
}`
I've seen people on the internet using stacks but I don't know how to use them and I'd like to avoid it for now so preferably don't offer me solutions with that
The logic to determine the source and target stacks is not very complex, but not as simple as you have in your code.
You rightly started by dealing with the parity of the .
Other observations are that the interval at which a disc gets to move is a power of 2. The smallest disc moves every 2 turns (starting at move 1), the one-but-smallest disc moves every 4 turns (starting at move 2), ...etc.
The direction of the move is again ruled by parity.
Here is your code adapted to apply that logic: