The last element we need is the ’Wall’, the boundary of the playing field. This is not a real element because we can deduce the boundaries of the playing field from the constants we have declared earlier. Yet it is convenient to use the idea of a wall, so that with each movement we can check for collisions with the wall, just as we did with the pile.
Therefore, this class consists of only two methods. A private method canMove checks if a square is allowed to move in a certain direction. A public method collides checks whether a block would hit a wall if it would be moved. The class looks like this:
class wall
{
private:
bool canMove (C square & s, DIRECTION dir) C {}
5
public:
bool collides(C block & b, DIRECTION dir) C {}
}
10wall Wall;
You can surely write this class on your own. The method collides is identical to the method in the class pile. The method canMove is almost the same, but now you don’t compare the new position with the content of a pile. Instead, the result is false when the x or the y position are smaller than 0. Also if the x position is equal to or greater than the value SQUARES_PER_ROW, the result is false. In all other cases, the result will be true.
You can write a new test program or add the new class to the test application for the pile.