Optional Page

Brick Wall (Optional)

Designing a program to draw this picture—a brick wall that you might later use as a stage background in some program you write—is good practice in analyzing structure.

Humpty Dunpty on brick wall
Sample image of brick wall

Abstraction

While any good programming language might have many tools for drawing and moving, it wouldn't make sense to have special tools for drawing bricks because most programs don't involve bricks. That is the sort of tool you make yourself when you need it.

When you need to draw bricks, having a special draw-brick block lets your code use terms directly related to the problem you are trying to solve, rather than the more general-purpose terms that the computer uses for all kinds of tasks. The process of moving from what the programming language gives you to a natural language (one that humans speak to communicate) is a key part of good abstraction.

In this problem, you will build an abstraction for drawing a brick wall, first by creating draw-brick, then blocks for drawing rows, and ultimately the goal: draw-brick-wall-n.

Drawing One Brick

A picture of a brick is rectangle, filled in "brick red" (darker than the primary-color red). There's no draw rectangle block in snap. One way to fake it is by thinking of a rectangle as a very thick line.

Code for draw brick
  1. Try out this block.
    If your "brick" has rounded ends, remember that you can change how lines are drawn by clicking the settings button (settings button) in the toolbar, and turning on "Flat line ends." Rounded ends stick out beyond the length you specified. Here's a picture of the rounded brick with a regular line (pen size 1) of the same length inside it: Round brick

    Rounded ends are better for most line drawings, but not for drawing bricks.

Using Problem Decomposition to Write an Abstraction

You'd like the "top level" block to be something like this:

draw-brick-wall-7

A human brick-layer would understand exactly how to build such a wall. A computer doesn't know, so you must fill in the details. This means going from the abstract (draw a brick wall) to the concrete (pardon the pun). This involves problem decomposition.

There are two kinds of rows, so we make blocks that specialize in each:

  1. Make blocks Row A and Row B.
  2. Think about what helper blocks besides Draw Brick you might want.
  3. Too much abstraction? It's possible to go overboard on abstraction and build so many blocks that your program is just as cluttered as it would be without the special blocks.

    But it can also be useful to make a special block even when its definition is just one built-in block. For example, to draw the mortar (the cement between bricks, shown as white space), you can just use Move 4 Steps, but it might make sense to define a draw mortar block.

    Why? You might later decide that 4 steps is the wrong thickness for mortar, and you'd rather have 5. Or you might want the mortar to be cement-colored, slightly gray. And your complete project might have Move 4 Steps blocks that aren't about mortar. With many move blocks scattered through your program, you would have to find and change each one, and keep putting in set pen color blocks. With a draw mortar block, you can just change its definition, and all the mortar in your picture will be changed.

  4. The two kinds of rows should be exactly the same length. Your first try at drawing Row B may be a little too long. If so, think through what causes that bug.

    How are you going to fix it? Should Row B have different-size bricks, different-size mortar gaps, or different-size half-bricks? If you're not sure, try all the possibilities and see which looks right in the finished wall. Or think "What would make most sense in a real brick wall?"

  5. Once you have rows A and B the same length, you can write the Draw a Brick Wall with ( ) Rows block. Make sure that it works sensibly for both even and odd numbers of rows.
Now Would be a Good Time to Save
 
  1. After you've drawn the picture at the top of this page, add more inputs to the Draw a Brick Wall block:

    1. Length of the wall (how many bricks)
    2. Length of a brick
    3. Mortar thickness

    Add these one at a time, not all at once! When you modify the length of a brick, that should also change the length of a half-brick. When you modify the mortar thickness, that should also change the distance between rows (since that's mortar too).