Difference between revisions of "BulletSimBot/Current Code"
Jump to navigation
Jump to search
Awesomeness (talk | contribs) (Showing my messed up code) |
Awesomeness (talk | contribs) (Fixing code) |
||
Line 1: | Line 1: | ||
This is big, but here it is... | This is big, but here it is... | ||
<pre>package awesomeness; | <pre>package awesomeness; | ||
− | |||
import robocode.*; | import robocode.*; | ||
import robocode.util.*; | import robocode.util.*; | ||
Line 26: | Line 25: | ||
ArrayList<VirtualBullet> bullets = new ArrayList<VirtualBullet>(); | ArrayList<VirtualBullet> bullets = new ArrayList<VirtualBullet>(); | ||
VirtualBullet newBullet, bullet; | VirtualBullet newBullet, bullet; | ||
− | |||
public void run() { | public void run() { | ||
− | |||
− | |||
− | |||
scannedRobotX = -1; | scannedRobotX = -1; | ||
Line 70: | Line 65: | ||
newBullet = new VirtualBullet(scannedRobotX, scannedRobotY, 20 - 3 * changeInEnergy, Utils.normalRelativeAngle(absoluteBearing + Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * changeInEnergy)))); | newBullet = new VirtualBullet(scannedRobotX, scannedRobotY, 20 - 3 * changeInEnergy, Utils.normalRelativeAngle(absoluteBearing + Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * changeInEnergy)))); | ||
− | |||
bullets.add(newBullet); | bullets.add(newBullet); | ||
− | + | ||
+ | |||
} | } | ||
Line 90: | Line 85: | ||
} | } | ||
− | |||
///////////////////End Bullet Code///////////////////// | ///////////////////End Bullet Code///////////////////// | ||
− | + | //Stay at almost right angles | |
− | + | //0setTurnRightRadians(Utils.normalRelativeAngle(absoluteBearing + Math.PI/2 - getHeadingRadians())); | |
− | + | ||
− | + | //setAhead(movementDirection); | |
+ | |||
+ | //if (Math.random() > 0.92) i(); | ||
Line 137: | Line 133: | ||
movementDirection = -movementDirection; | movementDirection = -movementDirection; | ||
} | } | ||
− | + | ||
− | |||
− | |||
// Paint a transparent square on top of the last scanned robot | // Paint a transparent square on top of the last scanned robot | ||
public void onPaint(Graphics2D g) { | public void onPaint(Graphics2D g) { | ||
Line 156: | Line 150: | ||
if(scannedRobotX == -1 && scannedRobotY == -1) { //If we have no target | if(scannedRobotX == -1 && scannedRobotY == -1) { //If we have no target | ||
g.setColor(Color.green); //Draw the target | g.setColor(Color.green); //Draw the target | ||
− | g.drawString("No target", | + | g.drawString("No target", 10, 10); |
} | } | ||
else { //If we do have a target | else { //If we do have a target | ||
g.setColor(new Color(255, 96, 0)); //Draw the target | g.setColor(new Color(255, 96, 0)); //Draw the target | ||
− | g.drawString("Target locked", | + | g.drawString("Target locked", 10, 10); |
g.setColor(new Color(255, 32, 0)); //Draw the target | g.setColor(new Color(255, 32, 0)); //Draw the target | ||
g.drawLine( (int) scannedRobotX, (int) getBattleFieldHeight(), (int) scannedRobotX, 0); | g.drawLine( (int) scannedRobotX, (int) getBattleFieldHeight(), (int) scannedRobotX, 0); | ||
g.drawLine( (int) getBattleFieldWidth(), (int) (getBattleFieldHeight()-scannedRobotY), 0, (int) (getBattleFieldHeight()-scannedRobotY)); | g.drawLine( (int) getBattleFieldWidth(), (int) (getBattleFieldHeight()-scannedRobotY), 0, (int) (getBattleFieldHeight()-scannedRobotY)); | ||
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
− | + | ||
− | |||
− | |||
− | |||
public void onHitByBullet(HitByBulletEvent e) { previousEnergy += 3 * e.getBullet().getPower(); } | public void onHitByBullet(HitByBulletEvent e) { previousEnergy += 3 * e.getBullet().getPower(); } | ||
public void onBulletHit(BulletHitEvent e) { previousEnergy -= 4 * e.getBullet().getPower() + Math.max(0,2 * (e.getBullet().getPower() - 1)); } | public void onBulletHit(BulletHitEvent e) { previousEnergy -= 4 * e.getBullet().getPower() + Math.max(0,2 * (e.getBullet().getPower() - 1)); } | ||
Line 179: | Line 166: | ||
− | + | public class VirtualBullet { //A class to simulate bullets. | |
− | |||
− | |||
− | |||
− | public class VirtualBullet { //A class to simulate bullets. | ||
− | |||
double x, y, speed, angle; | double x, y, speed, angle; | ||
Line 192: | Line 174: | ||
x = startX; | x = startX; | ||
y = startY; | y = startY; | ||
− | speed = velocity | + | speed = velocity; |
angle = trajectory; | angle = trajectory; | ||
− | |||
} | } | ||
Line 228: | Line 209: | ||
} | } | ||
} | } | ||
− | + | ||
− | + | }</pre> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 23:13, 4 May 2009
This is big, but here it is...
package awesomeness; import robocode.*; import robocode.util.*; import java.util.Random; import java.util.ArrayList; import java.awt.*; /** * PwnBot - a robot by (your name here) */ public class PwnBot extends AdvancedRobot { double previousEnergy = 100d; double changeInEnergy; double scannedRobotX, scannedRobotY; int movementDirection = 50; long lastTime; //The last time known, used to track bullets Random generator = new Random(); //This makes random numbers ArrayList<VirtualBullet> bullets = new ArrayList<VirtualBullet>(); VirtualBullet newBullet, bullet; public void run() { scannedRobotX = -1; scannedRobotY = -1; setBodyColor(Color.red); setGunColor(Color.darkGray); setRadarColor(Color.red.darker()); setBulletColor(Color.red); lastTime = 0; setAdjustGunForRobotTurn(true); setAdjustRadarForGunTurn(true); setAdjustRadarForRobotTurn(true); while(true) turnRadarRightRadians(Double.POSITIVE_INFINITY); } /** * onScannedRobot: What to do when you see another robot */ public void onScannedRobot(ScannedRobotEvent e) { //The absolute bearing, this is used a lot double absoluteBearing = e.getBearingRadians() + getHeadingRadians(); /////////////////////////////////////////////////////// ////////////////////Movement Code////////////////////// /////////////////////Bullet Code/////////////////////// scannedRobotX = getX() + Math.sin(absoluteBearing) * e.getDistance(); scannedRobotY = getY() + Math.cos(absoluteBearing) * e.getDistance(); //If there's a change in energy, it probably fired if ((changeInEnergy = previousEnergy - (previousEnergy = e.getEnergy())) > 0d && changeInEnergy<=3) { newBullet = new VirtualBullet(scannedRobotX, scannedRobotY, 20 - 3 * changeInEnergy, Utils.normalRelativeAngle(absoluteBearing + Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * changeInEnergy)))); bullets.add(newBullet); } for (int i = 0; i < bullets.size(); i++) { bullet = bullets.get(i); bullet.tick(getTime() - lastTime); bullets.set(i, bullet); if (bullets.get(i).checkCollide(getX(), getY())) { // I think this will work // bullets.remove(i--); // but usually I do bullets.remove(i); i--; } } ///////////////////End Bullet Code///////////////////// //Stay at almost right angles //0setTurnRightRadians(Utils.normalRelativeAngle(absoluteBearing + Math.PI/2 - getHeadingRadians())); //setAhead(movementDirection); //if (Math.random() > 0.92) i(); ////////////////////Movement Code////////////////////// /////////////////////////////////////////////////////// /////////////////////////////////////////////////////// ///////////////////////Gun Code//////////////////////// ///////////////////////Gun Code//////////////////////// /////////////////////////////////////////////////////// /////////////////////////////////////////////////////// /////////////////////Radar Code//////////////////////// setTurnRadarRightRadians(2 * Utils.normalRelativeAngle(absoluteBearing - getRadarHeadingRadians())); /////////////////////Radar Code//////////////////////// /////////////////////////////////////////////////////// lastTime = getTime(); } public void onHitWall(HitWallEvent e) { i(); } public void i() { movementDirection = -movementDirection; } // Paint a transparent square on top of the last scanned robot public void onPaint(Graphics2D g) { g.setColor(new Color(255, 255, 0)); for (int i = 0; i < bullets.size(); i++) { //Draw all the bullets bullet = bullets.get(i); g.fillOval( (int) bullet.getX() - 3, (int) (getBattleFieldHeight() - bullet.getY() - 3), 6, 6); } if(scannedRobotX == -1 && scannedRobotY == -1) { //If we have no target g.setColor(Color.green); //Draw the target g.drawString("No target", 10, 10); } else { //If we do have a target g.setColor(new Color(255, 96, 0)); //Draw the target g.drawString("Target locked", 10, 10); g.setColor(new Color(255, 32, 0)); //Draw the target g.drawLine( (int) scannedRobotX, (int) getBattleFieldHeight(), (int) scannedRobotX, 0); g.drawLine( (int) getBattleFieldWidth(), (int) (getBattleFieldHeight()-scannedRobotY), 0, (int) (getBattleFieldHeight()-scannedRobotY)); } } public void onHitByBullet(HitByBulletEvent e) { previousEnergy += 3 * e.getBullet().getPower(); } public void onBulletHit(BulletHitEvent e) { previousEnergy -= 4 * e.getBullet().getPower() + Math.max(0,2 * (e.getBullet().getPower() - 1)); } public void onHitRobot(HitRobotEvent e) { previousEnergy -= 0.6; } public class VirtualBullet { //A class to simulate bullets. double x, y, speed, angle; VirtualBullet(double startX, double startY, double velocity, double trajectory) { x = startX; y = startY; speed = velocity; angle = trajectory; } public void tick(long ticks) { //Moves the bullet the amount it would while(ticks > 0) { x += Math.sin(angle)*speed; y += Math.cos(angle)*speed; ticks --; } } public double getX() { return x; } public double getY() { return y; } public double getDistance(double botX, double botY) { return Math.sqrt(Math.pow(botX-x, 2)+Math.pow(botY-y, 2)); } public boolean checkCollide(double botX, double botY) { if (x < 0 || y < 0 || x > getBattleFieldWidth() || y > getBattleFieldHeight()) { return true; } if(botX-18<x && botX+18>x && botY-18<y && botY+18>y) { return true; } return false; } } }