1. Loading an existing rule
To load the 1-D Bubble Sort project, select the File option from the .CA screen and select Get template... In the downloaded version of TREND, there are existing rules that are made available to the user. From the list of rules, select the bubble.tmpl. This is done with the same steps as are used in loading the Game of Life Tutorial. For more information on the three separate screens that appear upon loading of the rule, check out the Basic Trend tutorial.
For a explanation of the One Dimensional Bubble Sort click here.
Below is code for the rule 1-D Bubble Sort:
1 |
default value = value; |
2 |
default flip = flip; |
3 |
nbr target; |
4 |
if(flip = = 0) { |
5 |
value = no:value; |
6 |
} |
7 |
else { |
8 |
if(flip = = '<'){ |
9 |
target: = we:; |
10 |
if(value != 0 && target:value != 0) |
11 |
if(target:value > value) |
12 |
value = target:value; |
13 |
flip = '>'; |
14 |
} |
15 |
else { |
16 |
target: = ea:; |
17 |
if(value != 0 && target:value != 0) |
18 |
if(target:value < value) |
19 |
value = target:value; |
20 |
flip = '<'; |
21 |
} |
22 |
} |
Earlier tutorials such as the Game of Life and the Mouse Maze have several lines of comments in the .rule. This rule contains no comments. However, each line will be explained in this tutorial.
1 |
default value = value; |
2 |
default flip = flip; |
Above are the first two lines in the bubble.rule. The lines suggest that if no rule applies to a particular cell, then leave it as is. Meaning, if no code applies to the variable "value", then leave the variable alone. The same is true with the variable "flip".
Line 3 is a declaration of a nbr type named "target". The nbr type refers to a cell's neighbors which are defined in the .tmpl screen. This was discussed in the Game of Life tutorial.
Line 4 is the first part of the selection process for the rule.
It states that if the variable "flip" for a particular cell is equal to zero, then execute the statement(s) that follow. In TREND, if a varible is equal to zero, it is said to be "dead". It has no value, so it is not "alive". If this condition is proven to be true for a particular cell, then it will execute the statement in line 5 below.
Line 5 is copying a value from one cell to another. It is assigning the "value" field of the northern neighbor to the cell that found line 4 to be true. To recap lines 4 and 5. If a cell has a value of 0 for the "flip" field, then copy the value of the northern neighbor for the field "value". The naming of the field "value" may be confusing in this example. Note in this example, the field named "value" will be enclosed in quotes, and the variable named value will not.
Lines 1 through 5 allow for the previous lines to be shown on the screen as the epoch increases. It shifts everything down a line as it executes a new epoch. This allows the user to compare previous lines with the current state of the CA space. This also serves a purpose when converting the numbers to colors to give the impression of movement as the bubble sort executes.
In the example below, each new epoch will create a new line. The new line is created at the top of the screen and the other lines get pushed down the screen. So the bottom line is actually the first line and the top line is the newest line, just having been created.
|