staff project download information miscellaneous
Trend Download   The Game of Life
  Download

Trend Tutorials
Trend Basics
Game of Life
One Dimensional CA
Mouse Maze
1-D Bubble Sort
Animation


More Trend Examples

 
MangoVect DownloadPicky DownloadLucy2 DownloadGRAMAUBViz DownloadgeneDBN Download

2. Examining the rule

The code (rule) for The Game of Life is now in the .rule screen. The syntax of the code is similar to that of a C++. The lines with nothing in them are ignored, as are the lines with comments. The indentations are up to the user, but it is recommended to use indentation to keep track of structure. Below is a numbered version of the same rule shown above to make it easier to examine each line of the code.

line# code
1 // A simple Game of Life rules implementation.
2
3 int count;
4 nbr y;
5
6 default cell=cell;
7
8 count=0;
9 over each other y:
10 if(y:cell) count++;
11
12 if (cell && (count<2 || count>3))
13 cell=0;
14 if (cell ==0 && count==3)
15 cell=1;

Trend has the ability to add comments in the same way as C++. In line #1, the line starts with // which allows comments to be added without affecting the code. If the comment is long enough to continue to the next line, the block comment symbols /*...*/ are an option as well. Comments are suggested in order for others to follow the rule structure and for later debugging if necessary. The lines with no code are just for cosmetic purposes to make the code more readable.

1 // A simple Game of Life rules implementation.
In this case, the comments are used as a short description of the rule.

3 int count;
Line 3 is a declaration of an integer named count. Again this is similar to the C++ programming language and declaring variables. In this rule, the integer count is used as a counter for the number of neighbors that are currently "alive". This will be explained more as the theory behind The Game of Life is explained later in the documentation.


Looking at the neighbor positions

4 nbr y;

Line 4 is a declaration of a nbr type named y. The nbr type refers to a cell's neighbors which are defined in the .tmpl screen shown above.The rule The Game of Life has 8 neighbors around each cell. This is shown above with the C being a particular cell and X being the neighbors. The neighbors can be modified to include up to 32 neighbors. This will be used with other rules explained later. The code in line 4 will check all the neighbors to see if they are "alive", have a value of something other than zero. If a neighbor is "alive", it will increment the count variable. An example would be if a particular cell "C" has 5 neighbors "X" that are "alive", it will increment the count variable to 5. The value of the count variable determines the effect of the rule on the cell "C". This will be more obvious once the rest of the lines of code are explained.

6 default cell=cell;
Line 6 is a sort of standard declaration in TREND. It suggests that if no rule applies to a particular cell, then leave it as is. For example, if you have a CA space that has only one "alive" cell, and your rule requires more than one "alive" cell in order to execute the statement, nothing will happen and the CA space will not change.

8 count=0;
Line 8 assigns a value to the variable count. Again this is similar to programming in C++. The variable is declared and initialized with a value of zero.

9 over each other y:
Line 9 is the command that allows each neighbor of a particular cell to be examined for the state of that neighbor cell. It allows for different neighbors to be specified. For example, a cell may have a no (North) neighbor that physically exists above the original cell. Or a neighbor that exists underneath the original cell may be called the so (South) neighbor. So each neighbor has a specific name in order to keep them apart and work with them separately. The actual names can be user defined by using the .tmpl screen talked about earlier and discussed further in the tools tutorial.

10 if(y:cell) count++;
Line 10 is a traditional "if" statement with the condition and a statement to follow when the condition is true. In this case, the code reads if(y:cell). Which means any neighbors of the original cell (think back to the C surrounded by the X as neighbors) that are "alive", then increment the variable count by one. Going back to an earlier example, if 5 neighbors are "alive", then increment the variable count 5 times. Since it started with a value of 0 by line 8 in the code, incrementing it 5 times will give count a value of 5.

12 if (cell && (count<2 || count>3))
13 cell=0;

Line 12 is a conditional statement that starts out by saying if cell. That is an easy way of stating a condition that says if a particular cell has a value other than 0, then it is "alive" and can be called cell. If a cell has a value of 0, then it is considered "dead" and is shown as cell==0. So back to line 12. It states that if the cell is "alive" and the number of its neighbors that are "alive" is less than 2 or greater than 3, then execute the statement that follows. That statement is shown in line 13. It says assign a value of 0 to the cell, or in other words make it "dead".

So to recap lines 12 and 13. The condition states that if any cell has a value other than 0, and it has less than 2 "alive" neighbors or more than 3 "alive" neighbors, assign the value of the cell to 0. Hence, making it "dead". This rule is simulating the theory of The Game of Life in a sense. The theory suggests if a person exists and has too few neighbors to interact with, that person will die. Likewise, if a person has too many neighbors, they will get crowded out and die.

14 if (cell==0 && count==3)
15 cell=1;

Line 14 is the last condition for this rule. It states that if a cell is "dead" (has a value of 0), and has 2 or 3 neighbors that are "alive" (values other than 0), assign the "dead" cell a value of 1. Making it "alive". Going back to the theory of The Game of Life, the rule simulates the idea that if you have 3 neighbors, you will have some sort of reproduction or division that takes place to produce more cells. This rule can be changed slightly to get dramatically different results.

Last modified June 13, 2008 . All rights reserved.

Contact Webmaster

lab