A code-along is a story about coding in which you are encouraged to “code up” all the examples in the story. In a code-along you should not cut-and-paste.
In this code-along, we will explore how to translate a specification (in condensed notation) of a Bricklayer fabric into an implementation (i.e., a Bricklayer program) that, when executed, creates the specified fabric.
The Specification of a Bricklayer Fabric
The fabric specification implemented in this code-along use a base-pattern which consists of four lines: 2 horizontal and 2 vertical. This pattern is repeated 6 times to create the fabric. During each iteration of the pattern each line moves further away from its initial central position. The first line moves up, the second line moves down, the third line moves to the right, and the fourth line moves to the left. The formal specification of this fabric is given below.
|Line Segment Name||Line Segment Specifics|
|line1||=||(0,16) (31,16) BLUE|
|line2||=||(0,15) (31,15) YELLOW|
|line3||=||(16,0) (16,31) RED|
|line4||=||(15,0) (15,31) BLACK|
The program shown below represents an initial attempt to construct a portion the specified fabric. Specifically, the fabric created by this program consists of (one iteration of) the base pattern. Note that the boundary position (e.g., 31) is a are hard-coded constant. Such hard-coded constants will make it difficult to create a larger similar fabric.
The code in this attempt creates a fabric consisting of two iterations of the base-pattern. Notice the weaving pattern is starting to emerge. From the standpoint of the implementation, it is worth noting that the upper boundary position is now a parameter to the weave function. This will enable the size of the fabric to be easily changed. Furthermore, note that the increments/decrements associated with each iteration are explicitly written out (e.g., 16+0). This highlights the similarities (as well as differences) between the first and second iterations.
Limitations of the current implementation include: (1) the hard-coded positional values 15 and 16, and (2) code redundancy between iterations. The drawback of hard-coding positional values is that moving the fabric to a different location in the virtual space requires the changing of numerous positional constants.
The final version of the code that implements the fabric is shown below. Note that a function, called pattern, has been introduced for creating instances of the fabric’s base-pattern. This function is parameterized on an integer value, called delta. When called with a specific value for delta, the function pattern creates an instance of the base-pattern in which all lines in the pattern are accordingly incremented/decremented by delta.
The function weave has two additional parameters: x/z, which represents a parameterization of the x/z value’s occurring in the definintions of each of the lines. Also note, that the different values of x/z (i.e., 15 and 16) are expressed as a function of the paramenter x/z (e.g., x and x-1). Adding x and z to the function weave enables the “center” of the weave pattern to be placed anywhere in the xz-plane, provided the max value and the dimensions of the virtual space are adjusted accordingly.
Two drawbacks of this implementation are: (1) the value of the lower boundary, 0, is still hard-coded, (2) the connection (size = max + 1) between max and the size of the virtual space is implicit. However, the most significant drawback is the fact that the fabric is created by calling the function pattern six times! At a higher coding level we will see that such simple repetitions of code can be easily and compactly expressed using functions having a loop-like behavior.