my second robot. anatomy of a robot a robot consists of 3 independently moving parts: vehicle -...

8
My Second Robot

Upload: hilary-griffith

Post on 14-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move

My Second Robot

Page 2: My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move

Anatomy of a Robot A robot consists of 3 independently moving

parts:  

Vehicle - determines the direction in which the robot will move

Gun - determines the direction in which the robot will fire

Radar - determines what the robot can see (if you turn on Visible scan arcs in options -> Preferences, this will give you a feel for how much a robot can see)

Page 3: My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move

Turning By default, moving a component will result in all components ON

TOP of it moving accordingly (e.g. if the gun turns 90 degrees left, the radar will turn 90 degrees left). However, you can set components to adjust themselves automatically so that they are independent of the movement of other components, using these methods:

setAdjustGunForRobotTurn(boolean flag): If the flag is set to true, the gun will remain in the same direction while the vehicle turns.

setAdjustRadarForRobotTurn(boolean flag): If the flag is set to true, the radar will remain in the same direction while the vehicle (and the gun) turns.

setAdjustRadarForGunTurn(boolean flag): If the flag is set to true, the radar will remain in the same direction while the gun turns. It will also act as if setAdjustRadarForRobotTurn(true) has been called.

Page 4: My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move

Basic Commands Below are the basic commands for moving the robot around, you

can find more detailed definitions in the Robocode API. turnRight(double degree) and turnLeft(double degree) turn the

robot by a specified degree. ahead(double distance) and back(double distance) move the robot

by the specified distance; these two methods return early if the robot hits a wall or another robot.

turnGunRight(double degree) and turnGunLeft(double degree) turn the gun, independent of the vehicle's direction.

turnRadarRight(double degree) and turnRadarLeft(double degree) turn the radar on top of the gun, independent of the gun's direction (and the vehicle's direction).

Page 5: My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move

How to build a robot

public class GenericRobot extends Robot { public void run() {

while(true) {

}

}

}

Page 6: My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move

Useful Event-handlers onScannedRobot() - this method is called

when the radar detects a robot. onHitByBullet() - this method is called when

the robot is hit by a bullet. onHitRobot() - this method is called when

your robot hits another robot. onHitWall() method; this method is called

when your robot hits a wall.

Page 7: My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move

ProblemWhat does this robot do?

import robocode.*; public class MySecondRobot extends Robot {     public void run() {         turnLeft(getHeading());         while(true) {             ahead(1000);             turnRight(90);         }     }     public void onScannedRobot(ScannedRobotEvent e) {         fire(1);     }     public void onHitByBullet(HitByBulletEvent e) {         turnLeft(180);     } }

Page 8: My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move

Solution

The robot begins by driving straight up (turnLeft(getHeading())). It turns right 90 degrees whenever it hits something, so it moves orthogonally in a large rectangle. If hit by a bullet it changes direction (turnLeft(180)).