|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
extend robot.... overwrite fire()?hi,
i'm relatively new to both java and robocode. when i build a robot i extend one of JuniorRobot, Robot or AdvancedRobot. then i overwrite the run() method to run my own code. but can i write a robot with some basic behavior and extend that instead of robot? and can i overwrite methods like fire() or ahead()? if so, can i copy the source code from the robot class and edit it? imagin a fire() method that does not check for gun heat and does not decreas my life... rapidfire robot! bye! pepijn |
|
|
Re: extend robot.... overwrite fire()?pepijndevos wrote:
> hi, > > i'm relatively new to both java and robocode. > when i build a robot i extend one of JuniorRobot, Robot or > AdvancedRobot. > You can also extend Droid or TeamRobot. > then i overwrite the run() method to run my own code. > The correct term is override. =) However, most bots have a lot of code in the event handlers, which you also override. > but can i write a robot with some basic behavior and extend that > instead of robot? > No, you cannot. Robocode will only recognize bots that are extended, and thus linked in, with its internals. If you copied the code from the robot class none of your methods like getX(), getGunHeading(), etc would work because they would not receive the correct updates from the Robocode engine. > and can i overwrite methods like fire() or ahead()? > if so, can i copy the source code from the robot class and edit it? > imagin a fire() method that does not check for gun heat and does not > decreas my life... rapidfire robot Well, you could, but it would prevent the calls from reaching the parent class, so nothing would actually happen. Take a look at Robowiki.net for a bunch of ideas and code snippets. Julian (aka Skilgannon) |
|
|
Re: extend robot.... overwrite fire()?[snip]
> i'm relatively new to both java and robocode. > when i build a robot i extend one of JuniorRobot, Robot or > AdvancedRobot. Then i overwrite the run() method to run my own code. > > but can i write a robot with some basic behavior and extend that > instead of robot? Yes, a lot of robots are developed that way. You could make your own robot based on e.g. the AdvancedRobot, and then create your own base class on top on this, e.g. one that has a good scanning strategy. Next you could extend this one for making variants or complete robots. With Robocode 1.6.0 and newer versions, you can also create your own robot class bases on the core robocode.robotinterfaces backage, which is a more "raw" way to create new robot classes. The JuniorRobot, Robot, AdvancedRobot, TeamRobot are all based on these interfaces, so you could make your own robot type if you want to, but it still has to follow the common rules and limitations provided with the robot interfaces. > and can i overwrite methods like fire() or ahead()? Yes, but you must call super's methods first. Otherwise it will not work. E.g. public void fire(double power) { if (someCondition) { power = 1; } super.fire(power); } I do not recommend overriding the API methods. It makes it hard to tell what is going on inside your robot. I think it better that you make your own methods like: public void doFire(double power) { if (someCondition) { power = 1; } fire(power); } > if so, can i copy the source code from the robot class and edit it? > imagin a fire() method that does not check for gun heat and does not > decreas my life... rapidfire robot! You can make your own Robot class by copying the source from the Robot class. As I mentioned, this is using the robocode.robotinterfaces: http://robocode.sourceforge.net/docs/robocode/robocode/robotinterfaces /package-summary.html For example, you could implement your own robot type that is inherited from robocode.robotinterfaces.IBasicRobot, and then use the getPeer(), which will return a robocode.robotinterfaces.peer.IBasicRobotPeer which you can use for telling your robot what to do, and also read out data etc. The Robot class does this. But it still has to obey the rules in Robocode, and this is protected by the peers (the ones in robocode.robotinterfaces.peer). - Flemming |
| Free embeddable forum powered by Nabble | Forum Help |