samedi 8 août 2020

using nested if statements vs && in c++

Let's say I have an integer array representing the chess pieces on a board;

int board[8][8];

In my chess game i am currently coding a generator function that will return an integer vector of all legal moves.

Naturally i will be using if statements

I am at a point where i need to check a certain element in the board relative to a piece on the board

For example, If i have a pawn piece;

board[row][col] == 'p';

I need to generate [row+1][col],[row+2][col] and in some cases if it can attack a piece, a change in column too.

But if a piece is on any edge of the board, board[row+1][col] will return be index out of range

For that reason i need an extra if statement.

My question is;

Shall i use;

if (pieceisnotonedge && board[row+1][col] == 0)

or

if (pieceisnotonedge){
    if (board[row+1][col] == 0)
}

For the first example, if pieceisnotonedge returns false, will it also check the next condition? Because if it does, then i am in trouble.

Aucun commentaire:

Enregistrer un commentaire