Create Territories

We will create territories by drawing different coloured regions in SpaceLand.  Devils generally move around and stay within their own territory.

Switch to SpaceLand, and use the zoom button to zoom out so that you can see the whole land.

Click the Edit Terrain button.

Use the mouse to select a region, then click the colour palette to make it a different colour.

 

Stay In Territory

Devils should generally stay in their own territory.  To make them do this, we will create a variable called previous patch colour and store the colour of the ground the Devil is on before it moves. 
Next we will slightly alter the Devils Move procedure to record the colour of the ground the Devil is on before it moves.
Now we will make a procedure that compares the patch colour after the Devil has moved a step with what is stored in previous patch colour.  If it is different, then the Devil has moved out of its territory.  We will make it turn around 180 degrees and take 3 steps.  This should return it to its original territory. 

Finally, switch to the Runtime workspace and add in the Stay in Territory procedure to the Forever block.

Switch to SpaceLand to test your simulation.