here is the solution, it is lengthy and that is because the coordinates inputed are given as a character and a integer. I'm proud of my self, because yesterday the actuall swapping the letter function was 675 lines!
I had something like this, for each of the 25 positions, 25 options:
Code:
if ( from == 1 )
{
temp = boardArray[0][0];
if ( to == 2 ) { boardArray[0][0] = boardArray[0][1]; boardArray[0][1] = temp;}
if ( to == 3 ) { boardArray[0][0] = boardArray[0][2]; boardArray[0][2] = temp;}
.....and so on for a lot of lines
I noticed that all these followed a certain sequence which I knew could've been written easier and nicer.
And somehow I figured out a way to split it in two function totalling 86 lines!!!!
this stuff is really starting to kick in to my mind. I was on the train to school when it came to me! sorry, I'm just excited about it....
anyways...here it is
Code:
void getInput(char boardArray[][SIZE])
{
//declaring strings
char strFrom[2]; //holds swap from coordinate
char strTo[2]; //holds swap to coordinate
//declaring variables
int from = 0;
int to = 0;
cout << "Enter the coordinates of the locations to swap: ";
cin >> strFrom >> strTo;
strFrom[0] = toupper(strFrom[0]);
strTo[0] = toupper(strTo[0]);
from = matchPosition(strFrom); //function returning value 1-25 for each
//position on the board
to = matchPosition(strTo); //function returning value 1-25 for each
//position on the board
swapPosition(from, to, boardArray);
}
//=================================
int matchPosition(char pos[])
{
if( (pos[0] == 'A') && (pos[1] == '1')) { return 1;}
else if( (pos[0] == 'A') && (pos[1] == '2')) { return 2; }
else if( (pos[0] == 'A') && (pos[1] == '3')) { return 3; }
else if( (pos[0] == 'A') && (pos[1] == '4')) { return 4; }
else if( (pos[0] == 'A') && (pos[1] == '5')) { return 5; }
else if( (pos[0] == 'B') && (pos[1] == '1')) { return 6; }
else if( (pos[0] == 'B') && (pos[1] == '2')) { return 7; }
else if( (pos[0] == 'B') && (pos[1] == '3')) { return 8; }
else if( (pos[0] == 'B') && (pos[1] == '4')) { return 9; }
else if( (pos[0] == 'B') && (pos[1] == '5')) { return 10; }
else if( (pos[0] == 'C') && (pos[1] == '1')) { return 11; }
else if( (pos[0] == 'C') && (pos[1] == '2')) { return 12; }
else if( (pos[0] == 'C') && (pos[1] == '3')) { return 13; }
else if( (pos[0] == 'C') && (pos[1] == '4')) { return 14; }
else if( (pos[0] == 'C') && (pos[1] == '5')) { return 15; }
else if( (pos[0] == 'D') && (pos[1] == '1')) { return 16; }
else if( (pos[0] == 'D') && (pos[1] == '2')) { return 17; }
else if( (pos[0] == 'D') && (pos[1] == '3')) { return 18; }
else if( (pos[0] == 'D') && (pos[1] == '4')) { return 19; }
else if( (pos[0] == 'D') && (pos[1] == '5')) { return 20; }
else if( (pos[0] == 'E') && (pos[1] == '1')) { return 21; }
else if( (pos[0] == 'E') && (pos[1] == '2')) { return 22; }
else if( (pos[0] == 'E') && (pos[1] == '3')) { return 23; }
else if( (pos[0] == 'E') && (pos[1] == '4')) { return 24; }
else if( (pos[0] == 'E') && (pos[1] == '5')) { return 25; }
else { cout << "\n\nfatal error in compareAndSwap\n\n"; }
}
//==================================================
void swapPosition(int from, int to, char boardArray[][SIZE])
{
char temp;
int i;
for ( i = 1; i <= 25; i++ )
{
if(from == i)
{
if( i >= 1 && i <=5 )
{
temp = boardArray[0][i-1];
matchTo(to, boardArray, temp);
boardArray[0][i-1] = temp;
break;
}
else if( i >= 6 && i <=10 )
{
temp = boardArray[1][i-6];
matchTo(to, boardArray, temp);
boardArray[1][i-6] = temp;
break;
}
else if( i >= 11 && i <=15 )
{
temp = boardArray[2][i-11];
matchTo(to, boardArray, temp);
boardArray[2][i-11] = temp;
break;
}
else if( i >= 16 && i <=20 )
{
temp = boardArray[3][i-16];
matchTo(to, boardArray, temp);
boardArray[3][i-16] = temp;
break;
}
else if( i >= 21 && i <=25 )
{
temp = boardArray[4][i-21];
matchTo(to, boardArray, temp);
boardArray[4][i-21] = temp;
break;
}
}
}
displayTheBoard2(boardArray);
}
//=====================================================
void matchTo(char to, char boardArray[][SIZE], char& temp)
{
char temp2;
int i;
for ( i = 1; i <= 25; i++ )
{
if(to == i)
{
if( i >= 1 && i <=5 )
{
temp2 = boardArray[0][i-1];
boardArray[0][i-1] = temp;
temp = temp2;
break;
}
else if( i >= 6 && i <=10 )
{
temp2 = boardArray[1][i-6];
boardArray[1][i-6] = temp;
temp = temp2;
break;
}
else if( i >= 11 && i <=15 )
{
temp2 = boardArray[2][i-11];
boardArray[2][i-11] = temp;
temp = temp2;
break;
}
else if( i >= 16 && i <=20 )
{
temp2 = boardArray[3][i-16];
boardArray[3][i-16] = temp;
temp = temp2;
break;
}
else if( i >= 21 && i <=25 )
{
temp2 = boardArray[4][i-21];
boardArray[4][i-21] = temp;
temp = temp2;
break;
}
}
}
}
what do you guys think?
axon