Some basics ....
From User talk:Astacus
Jump to navigation
Jump to search
Hi mate.
To answer you question about the 'while' loop:
The debug properties will be printed last because ahead(200) is a blocking command. This means the bot will move 200 pixels and after that the next line will be executed and so on.
There is a difference between setXXX(...) and blocking commands (ahead(..), turnXXX(..)). A setXXX will register the value for later execution and returns immediately. Maybe i give you a little example so you can see the difference:
public class YourRobotName extends AdvancedRobot
{
.....
public run()
{
System.out.format("Start\n"); // this will be executed only once per battle
while(true)
{
// output to the console - immediately
// will be executed every turn, if no blocking command is used
System.out.format("heading: %.1f° at time %d. \n", getHeading(), getTime());
// register formatted string for later output
setDebugProperty("startingWhile", String.format("heading: %.1f° at time %d.", getHeading(), getTime()));
// move the bot 200 pixel - does nothing in between
// beware that a bot can only move max 8 pixel per turn and this would return only if the bot
// has reached its destination so if this comes back you are not at turn 1 anymore - instead
// you are at turn 25 (200/8) ( can be +/- and depends on acceleration/deceleration)
ahead(200);
// register 200 movement for later execution (you have to extend your class by AdvancedRobot
// to make it work - returns immediately and does nothing until execute()
setAhead(200);
....
// after this, the setAhead(200) value you have registered will be executed - but with a slightly
// different behavior than ahead(...)
// basically it does move your bot with the current velocity (lets say 8 pixel) and returns to
// the beginning of the while loop. This means the next turn the 200 are still registered
// minus 8 - so 192 pixel to go. This has the advantage that you can command your bot
// every turn if you want.
//Also if you have the setAhead(200) implemented like above you will set 200 every turn
//and your bot will move endless (or at least until you hit a wall)
// It works for all setXXX commands the same - and setDebugProperties(...) is no difference.
// (actually there is a little glitch with the properties and you have to update the value every turn
// but that should not bother you right now)
// - for starters i would suggest the System.out.format(...) to be on the easy side.
execute();
}
}
}
I'm not sure if this helps a little, but maybe you get a glimpse of it. And don't worry to ask whatever you want to know - the community is quite helpful and we all had our first steps someday.
Wompi