The method canMove is private. It will only be used inside this class, called from the collides method. The method should check if a particular square can move in a certain direction (the second argument).
Note that you should not move the square: only check if the movement is possible. For that reason it is not possible to use the Move method of class square. (It is also impossible to do so: precisely to avoid this error, the square is passed as a const reference.)
The method consists of the following steps:
The public method collides also has a direction argument, but with a const reference to a block. It is this method that will be used in the Tetris application.
Because a block has a method to retrieve a reference to the squares in the block, we use that first:
C Mems<square> & squares = b.getSquares();
The rest of the method will call the method canMove for each square. Even when only one square cannot move in the desired direction, the block collides with the pile. In this case the result will be false. Otherwise true is returned. Here’s the entire method:
bool collides(C block & b, DIRECTION dir) C
{
// Get squares from this block
C Mems<square> & squares = b.getSquares();
5
// check all of them
REPA(squares)
{
if(!canMove(squares[i], dir))
10 {
return true;
}
}
return false;
15}
Now that you have completed these methods, you can reuse your test application to verify this class.