Skip to content

Instantly share code, notes, and snippets.

@berkes
Created September 25, 2015 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berkes/11003985694c3d9916df to your computer and use it in GitHub Desktop.
Save berkes/11003985694c3d9916df to your computer and use it in GitHub Desktop.
/**
* Copyright (c) 2001-2014 Mathew A. Nelson and Robocode contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://robocode.sourceforge.net/license/epl-v10.html
*/
package sample;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import java.awt.*;
/**
* Walls - a sample robot by Mathew Nelson, and maintained by Flemming N. Larsen
* <p/>
* Moves around the outer edge with the gun facing in.
*
* @author Mathew A. Nelson (original)
* @author Flemming N. Larsen (contributor)
*/
public class TweedeHelft extends Robot {
boolean peek; // Don't turn if there's a robot there
double moveAmount; // How much to move
double turnGunAngle; // How much to move
/**
* run: Move around the walls
*/
public void run() {
// Set colors
setBodyColor(Color.black);
setGunColor(Color.black);
setRadarColor(Color.orange);
setBulletColor(Color.cyan);
setScanColor(Color.cyan);
// Initialize moveAmount to the maximum possible for this battlefield.
moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
// Initialize peek to false
peek = false;
// turnLeft to face a wall.
// getHeading() % 90 means the remainder of
// getHeading() divided by 90.
turnLeft(getHeading() % 90);
ahead(moveAmount);
// Turn the gun to turn right 90 degrees.
peek = true;
turnGunRight(90);
turnRight(90);
while (true) {
// Look before we turn when ahead() completes.
peek = true;
// Move up the wall
ahead(moveAmount);
// Don't look now
peek = false;
// Turn to the next wall
turnRight(90);
// Make sure the Gun is pointing inwards
turnGunRight(getHeading() % 90));
}
}
/**
* onHitRobot: Move away a bit.
*/
public void onHitRobot(HitRobotEvent e) {
turnGunAngle = (getHeading() + 90 + e.getBearing()) + 180;
while (turnGunAngle >= 360) {
turnGunAngle -= 360;
}
//
if(turnGunAngle > 180) {
turnGunRight(turnGunAngle);
}
else {
turnGunLeft(360 - turnGunAngle);
}
fire(3);
// If he's in front of us, set back up a bit.
if (e.getBearing() > -90 && e.getBearing() < 90) {
back(100);
} // else he's in back of us, so set ahead a bit.
else {
ahead(100);
}
}
/**
* onScannedRobot: Fire!
*/
public void onScannedRobot(ScannedRobotEvent e) {
fire(2);
// Note that scan is called automatically when the robot is moving.
// By calling it manually here, we make sure we generate another scan event if there's a robot on the next
// wall, so that we do not start moving up it until it's gone.
if (peek) {
scan();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment