- std::cout << "************************************" << std::endl;
Since you have included
this can be simplified to
- cout << "************************************" << endl;
This
- if (col == 'a' || col == 'b' || col == 'c' || col == 'd' || col == 'e' || col == 'f' || col == 'g' || col == 'h' || col == 'A' || col == 'B' || col == 'C' || col == 'D' || col == 'E' || col == 'F' || col == 'G' || col == 'H') {
-
if (row == '1' || row == '2' || row == '3' || row == '4' || row == '5' || row == '6' || row == '7' || row == '8')
-
return true;
can be simplified to
- if (tolower(col) >= 'a' && tolower(col) <= 'h' && row >= '1' && row <= '8')
-
return true;
Here is your problem
- if (board[translateColumn(command[0])][command[1]] != "0")
You translate the first character of column from letters to integers but you do not translate the second character, you use it directly. However command[1] does not have the value in the range 0 - 7 which is what is required to be in range for the variable board, it has the range '1' - '8' which is decimal is 49 - 56, well out of range. You need a translateRow function in the same way you have a translate column.
This error appears multiple times in the function
processCoord
and finally this
- int translateColumn(char letter)
-
{
-
int col;
-
-
if (letter == 'a' || letter == 'A')
-
col = 1;
-
if (letter == 'b' || letter == 'B')
-
col = 2;
-
if (letter == 'c' || letter == 'C')
-
col = 3;
-
if (letter == 'd' || letter == 'D')
-
col = 4;
-
if (letter == 'e' || letter == 'E')
-
col = 5;
-
if (letter == 'f' || letter == 'F')
-
col = 6;
-
if (letter == 'g' || letter == 'G')
-
col = 7;
-
if (letter == 'h' || letter == 'H')
-
col = 8;
-
-
return col;
-
}
also has an error in it, it returns a value between 1 and 8 however this is used as an index into the board array. Since this array is declared
string board[8][8] the index needs to be in the range 0 - 7. Additionally this could be written as
- int translateColumn(char letter)
-
{
-
return tolower(letter) - 'a';
-
}
To use the function
tolower you need to include the header ctype.h