2. Mouse Maze Code
The code below is the rule for the Mouse Maze project. As with the other examples, there are several lines of code that have comments. It is recommended to make comments often in the code to help debugging and maintenance of the code.
Below is a table showing the line numbers of the code shown above. This will help in disecting the code line by line.
line |
code
|
1 |
// default is you don't change the ca space |
2 |
|
3 |
default mouse=mouse; |
4 |
default direc=direc; |
5 |
|
6 |
// mouse will find empty cell next to it to move into |
7 |
|
8 |
if (mouse=='M') { |
9 |
rot if (direc=='d' || direc=='^' && no:direc=='.') |
10 |
// mouse has already decided where to move |
11 |
mouse='.'; // so it moves |
12 |
else rot if ((no:mouse=='.' || no:mouse=='F') && no:direc=='.') |
13 |
// check empty neighbors |
14 |
direc='^'; // find one, so set the direction |
15 |
else // exhausted all chance, should back track now |
16 |
direc='d'; // so set to dead direction and wait for rescue |
17 |
} |
18 |
|
19 |
if (mouse=='F') { //if I am food and mouse found me then I change to 'V' |
20 |
rot if (so:mouse=='M' && so:direc=='^') |
21 |
mouse= 'V'; |
22 |
} |
23 |
|
24 |
// empty space will check if a mouse is willing to come over |
25 |
|
26 |
if (mouse=='.') { |
27 |
rot if (direc=='.' && so:direc=='^' && so:mouse=='M') |
28 |
// a mouse points at me |
29 |
mouse=so:mouse; //so I copy it over |
30 |
else rot if (direc=='.' && no:direc=='d' && no:mouse=='M') |
31 |
// a mouse needs rescue |
32 |
mouse=no:mouse; // so I also copy it over |
33 |
} |
|