package DL;
import robocode.*;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import java.util.Random;
//import java.awt.Color;
/**
* Spaz - a robot by Drew Langston
*/
public class Spaz extends Robot
{
/**
* run: Spaz's default behavior
*/
public void run() {
// Initialization of the robot should be put here
setAdjustGunForRobotTurn(true);
Random ranGen = new Random();
String move = null;
int move1 = 0;
boolean wallClose = false;
// Robot main loop
while(true) {
//These if statements let the robot know if it's close to a wall.
if (getX() < 20){
wallClose = true;
if (0 <= getHeading() && getHeading() <= 90){
turnRight(45);
ahead(50);
}
else{
turnLeft(45);
ahead(50);
}
}
if (getBattleFieldWidth() - getX() < 20){
wallClose = true;
if (180 <= getHeading() && getHeading() <= 270){
turnRight(45);
ahead(50);
}
else{
turnLeft(45);
ahead(50);
}
}
if (getY() < 20){
wallClose = true;
if (180 <= getHeading() && getHeading() <= 270){
turnLeft(45);
ahead(50);
}
else{
turnRight(45);
ahead(50);
}
}
if (getBattleFieldHeight() - getY() < 20){
wallClose = true;
if ( 0 <= getHeading() && getHeading() <= 90){
turnLeft(45);
ahead(50);
}
else{
turnRight(45);
ahead(50);
}
}
//generate random number and shorten it to a practical value for moving and turning.
move = Integer.toString(Math.abs(ranGen.nextInt())).substring(0, 3);
move1 = Integer.parseInt(move);
System.out.println(" (Run Loop) Ahead");
ahead(move1);
if(wallClose == false){
System.out.println("(Run loop) Turn, Back, turn");
turnRight(move1 / 4);
back(move1);
turnLeft(move1 / 4);
}
}
}
/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
// Replace the next line with any behavior you would like
System.out.println("Scan activated");
double turnGunAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
if(e.getDistance() < 200 && getEnergy() > 50)
{
System.out.println("Fire 3, " + turnGunAmt + " " + e.getDistance());
turnGunRight(turnGunAmt);
fire(3);
}
else if (e.getDistance() < 300 && getEnergy() > 50)
{
System.out.println("Fire 2, " + turnGunAmt + " " + e.getDistance());
turnGunRight(turnGunAmt);
fire(2);
}
else
{
System.out.println("Too far, " + turnGunAmt + " " + e.getDistance());
turnRight(turnGunAmt);
ahead(200);
scan();
}
scan();
}
/**
* onHitByBullet: What to do when you're hit by a bullet
*/
public void onHitByBullet(HitByBulletEvent e) {
System.out.println("Hit by bullet. Evasive manuvers");
turnRight(20);
ahead(10);
turnRight(20);
ahead(20);
turnRight(30);
ahead(20);
}
/**
* onHitWall: What to do when you hit a wall
*/
public void onHitWall(HitWallEvent e) {
// Replace the next line with any behavior you would like
System.out.println("Hit wall. Turning.");
turnRight(90);
ahead(50);
}