3. Explanations of Code
Line one of the code is just a comment declaring the default action of the code. If a cell doesn't fit a condition in the code, it is left alone (not changed).
1 |
// default is you don't change the ca space |
Line three and four are declarations. They make the defaults for the condition explained in line one.
3 |
default mouse=mouse; |
4 |
default direc=direc; |
Line six is another comment that explains the code in line eight.
6 |
// mouse will find empty cell next to it to move into |
As the comments in line six suggest, line eight will search the CA space for the mouse 'M'. The symbol 'M' is a value in the field labeled "mouse". This can be seen in the screen print of the
.tmpl on page one. If a symbol 'M' is found in the CA space, the program will continue with the lines of code after line eight until it finds a close brace.
Line nine is a condition that is started with a "rotated if". A "rotated if" is a short cut in the TREND language. It is a very powerful tool that allows the user to combine 4 sets of conditions into one. For now think of it as a normal "if". Line nine basically says that if any cell in the CA space has a
direc of 'd', then make the
mouse value for that cell equal to '.'. Meaning, if the cell has a direc value of 'd' (it is a dead end), then make the mouse value for that cell equal to 0 (zero value is shown with a '.'). There is an
or in this line. It suggests that if the first part of the condition is not true, then check to see if the second part is true. Check if
direc is equal to '^'
and the cell above it has a
direc of '.' (zero), then assign the value of mouse to '.' (zero).
9 |
rot if (direc=='d' || direc=='^' && no:direc=='.') |
10 |
// mouse has already decided where to move |
11 |
mouse='.'; // so it moves |
With the "rotated if" in line nine, the code is shortened significantly. Here is another way of coding the conditions presented in line nine.
if (direc=='d' || direc=='^' && no:direc=='.') |
else if (direc=='d' || direc=='>' && ea:direc=='.') |
else if (direc=='d' || direc=='V' && so:direc=='.') |
else (direc=='d' || direc=='<' && we:direc=='.') |
The "rotated if does the rotation automatically. The rule is looking for the conditions to be true from it's North, South, East, and West neighbors. The "rotated if" will check all of these neighbors for the condition automatically. This saves a lot of time and space when coding.
Line ten is another comment stating that if the condition in line nine is found to be true, the mouse knows which way it is going to go.
10 |
// mouse has already decided where to move |
Line 11 is the statement to be executed when the condition in line nine is found to be true. Basically it just makes the value of the mouse '.' (zero).
11 |
mouse='.'; // so it moves |