# help make my program recursive

Discussion in 'Programming' started by tfarmer4, Oct 25, 2010.

1. ### tfarmer4New Member

Joined:
Oct 25, 2010
Messages:
1
0
Trophy Points:
0
I am writing a program that will verify that a given set of moves on the Towers of Hanoi will end with a solution. I have written the code below for a 3 disk game, but I can't figure out how to make this code recursive. It is a really long piece of code considering its only for 3 disks. I'm pretty sure there is a way to make it recursive, but I am just stumped. Help will be greatly appreciated.

Code:
```#include <stdio.h>
int tower1[3] = {1,2,3};
int tower2[3] = {0,0,0};
int tower3[3] = {0,0,0};
int moves[2][7] = {{1,1,2,1,3,3,1},
{2,3,3,2,1,2,2}}; //set of moves

//function to move the disks
void moveDisk(int towerFrom, int towerTo)
{
int temp = 0;
int i = 0;
int k = 2;

if(towerFrom == 1 && towerTo == 2) //take top disc from tower 1 and place on tower 2
{
while(i < 3)
{
if(tower1[i] != 0)
{
temp = tower1[i];
tower1[i] = 0;
break;
}//end if
else
i++;
}
while(k >= 0)
{
if(temp > tower2[k+1] && k <= 1)
{
printf("Invalid move.");
exit();
}
if(tower2[k] == 0)
{
tower2[k] = temp;
break;
}
else
k--;
}
}

if(towerFrom == 1 && towerTo == 3)//take top disk from tower 1 and move to tower 3
{
i=0;
k=2;
while(i < 3)
{
if(tower1[i] != 0)
{
temp = tower1[i];
tower1[i] = 0;
break;
}//end if
else
i++;
}
while(k >= 0)
{
if(temp > tower3[k+1] && k <= 1)
{
printf("Invalid move.");
exit();
}
if(tower3[k] == 0)
{
tower3[k] = temp;
break;
}
else
k--;
}
}

if(towerFrom == 2 && towerTo == 3)//take top disk from tower 2 and place on tower 3
{
i=0;
k=2;
while(i < 3)
{
if(tower2[i] != 0)
{
temp = tower2[i];
tower2[i] = 0;
break;
}//end if
else
i++;
}
while(k >= 0)
{
if(temp > tower3[k+1] && k <= 1)
{
printf("Invalid move.");
exit();
}
if(tower3[k] == 0)
{
tower3[k] = temp;
break;
}
else
k--;
}
}
if(towerFrom == 2 && towerTo == 1)//take top disk from tower 2 and place on tower 1
{
i=0;
k=2;
while(i < 3)
{
if(tower2[i] != 0)
{
temp = tower2[i];
tower2[i] = 0;
break;
}//end if
else
i++;
}
while(k >= 0)
{
if(temp > tower1[k+1] && k <= 1)
{
printf("Invalid move.");
exit();
}

if(tower1[k] == 0)
{
tower1[k] = temp;
break;
}

else
k--;
}
}
if(towerFrom == 3 && towerTo == 1)//take top disk from tower 3 and place on tower 1
{
i=0;
k=2;
while(i < 3)
{
if(tower3[i] != 0)
{
temp = tower3[i];
tower3[i] = 0;
break;
}//end if
else
i++;
}
while(k >= 0)
{
if(temp > tower1[k+1] && k <= 1)
{
printf("Invalid move.");
exit();
}
if(tower1[k] == 0)
{
tower1[k] = temp;
break;
}
else
k--;
}
}
if(towerFrom == 3 && towerTo == 2)//take top disk off tower 3 and place on tower 2
{
i=0;
k=2;
while(i < 3)
{
if(tower3[i] != 0)
{
temp = tower3[i];
tower3[i] = 0;
break;
}//end if
else
i++;
}
while(k >= 0)
{
if(temp > tower2[k+1] && k <= 1)
{
printf("Invalid move.");
exit();
}
if(tower2[k] == 0)
{
tower2[k] = temp;
break;
}
else
k--;
}
}
}

void main()
{
moveDisk(moves[0][0], moves[1][0]);
system("pause");
printf("%d %d %d \n", tower1[0], tower2[0], tower3[0]);
printf("%d %d %d \n", tower1[1], tower2[1], tower3[1]);
printf("%d %d %d \n", tower1[2], tower2[2], tower3[2]);
moveDisk(moves[0][1], moves[1][1]);
system("pause");
printf("%d %d %d \n", tower1[0], tower2[0], tower3[0]);
printf("%d %d %d \n", tower1[1], tower2[1], tower3[1]);
printf("%d %d %d \n", tower1[2], tower2[2], tower3[2]);
moveDisk(moves[0][2], moves[1][2]);
system("pause");
printf("%d %d %d \n", tower1[0], tower2[0], tower3[0]);
printf("%d %d %d \n", tower1[1], tower2[1], tower3[1]);
printf("%d %d %d \n", tower1[2], tower2[2], tower3[2]);
moveDisk(moves[0][3], moves[1][3]);
system("pause");
printf("%d %d %d \n", tower1[0], tower2[0], tower3[0]);
printf("%d %d %d \n", tower1[1], tower2[1], tower3[1]);
printf("%d %d %d \n", tower1[2], tower2[2], tower3[2]);
moveDisk(moves[0][4], moves[1][4]);
system("pause");
printf("%d %d %d \n", tower1[0], tower2[0], tower3[0]);
printf("%d %d %d \n", tower1[1], tower2[1], tower3[1]);
printf("%d %d %d \n", tower1[2], tower2[2], tower3[2]);
moveDisk(moves[0][5], moves[1][5]);
system("pause");
printf("%d %d %d \n", tower1[0], tower2[0], tower3[0]);
printf("%d %d %d \n", tower1[1], tower2[1], tower3[1]);
printf("%d %d %d \n", tower1[2], tower2[2], tower3[2]);
moveDisk(moves[0][6], moves[1][6]);
system("pause");
printf("%d %d %d \n", tower1[0], tower2[0], tower3[0]);
printf("%d %d %d \n", tower1[1], tower2[1], tower3[1]);
printf("%d %d %d \n", tower1[2], tower2[2], tower3[2]);
};```

Joined:
Nov 24, 2009
Messages:
387