Hi..i am new to c++..i wanted to limit the chesspiece to be able to move 1-tile adjcent to their current position..but my code does not work at all..can anyone give me any pointers?..thx
const int row = 4;
const int column = 3;
int i, j;
void printBoard(char chessboard[row][column]);
void linearSearchRow (char chessboard[row][column], char target);
void linearSearchCol umn(char chessboard[row][column], char target);
void move(char chessboard[row][column], string piece, char target);
int main()
{
char chessboard[row][column] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '},
{'a', 'b', 'c'}
};
move(chessboard , "assasin", 'a');
printBoard(ches sboard);
}
int linearSearchRow (char chessboard[row][column], char target)
{
for (i = 0; i < row; i++)
for(j = 0; j < column; j++)
if(chessboard[i][j] == target)
return i;
return -1;
}
int linearSearchCol umn(char chessboard[row][column], char target)
{
for (i = 0; i < row; i++)
for(j = 0; j < column; j++)
if(chessboard[i][j] == target)
return j;
return -1;
}
void move(char chessboard[row][column], string piece, char target)
{
int result_r, result_c, new_r, new_c;
do
{
cout << "\nIt is your " << piece << "'s turn\n";
cout << "Where do you wish to move it(row): ";
cin >> r;
cout << "Where do you wish to move it(column): ";
cin >> c;
r = r - 1;
c = c - 1;
new_r = linearSearchRow (chessboard, target);
new_c = linearSearchCol umn(chessboard, target);
result_r = new_r - r;
result_c = new_c - c;
if(result_r <= 1 && result_c <= 1)
chessboard[r][c] = target;
else
cout << "You cannot move there!" << endl;
}while(result_r > 1 || result_c > 1);
}
void printBoard(char chessboard[row][column])
{
cout << "\t Column";
cout << "\n\t 1 2 3 ";
for(i = 0; i < row; i++)
{
cout << "\nRow" << i + 1;
cout << "\t - - -";
cout << endl << "\t";
for(j = 0; j < column; j++)
cout << "|" << chessboard[i][j] << "|";
cout << "\n\t - - -";
}
}
const int row = 4;
const int column = 3;
int i, j;
void printBoard(char chessboard[row][column]);
void linearSearchRow (char chessboard[row][column], char target);
void linearSearchCol umn(char chessboard[row][column], char target);
void move(char chessboard[row][column], string piece, char target);
int main()
{
char chessboard[row][column] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '},
{'a', 'b', 'c'}
};
move(chessboard , "assasin", 'a');
printBoard(ches sboard);
}
int linearSearchRow (char chessboard[row][column], char target)
{
for (i = 0; i < row; i++)
for(j = 0; j < column; j++)
if(chessboard[i][j] == target)
return i;
return -1;
}
int linearSearchCol umn(char chessboard[row][column], char target)
{
for (i = 0; i < row; i++)
for(j = 0; j < column; j++)
if(chessboard[i][j] == target)
return j;
return -1;
}
void move(char chessboard[row][column], string piece, char target)
{
int result_r, result_c, new_r, new_c;
do
{
cout << "\nIt is your " << piece << "'s turn\n";
cout << "Where do you wish to move it(row): ";
cin >> r;
cout << "Where do you wish to move it(column): ";
cin >> c;
r = r - 1;
c = c - 1;
new_r = linearSearchRow (chessboard, target);
new_c = linearSearchCol umn(chessboard, target);
result_r = new_r - r;
result_c = new_c - c;
if(result_r <= 1 && result_c <= 1)
chessboard[r][c] = target;
else
cout << "You cannot move there!" << endl;
}while(result_r > 1 || result_c > 1);
}
void printBoard(char chessboard[row][column])
{
cout << "\t Column";
cout << "\n\t 1 2 3 ";
for(i = 0; i < row; i++)
{
cout << "\nRow" << i + 1;
cout << "\t - - -";
cout << endl << "\t";
for(j = 0; j < column; j++)
cout << "|" << chessboard[i][j] << "|";
cout << "\n\t - - -";
}
}
Comment