C ++ - Chess Bishop MoveCode error?

So, I am making a chess game, however I cannot correctly move part of the bishop.

This is my chessboard:

string board[8][8] = { {"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, {"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, {"_" , "_", "_" , "_" ,"_", "B" , "_" , "_"}, {"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, {"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, {"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, {"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, {"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"} }; 

Here is the Draw function for drawing boards.

 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"; } 

The bishop's movement code is still.

  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 X spaces that a piece can move to the user in some places on the board. however, when a piece is in certain places in the array, like the place it is in the code of the board. It overlaps and displays the Xs on the other side of the board, instead of stopping the drawing of Xs when they are not "_".

+6
source share
1 answer

You need to check if x + counter and y + counter are included in 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 move in four directions.

Moreover, this does not check if there are any fragments between the contour.

To consider all four directions, you can create a direction matrix that 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; } } } } 
+7
source

Source: https://habr.com/ru/post/1014058/


All Articles