colobot-data/help/texch2.txt

37 lines
2.1 KiB
Plaintext

\b;Exercise
In this exercise, the \l;information exchange posts\u object\exchange; along the way contain the direction of the next post and the distance. And you do not know how many exchange posts you must get in touch with before arriving at your goal.
\b;General principle
Repeat forever:
o Retrieve the direction from the \l;exchange post\u object\exchange;.
o Retrieve the distance from the \l;exchange post\u object\exchange;.
o If no information could be retrieved, stop the program.
o Execute the rotation.
o Move forward to the next post.
In order to repeat always, use a \c;\l;while\u cbot\while; (true)\n; loop. The instructions between the braces \c;{ }\n; will be repeated over and over, or until a \c;\l;break\u cbot\break;\n; instruction is executed.
\s;\c; while ( true )
\n;
This time you need 2 \l;variables\u cbot\var;, one for the rotation angle, one for the distance, for example \c;dir\n; and \c;len\n; :
\s;\c; float dir, len;
\n;
Then you can retrieve the information:
\s;\c; dir = receive("Direction");
\s;\c; len = receive("Length");
\n;
A variable of the \l;type\u cbot\type; \c;\l;float\u cbot\float;\n; can take a special value called \c;\l;nan\u cbot\nan;\n;. This value means that the variable contains no number (Not A Number).
When there is no \l;exchange post\u object\exchange; nearby, either because the bot has reached the goal, or because it took the wrong way, the two variables \c;dir\n; and \c;len\n; contain this value. You can test this with the instruction \c;\l;if\u cbot\if;\n;, and stop the program if necessary with the instruction \c;\l;break\u cbot\break;\n;:
\s;\c; if ( dir == nan ) break;
\n;
If the information retrieval from the \l;exchange post\u object\exchange; has been performed successfully, execute the rotation:
\s;\c; turn(dir);
\n;
And move forward:
\s;\c; move(len);
\n;
\b;Remark
You can click on an \l;information exchange post\u object\exchange; in order to read what information it contains. In this exercise, every exchange post contains two values called \c;"Direction"\n; and \c;"Length"\n;.
\t;See also
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.