So i'm making a chess game, however i can't get the bishop piece moving correctly.
This is my chessboard:
string board[8][8] = {
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "B" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"} };
Here is the Draw function to draw board.
void Draw()
{
for( int i = 0; i < 8; i++ )
{
for( int j = 0; j < 8; j++ )
std::cout << board[ i ][ j ] << ' ';
std::cout << '\n';
}
cout<<"\n";
}
Bishop Movement Code so far.
if (board[x][y] == "B")
{ //Highlight the users chosen piece
board[x][y] = "\033[0;31mB\033[0m";
//Now showing available moves the chosen bishop can move to
for(int counter=1; counter <=7; counter++) {
if(board[x+counter][y+counter] == "_") { //if there is an empty space, then place X to show peice can move there
board[x+counter][y+counter] = "X";
}
else { //if cannot move their ,then break
break;
}
}
}
Here is my problem. It shows the X spaces the piece is able to move to to the user in some places on the board. however when the piece is in certain places of the array like the the place it is in the board code. It overlaps and shows Xs on different side of the board, instead of stopping drawing the Xs when their is no "_" available.
You need to check if x + counter
and y + counter
fall within the board.
if (board[x][y] == "B")
{ //Highlight the users chosen piece
board[x][y] = "\033[0;31mB\033[0m";
//Now showing available moves the chosen bishop can move to
for(int counter = 1; (x + counter) <= 7 && (y + counter) <= 7; counter++){
if(board[x + counter][y + counter] == "_") {
//if there is an empty space, then place X to show peice can move there
board[x + counter][y + counter] = "X";
}
else { //if cannot move their ,then break
break;
}
}
}
Of course, this marks only one direction, while the bishop can actually move in four directions.
Also, this does not check if there are any pieces in between the path.
To consider all four directions, you can create a direction matrix, which stores the change in x and y for each of the directions.
// 4 directions in which bishop can move
int dx[4] = {-1, -1, 1, 1};
int dy[4] = {-1, 1, -1, 1};
if (board[x][y] == "B")
{
// for each direction
for(int dir = 0; dir < 4; dir++) {
// the bishop can move to a maximum of 7 squares
for(int counter = 1; counter < 8; counter++) {
// calculate where the bishop will be
// after moving "counter" number of squares
int new_x, new_y;
new_x = x + dx[dir] * counter;
new_y = y + dy[dir] * counter;
// check if the square lies within the board
if(new_x >= 0 && new_x < 8 && new_y >= 0 && new_y < 8) {
// if there is an empty space, then place X to show peice can move there
if(board[cur_x][cur_y] == "_") {
board[cur_x][cur_y] = "X";
}
// if there is any other piece in between, the bishop can't move further
else {
break;
}
}
// break if the square is outside the board
else {
break;
}
}
}
}