DEV Community

loading...

Discussion on: Don’t pay the for-loop tax

danhomola profile image
Dan Homola Author

I agree the loop would look worse. However, in this case all you are doing is finding the first piece and if it's white calling a method, right?

So you could do something like

const pieceIndex = findFirstPiece(row);
if (pieceIndex > 0 && is_white_piece(pieceIndex , column))
  GENERATE_MOVE;

where the findFirstPiece would find the first row value that is either a white or a black piece, returning -1 if no such value was found.

Thread Thread
tbodt profile image
tbodt

Oops, I pasted the code wrong. What I wanted to do was generate every move from the current position of the rook up to a piece, and including the piece if the piece is a black piece. See updated post.