The isCollision() function

The second section of code contains logic to detect collisions:

bool Board::isCollision(const Piece &piece) const {
for (int column = 0; column < PieceSize; ++column) {
for (int row = 0; row < PieceSize; ++row) {
if (piece.isBlock(column, row)) {
int columnTarget = piece.getColumn() + column;
int rowTarget = piece.getRow() + row;
if (
columnTarget < 0
|| columnTarget >= BoardColumns
|| rowTarget < 0
|| rowTarget >= BoardRows
) {
return true;
}
if (cells_[columnTarget][rowTarget]) return true;
}
}
}
return false;
}

The isCollision() function loops through each cell on the board until it reaches one populated by the &piece passed as an argument. If the piece is about to collide with either side of the board or it has reached the bottom, the function returns true, otherwise it returns false.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset