# Can't Find Problem In Coding

• January 18th, 2012, 10:35 PM
ulvak
Can't Find Problem In Coding
I can't find the problem with this coding. I'm trying to make the tower of hanoi. I know I don't have the stop movement if a higher # is put on a lower #. (console Application) Please help.

#include <iostream>
#include <iomanip>
//#include <SFML/Graphics.hpp>

using namespace std;

void move(int movefrom, int moveto, int movefromspot, int movetospot);
bool Checkwin();
void Draw();
int MoveSpotFrom(int move);
int MoveSpotTo(int move);

int disks;
int Left[100000];
int Middle[100000];
int Right[100000];

int main()
{
int movefrom, moveto, movefromspot, movetospot;
cout << "How many Disks?" << endl;
cin >> disks;
for (int a=disks-1; a>-1; a--)
{
Left[a]=disks-a;
Middle[a]=0;
Right[a]=0;
}
while(!Checkwin())
{
Draw();
cout << "Move from what tower to what tower?" << endl;
cin >> movefrom >> moveto;
movefromspot=MoveSpotFrom(movefrom);
movetospot=MoveSpotTo(moveto);
cout << movefromspot << " " << movetospot << " " << Left[movefromspot] << " " << Middle[movetospot] << endl;
if((movefrom != moveto) && (movefromspot != -1) && (movetospot != -1))
move(movefrom, moveto, movefromspot, movetospot);
}
}

void move(int movefrom, int moveto, int movefromspot, int movetospot)
{
switch(movefrom)
{
case 1:
{
switch(moveto)
{
case 2:
{
Middle[movetospot] = Left[movefromspot];
Left[movefromspot] = 0;
}
case 3:
{
Right[movetospot] = Left[movefromspot];
Left[movefromspot] = 0;
}
}
}
case 2:
{
switch(moveto)
{
case 1:
{
Left[movetospot] = Middle[movefromspot];
Middle[movefromspot] = 0;
}
case 3:
{
Right[movetospot] = Middle[movefromspot];
Middle[movefromspot] = 0;
}
}
}
case 3:
{
switch(moveto)
{
case 1:
{
Left[movetospot] = Right[movefromspot];
Right[movefromspot] = 0;
}
case 2:
{
Middle[movetospot] = Right[movefromspot];
Right[movefromspot] = 0;
}
}
}
}
}

bool Checkwin()
{
for (int a=disks-1; a>-1; a--)
{
if(Right[a]!=disks-a+1)
return false;
}
return true;
}

void Draw()
{
cout << endl;
for (int a=disks; a>-1; a--)
{
cout << setw(20) << Left[a] << setw(10) << Middle[a] << setw(10) << Right[a] << endl;
}
cout << setw(20) << "-" << setw(10) << "-" << setw(10) << "-" << endl << setw(20) << 1 << setw(10) << 2 << setw(10) << 3 << endl;
}

int MoveSpotFrom(int move)
{
switch(move)
{
case 1:
{
for (int a=disks; a>-1; a--)
{
if(Left[a]!=0)
return a;
}
return disks;
}
case 2:
{
for (int a=disks; a>-1; a--)
{
if(Middle[a]!=0)
return a;
}
return disks;
}
case 3:
{
for (int a=disks; a>-1; a--)
{
if(Right[a]!=0)
return a;
}
return disks;
}
default:
return -1;
}
}

int MoveSpotTo(int move)
{
switch(move)
{
case 1:
{
for (int a=0; a<disks; a++)
{
if(Left[a]==0)
return a;
}
}
case 2:
{
for (int a=0; a<disks; a++)
{
if(Middle[a]==0)
return a;
}
}
case 3:
{
for (int a=0; a<disks; a++)
{
if(Right[a]==0)
return a;
}
}
default:
return -1;
}
}
• January 19th, 2012, 02:09 AM
monarch_dodra
Re: Can't Find Problem In Coding
Quote:

Originally Posted by ulvak
I can't find the problem with this coding

Welcome to the forum. You can help others help you by using [CODE][/CODE] tags.

Code:

```#include <iostream> #include <iomanip> //#include <SFML/Graphics.hpp> using namespace std; void move(int movefrom, int moveto, int movefromspot, int movetospot); bool Checkwin(); void Draw(); int MoveSpotFrom(int move); int MoveSpotTo(int move); int disks; int Left[100000]; int Middle[100000]; int Right[100000]; int main() {     int movefrom, moveto, movefromspot, movetospot;     cout << "How many Disks?" << endl;     cin >> disks;     for (int a=disks-1; a>-1; a--)     {         Left[a]=disks-a;         Middle[a]=0;         Right[a]=0;     }     while(!Checkwin())     {         Draw();         cout << "Move from what tower to what tower?" << endl;         cin >> movefrom >> moveto;         movefromspot=MoveSpotFrom(movefrom);         movetospot=MoveSpotTo(moveto);         cout << movefromspot << "  " << movetospot << "  " << Left[movefromspot] << "  " << Middle[movetospot] << endl;         if((movefrom != moveto) && (movefromspot != -1) && (movetospot != -1))         move(movefrom, moveto, movefromspot, movetospot);     } } void move(int movefrom, int moveto, int movefromspot, int movetospot) {     switch(movefrom)     {         case 1:         {             switch(moveto)             {                 case 2:                 {                     Middle[movetospot] = Left[movefromspot];                     Left[movefromspot] = 0;                 }                 case 3:                 {                     Right[movetospot] = Left[movefromspot];                     Left[movefromspot] = 0;                 }             }         }         case 2:         {             switch(moveto)             {                 case 1:                 {                     Left[movetospot] = Middle[movefromspot];                     Middle[movefromspot] = 0;                 }                 case 3:                 {                     Right[movetospot] = Middle[movefromspot];                     Middle[movefromspot] = 0;                 }             }         }         case 3:         {             switch(moveto)             {                 case 1:                 {                     Left[movetospot] = Right[movefromspot];                     Right[movefromspot] = 0;                 }                 case 2:                 {                     Middle[movetospot] = Right[movefromspot];                     Right[movefromspot] = 0;                 }             }         }     } } bool Checkwin() {     for (int a=disks-1; a>-1; a--)     {         if(Right[a]!=disks-a+1)         return false;     }     return true; } void Draw() {     cout << endl;     for (int a=disks; a>-1; a--)     {         cout << setw(20) << Left[a] << setw(10) << Middle[a] << setw(10) << Right[a] << endl;     }     cout << setw(20) << "-" << setw(10) << "-" << setw(10) << "-" << endl << setw(20) << 1 << setw(10) << 2 << setw(10) << 3 << endl; } int MoveSpotFrom(int move) {     switch(move)     {         case 1:         {             for (int a=disks; a>-1; a--)             {                 if(Left[a]!=0)                 return a;             }             return disks;         }         case 2:         {             for (int a=disks; a>-1; a--)             {                 if(Middle[a]!=0)                 return a;             }             return disks;         }         case 3:         {             for (int a=disks; a>-1; a--)             {                 if(Right[a]!=0)                 return a;             }             return disks;         }         default:         return -1;     } } int MoveSpotTo(int move) {     switch(move)     {         case 1:         {             for (int a=0; a<disks; a++)             {                 if(Left[a]==0)                 return a;             }         }         case 2:         {             for (int a=0; a<disks; a++)             {                 if(Middle[a]==0)                 return a;             }         }         case 3:         {             for (int a=0; a<disks; a++)             {                 if(Right[a]==0)                 return a;             }         }         default:         return -1;     } }```