58 lines
2.1 KiB
Plaintext
58 lines
2.1 KiB
Plaintext
\b;Exercise
|
|
Destroy the six \l;targets\u object\bottarg; with a program using a loop. The bot must move 5m forward to get from one target to the next.
|
|
|
|
\b;General principle
|
|
The program must execute the following scheme:
|
|
Repeat 6 times :
|
|
o move 5m forward
|
|
o turn 90 degrees left
|
|
o shoot
|
|
o turn 90 degrees right
|
|
|
|
\image tfor1 14 5;
|
|
\b;Instruction \c;for ( )\n;
|
|
The instruction \c;\l;for\u cbot\for;\n; asks for 3 expressions:
|
|
1) Initialize the counting variable.
|
|
2) The end \l;condition\u cbot\cond;.
|
|
3) The counting \l;expression\u cbot\expr;.
|
|
|
|
Here is the loop once it is integrated into the program frame:
|
|
\s;\c;extern void object::Massacre( )
|
|
\s;{
|
|
\s; for ( int i=0 ; i<6 ; i=i+1 )
|
|
\s; {
|
|
\s; \n;instructions repeated 6 times ...\c;
|
|
\s; }
|
|
\s;}
|
|
\n;
|
|
ATTENTION: The line \c;for ( )\n; must not be followed by a \l;semicolon\u cbot\term; !
|
|
|
|
\b;Explanation of the instruction \c;for ( )\n;
|
|
1) \c;int i=0\n;
|
|
The \l;variable\u cbot\var; i is set to zero before the beginning of the loop.
|
|
|
|
2) \c;i<6\n;
|
|
The loop will be executed as long as i is smaller than 6.
|
|
|
|
3) \c;i=i+1\n;
|
|
At the end of every loop, add 1 to the variable i.
|
|
|
|
\b;Blocks
|
|
Use braces \c;{ }\n; in order to create a \l;block\u cbot\bloc;. All the instructions that must be executed in the \c;for\n; loop are held together by a block. The whole program itself is made up of a block:
|
|
\c;
|
|
\s;extern void object::massacre( )
|
|
\s;{
|
|
\s; \n;fill in here ...\c;
|
|
\s;}
|
|
\n;
|
|
Never change these characters. Just add the instructions of the program between the braces.
|
|
You can fit several blocks one into the other. For example the \c;for\n; block is fitted into the block of the whole program. In order to improve readability, the editor lines up the braces belonging to the different blocks.
|
|
|
|
\b;Remember
|
|
The instruction used to move forward is \c;\l;move\u cbot\move;();\n;.
|
|
The instruction used to turn the bot is \c;\l;turn\u cbot\turn;();\n;. A positive angle turns left.
|
|
The instruction used to fire the cannon is \c;\l;fire\u cbot\fire;(1);\n;. A one-second burst allows to destroy all six \l;targets\u object\bottarg;.
|
|
|
|
\t;See also
|
|
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.
|