60 lines
3.1 KiB
Plaintext
60 lines
3.1 KiB
Plaintext
\b;Exercise
|
|
The \l;bot\u object\bottr; must pass over all the \l;blue crosses\u object\waypoint; on the ground. The way that must be covered is made of two squares. The first one measures 15 meters, the second 25 meters.
|
|
|
|
\image tproc1a 8 8;
|
|
\b;General principle
|
|
In order to solve this problem, the most efficient solution consists in creating a \l;function\u cbot\function; that instructs the bot to move on a square shape of a certain size. The main program becomes then very simple:\c;
|
|
\s;extern void object::Function1( )
|
|
\s;{
|
|
\s; Square(15);
|
|
\s; Square(25);
|
|
\s;}
|
|
\n;
|
|
You still have to define the function called \c;Square\n;. In order to do this, you will have to write some instructions outside the \l;block\u cbot\bloc; that until now was the frame of each one of your programs. At the very end of the program, after the last closing brace, we will define the function \c;Square\n;. The program will take the following shape:
|
|
\c;
|
|
\s;extern void object::Function1( )
|
|
\s;{
|
|
\s; \n;main function ...\c;
|
|
\s;}
|
|
\s;
|
|
\s;void object::Square(float length)
|
|
\s;{
|
|
\s; \n;new function ...\c;
|
|
\s;}
|
|
\n;
|
|
Let us look in detail at the different elements of the declaration of the function \c;Square\n;:
|
|
|
|
\c;\l;void\u cbot\void;\n;
|
|
This means that this function will return no value.
|
|
|
|
\c;\l;object\u cbot\object;::\n;
|
|
When you write this in front of the function name, you can have access in the function to all the characteristics of the bot, such as \c;position\n;, \c;orientation\n;, etc. In this exercise, this element is not compulsory, as we will not need the characteristics of the bot in the function.
|
|
|
|
\c;Square ( )\n;
|
|
This is the name of the function. You can call it Square, or any other name.
|
|
|
|
\c;\l;float\u cbot\float; length\n;
|
|
Here you define the parameters that the function will get when it is called. The first time the function is actually called with \c;Square(15)\n;, the variable \c;length\n; will contain the value \c;15\n;. The second time, \c;length\n; will contain \c;25\n;.
|
|
|
|
Here is in detail what will happen when the program is executed:
|
|
- First the main function \c;Function\n; will be executed.
|
|
- At the line \c;Square(15)\n;, the program will follow the red arrow and enter the function \c;Square\n; a first time, \c;length\n; containing \c;15\n;.
|
|
- At the end of the function \c;Square\n;, the program follows the orange arrow and comes back to the main function.
|
|
- At the line \c;Square(25)\n;, the program will follow the blue arrow and enter the function \c;Square\n; a second time.
|
|
- At the end of the function \c;Square\n;, the program follows the light blue arrow and comes back to the main function.
|
|
|
|
\image tproc1b 17 12;
|
|
In the function \c;Square\n;, use the instructions \c;\l;move\u cbot\move;\n; and \c;\l;turn\u cbot\turn;\n;. In order to make it shorter, you can use a \c;\l;for\u cbot\for;\n; loop, that will repeat the instructions \c;\l;move\u cbot\move;\n; and \c;\l;turn\u cbot\turn;\n; 4 times; however, this is not compulsory.
|
|
\c;
|
|
\s;void object::Square(float length)
|
|
\s;{
|
|
\s; for ( int i=0 ; i<4 ; i=i+1 )
|
|
\s; {
|
|
\s; move(length);
|
|
\s; turn(90);
|
|
\s; }
|
|
\s;}
|
|
\n;
|
|
\t;See also
|
|
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.
|