44 lines
1.8 KiB
Plaintext
44 lines
1.8 KiB
Plaintext
\b;Exercise
|
|
Search the whole zone inside the barriers with the \l;sniffer bot\u object\botsr; for adequate locations for a derrick. When the bot sounds the subsoil and discovers an adequate location for a derrick, it will mark it with a red cross.
|
|
|
|
\b;Description
|
|
The zone inside the barriers is a square of 25 meters. The most efficient way is to check every 5 meters.
|
|
|
|
\image sniff1 12 12;
|
|
One way to achieve this is to fit two \c;\l;for\u cbot\for;\n; loops one into another:
|
|
\c;
|
|
\s;for ( int y=0 ; y<6 ; y=y+1 )
|
|
\s;{
|
|
\s; for ( int x=0 ; x<5 ; x=x+1 )
|
|
\s; {
|
|
\s; \n;sound the subsoil\c;
|
|
\s; \n;move 5m forward\c;
|
|
\s; }
|
|
\s; \n;sound the subsoil\c;
|
|
\s; \n;quarter turn left\c;
|
|
\s; \n;move 5m forward\c;
|
|
\s; \n;quarter turn left\c;
|
|
\s;}
|
|
\n;
|
|
This program works well only at the beginning. After the first line, the bot turns correctly left and sounds the second line. But after the second line, it must turn right!
|
|
You will have noticed that if \c;y\n; is even, the bot must turn left, and if \c;y\n; is odd, the bot must turn right.
|
|
The \l;expression\u cbot\expr; \c;y%2\n; returns the rest of the division of \c;y\n; by \c;2\n;. For example, if \c;y=3\n;, the expression returns \c;1\n;.
|
|
The test \c;\l;if\u cbot\if; { }\n; can be followed by a \c;else { }\n;. If the test is true, the first \l;block\u cbot\bloc; \c;{ }\n; will be executed. Otherwise, the second block \c;{ }\n; will be executed.
|
|
\c;
|
|
\s;if ( y%2 == 0 )
|
|
\s;{
|
|
\s; y \n;is even\c;
|
|
\s;}
|
|
\s;else
|
|
\s;{
|
|
\s; y \n;is uneven\c;
|
|
\s;}
|
|
\n;
|
|
It is up to you to finish the program.
|
|
|
|
\b;Remark
|
|
Use the instruction \c;\l;sniff\u cbot\sniff;( )\n; to sound the subsoil, and \c;\l;move\u cbot\move;( )\n; and \c;\l;turn\u cbot\turn;( )\n; to move the bot.
|
|
|
|
\t;See also
|
|
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.
|