Tower Hanoi (recursive) in Processing

I wrote this recursive code for Hanoi tower, but it just draw the first and last position.
I don’t understand why I cannot see each step

please format code with </> button * homework policy * asking questions

<!-- . You may delete this befo`int col, ring;

int h=50;
int w=100;
int size=3;
int flag=0;
int[][] Array = { {1,2,3}, {0,0,0}, {0,0,0 } };
int[] Top={ 0, -1, 2, 2};
void setup() {
size(1200,500);
// Array[0][0]=1;
// Array[1][0]=2;
// Array[2][0]=3;
// Array[0][1]=0;
// Array[1][1]=0;
// Array[2][1]=0;
// Array[0][2]=0;
// Array[1][2]=0;
// Array[2][2]=0;

// draw_hanoi();
//hanoi(3,1,2,3);

}

void draw() {

if (flag==0)
{
draw_hanoi();
hanoi(3,1,2,3);
flag=1;
}

}

void draw_hanoi() {
fill(51);
rect(0,0,1200,500);
fill(200);
for (col=0;col<3;col++) {
for (ring=(size-1);ring>=0;ring–) {
if (Array[col][ring]!=0) {
rect(150+300*col+(size-ring-1)50,h(ring+1),(Array[col][ring])*w,h);
}
}
}
delay(100);
}

void hanoi(int n, int from_t,int to_t, int via_t)
{
if(n>0) {
hanoi(n-1,from_t,via_t,to_t);
Top[from_t]=Top[from_t]+1;
Array[to_t-1][Top[to_t]]=Array[from_t-1][Top[from_t]];
Top[to_t]=Top[to_t]-1;
Array[from_t-1][Top[from_t]]=0;
draw_hanoi();
// delay(2000);
hanoi(n-1,via_t,to_t,from_t);
}
}`re posting. →

Hello, @dyehuda, and welcome to the Processing Forum!

Your code is not formatted for posting, so it is difficult to read and to copy for testing.

See Guidelines—Asking Questions for information on formatting code for posting.

1 Like