+ 1

Why is this code for tower of hanoi not working in Unity?

The code is of a function that should show how to transfer discs to solve tower of hanoi problem for any number of discs up to 64.(when called with StartCoroutine) [S, A and D work as the points where the discs should be transferred. Ob is an array of gameobjects, which in this case, are cylindars (or discs).] However, the code moves only two discs, the top one to A, and the second top one to D. Why ain't it working? What should be done to make the code work? https://code.sololearn.com/canX8c4R500q/?ref=app

30th Apr 2018, 7:49 PM
1604064_Sharif
1604064_Sharif - avatar
16 Respuestas
+ 3
The numbers in my code are just to see what is printed from which recursion call, not which disk is being moved. You can ignore those numbers. The disk that is being moved is the current one on the top of the rod. So, 'A to C'. If the 3 disks start on rod A. We just moved the top disk from rod A to rod C. I've editted my code to print the Current disk too, where disk 1 is the smallest and disk 3 is the largest. Have a look at it now¿ (See the numbers after "Disk:") In your program, is disc[disc.length() - 1] the smallest disk? Try ordering the array from largest to smallest.
1st May 2018, 7:52 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
I'm not sure what the variable 'i' is so I'll assume it is meant to be n. This looks like an infinite loop. yield return null does not exit the coroutine early. Use yield break instead. Or, wrap an else statement over the rest of the code. Have a look at an algorithm I modified from Wikipedia, and the modification I made to your algorithm. See if the 'After' version I gave fixes the issues. https://code.sololearn.com/cv8yW71t6RZ9/?ref=app
1st May 2018, 3:16 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
1 to n
1st May 2018, 8:34 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
If you use n <= 0, then also do: disc[n].transform.position = D; Instead of: disc[n - 1].transform.position = D; n should also start off as 2 rather than 3.
1st May 2018, 8:38 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Good job! Pesky errors always lurk in unexpected places 😅
3rd May 2018, 4:41 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
Well, the primary problem was with how I re ursively called hanoi function. I should have called by StartCoroutine(hanoi()) instead of just hanoi(). But there are still a few problems left. Each time, the smallest disc moves with the rest for no apparent reason.
1st May 2018, 7:14 PM
1604064_Sharif
1604064_Sharif - avatar
+ 1
i'm just doing disc[0] instead. because that's the actual disc to move.
1st May 2018, 8:42 PM
1604064_Sharif
1604064_Sharif - avatar
+ 1
Are the array and transforms positioned correctly? At the start, is the bottom disk (#2), in the 2nd position in the array? and the top disk (#0) at the 0th position.
1st May 2018, 8:46 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
Rrestoring faith, the hanoi part of the code finally worked. The problem was way above what I was thinking. I declared the value of Source as Destination and vice versa by mistake. I used "Geek for Geeks" recursive algorithm and modified for my program.
3rd May 2018, 3:05 PM
1604064_Sharif
1604064_Sharif - avatar
0
ok... n, not i. my bad. I had to rewrite the whole code. And, thanks. I'll tell you more after I find the results.
1st May 2018, 4:42 PM
1604064_Sharif
1604064_Sharif - avatar
0
I'm not sure how IEnumerator works with recursion, but it seems either only disc no. "n" moves, or only disc no. "n" and "n-1" moves depending on the changes I make in the code.
1st May 2018, 6:19 PM
1604064_Sharif
1604064_Sharif - avatar
0
Rrestoring faith, your code seems to have some problems. On the first move, it says to move disk 1 which should be 2nd disk if the disks are numbered (0, 1, 2).
1st May 2018, 7:33 PM
1604064_Sharif
1604064_Sharif - avatar
0
was your code for disk no, 1 to n, or 0 to n-1?
1st May 2018, 8:11 PM
1604064_Sharif
1604064_Sharif - avatar
0
so, any changes other than replacing if(n<=1) with if(n<=0) ?
1st May 2018, 8:35 PM
1604064_Sharif
1604064_Sharif - avatar
0
because, in my case, sometimes the top disk is moving to where it was, and sometimes middle discs start moving (probably because the upper disks should have moved elsewhere)
1st May 2018, 8:39 PM
1604064_Sharif
1604064_Sharif - avatar
0
in my case, there are 5 disks. so, the 5th disk is in 5th position, disc[4]
1st May 2018, 8:50 PM
1604064_Sharif
1604064_Sharif - avatar