63 lines
3.1 KiB
Plaintext
63 lines
3.1 KiB
Plaintext
\b;Exercise
|
|
Program the bot so that it will find its way without hitting the walls of the labyrinth. We suppose that you do not know the configuration of the labyrinth, but there are no bifurcations, and no dead-ends. The labyrinth is made of squares measuring 5m each.
|
|
|
|
\b;The instruction \c;radar\n;
|
|
The instruction \c;\l;radar\u cbot\radar;(Barrier, 0, 45, 0, 5);\n; will find any \l;barrier\u object\barrier; in front of the radar that is closer than 5m. Let us take a closer look at the five parameters used:
|
|
|
|
\s;\c;Barrier\n;
|
|
\l;Category\u cbot\category; of the object that the radar must look for, i.e. a barrier.
|
|
|
|
\s;\c;0\n;
|
|
Direction of the radar. \c;0\n; means that the radar must search straight forward.
|
|
|
|
\s;\c;45\n;
|
|
Opening angle in degrees. With an opening angle of 45 degrees, barriers situated between 22.5 degrees to the left and 22.5 degrees to the right will be detected.
|
|
|
|
\s;\c;0\n;
|
|
Minimum detection distance. \c;0\n; means that even object that are very close to the bot will be detected.
|
|
|
|
\s;\c;5\n;
|
|
Maximum detection range. Any barrier situated beyond \c;5\n; meters will not be detected.
|
|
|
|
To take another example, \c;\l;radar\u cbot\radar;(Barrier, 90, 45, 0, 5);\n; will direct the radar 90 degrees to the left, in order to test if the way to the left is free.
|
|
|
|
\image tlaby1 10 10;
|
|
\b;General principle
|
|
The program must only take care of one square portion of the labyrinth. You will have to execute it several times in order to arrive at the finishing pad.
|
|
o If there is nothing in front, move forward.
|
|
o If there is nothing on the left side, quarter turn left, move forward.
|
|
o If there is nothing on the right side, quarter turn right, move forward.
|
|
|
|
1) First of all declare three \l;variables\u cbot\var; of type \c;\l;object\u cbot\object;\n;, that we call \c;front\n;, \c;left\n; and \c;right\n;. Variables of this type can contain the description of any object, for example of a barrier found by the radar.
|
|
\s;\c; object front, left, right;\n;
|
|
|
|
2) Look for barriers in all three directions, and put the result of the \c;radar\n; instruction into the three variables defined at point 1). If the radar finds nothing, the variable will contain the value \c;\l;null\u cbot\null;\n;.
|
|
\s;\c; front = radar(Barrier, 0, 45, 0, 5);
|
|
\s; left = radar(Barrier, 90, 45, 0, 5);
|
|
\s; right = radar(Barrier, -90, 45, 0, 5);
|
|
\n;
|
|
3) Test if the way is free in front of the bot with the instruction \c;\l;if\u cbot\if;\n;. If the test is true, the instructions in braces \c;{ }\n; will be executed, otherwise the execution will resume after the closing brace \c;}\n;.
|
|
The instruction \c;return\n; leaves the program: the job is done.
|
|
\s;\c;if ( front == null )
|
|
\s;{
|
|
\s; move(5);
|
|
\s; return;
|
|
\s;}
|
|
\n;
|
|
4) Test if it is possible to turn left; if so, turn left with the instruction \c;\l;turn\u cbot\turn;\n; and move 5m forward.
|
|
\s;\c;if ( left == null )
|
|
\s;{
|
|
\s; turn(90);
|
|
\s; move(5);
|
|
\s; return;
|
|
\s;}
|
|
\n;
|
|
5) Test if it is possible to turn right.
|
|
...
|
|
|
|
\b;Attention
|
|
The instruction \c;if ( )\n; must never be followed by a \l;semicolon\u cbot\term;.
|
|
|
|
\t;See also
|
|
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.
|