Difference between revisions of "SuperWalls"

From Robowiki
Jump to navigation Jump to search
(revise code)
Line 6: Line 6:
  
 
== Movement==
 
== Movement==
SuperWalls uses the exact same movement as Walls.
+
SuperWalls moves like Walls, but without a constant speed and it changes directions every now and then.
  
  
Line 32: Line 32:
 
boolean gunIdent;      //Used to tell which gun we are using
 
boolean gunIdent;      //Used to tell which gun we are using
 
double moveAmount;      // How much to move
 
double moveAmount;      // How much to move
 
+
double oldEnergy=100;  //Enemies old energy
 +
double newEnergy;  //Enemies new energy
 +
double energyChange;    //Energy Change
 +
int moveDirection=1;    //Which way to move
 
public void run() {
 
public void run() {
 
setAdjustRadarForRobotTurn(true);
 
setAdjustRadarForRobotTurn(true);
Line 44: Line 47:
 
// Initialize moveAmount to the maximum possible for this battlefield.
 
// Initialize moveAmount to the maximum possible for this battlefield.
 
moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
 
moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
 
setTurnRadarRight(Double.POSITIVE_INFINITY);
 
  
 
// turnLeft to face a wall.
 
// turnLeft to face a wall.
Line 52: Line 53:
 
turnLeft(getHeading() % 90);
 
turnLeft(getHeading() % 90);
 
ahead(moveAmount);
 
ahead(moveAmount);
 
// Turn the gun to turn right 90 degrees.
 
turnRight(90);
 
 
while (true) {
 
while (true) {
 
// Move up the wall
 
// Move up the wall
ahead(moveAmount);
+
ahead(100*moveDirection);
// Turn to the next wall
+
setTurnRadarRight(10000);
turnRight(90);
 
 
}
 
}
 
}
 
}
Line 83: Line 80:
 
double latVel = e.getVelocity()*Math.sin(e.getHeadingRadians()-absBearing);// The enemies lateral velocity
 
double latVel = e.getVelocity()*Math.sin(e.getHeadingRadians()-absBearing);// The enemies lateral velocity
 
double radarTurn = absBearing - getRadarHeadingRadians();// The amount to turn our radar
 
double radarTurn = absBearing - getRadarHeadingRadians();// The amount to turn our radar
 
 
double HGRating = (double) HGHits / HGShots;
 
double HGRating = (double) HGHits / HGShots;
 
double LGRating = (double) LGHits / LGShots;
 
double LGRating = (double) LGHits / LGShots;
 
+
newEnergy=e.getEnergy();
if (getRoundNum() == 0 || LGRating>HGRating){ // In the first round use linear targeting
+
energyChange=oldEnergy-newEnergy;
 +
if(energyChange>=0.1 && energyChange<=3&&Math.random()>.9){
 +
moveDirection=-moveDirection;
 +
}
 +
if (getRoundNum() == 0){ // In the first round use linear targeting
 
double bulletPower = Math.min(3, e.getEnergy() / 4);
 
double bulletPower = Math.min(3, e.getEnergy() / 4);
 
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians() + Math.asin(latVel / (20 - 3 * bulletPower))));// Turn our gun
 
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians() + Math.asin(latVel / (20 - 3 * bulletPower))));// Turn our gun
Line 93: Line 93:
 
gunIdent = true;
 
gunIdent = true;
 
setFire(bulletPower);// Fire the minimum amount of energy needed to finish off the other robot
 
setFire(bulletPower);// Fire the minimum amount of energy needed to finish off the other robot
} else { //in the second use head-on
+
}
 +
else if(getRoundNum()==1){ //in the second use head-on
 
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()));
 
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()));
 
HGShots++;
 
HGShots++;
 
gunIdent = false;
 
gunIdent = false;
 
setFire(e.getEnergy() / 4);// Fire the minimum amount of energy needed to finish off the other robot
 
setFire(e.getEnergy() / 4);// Fire the minimum amount of energy needed to finish off the other robot
 +
}
 +
else{
 +
if(LGRating>HGRating){
 +
double bulletPower = Math.min(3, e.getEnergy() / 4);
 +
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians() + Math.asin(latVel / (20 - 3 * bulletPower))));// Turn our gun
 +
LGShots++;
 +
gunIdent = true;
 +
setFire(bulletPower);// Fire the minimum amount of energy needed to finish off the other robot
 +
}
 +
else{
 +
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()));
 +
HGShots++;
 +
gunIdent = false;
 +
setFire(e.getEnergy() / 4);// Fire the minimum amount of energy needed to finish off the other robot
 +
}
 
}
 
}
 
setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn) * 2);// Make the radar lock on
 
setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn) * 2);// Make the radar lock on
 +
oldEnergy=e.getEnergy();
 
}
 
}
  
Line 108: Line 125:
 
else{
 
else{
 
HGHits = HGHits+1;
 
HGHits = HGHits+1;
 +
}
 +
}
 +
public void onHitWall(HitWallEvent e){
 +
if(moveDirection==1){
 +
turnRight(90);
 +
}
 +
else{
 +
turnLeft(90);
 
}
 
}
 
}
 
}
 
}
 
}
 
</pre>
 
</pre>

Revision as of 20:41, 1 March 2009

SuperWalls is a part of the Super Sample Bots set by CrazyBassoonist. It is intended to provide new robocoders with a new challenge after beating all of the sample robots.

Thanks to nat for cleaning up the bad coding caused by my horrible coding skills.


Movement

SuperWalls moves like Walls, but without a constant speed and it changes directions every now and then.


Targeting

SuperWalls uses two guns, a Linear Targeting gun and a Head-On Targeting gun. It keeps track of the number and of shots and hits of each gun, and uses whichever gun works better.

Code

package wiki.SuperSampleBot;

import robocode.*;
import robocode.util.*;
import java.awt.*;

/**
 * SuperWalls - a sample robot by CrazyBassoonist based on the sample robot Walls by Mathew Nelson and maintained by Flemming N. Larsen
 * Moves around the outer edge with two targeting systems
 */
public class SuperWalls extends AdvancedRobot {
	static int HGShots;     //Number of shots with Head-On Targeting
	static int LGShots;     //Number of shots with Linear Targeting
	static int HGHits;      //Number of hits with Head-On Targeting
	static int LGHits;      //Number of hits with Linear Targeting
	boolean gunIdent;       //Used to tell which gun we are using
	double moveAmount;      // How much to move
	double oldEnergy=100;   //Enemies old energy
	double newEnergy;   	//Enemies new energy
	double energyChange;    //Energy Change
	int moveDirection=1;    //Which way to move
	public void run() {
		setAdjustRadarForRobotTurn(true);
		setAdjustRadarForGunTurn(true);
		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());

		// turnLeft to face a wall.
		// getHeading() % 90 means the remainder of
		// getHeading() divided by 90.
		turnLeft(getHeading() % 90);
		ahead(moveAmount);
		while (true) {
			// Move up the wall
			ahead(100*moveDirection);
			setTurnRadarRight(10000);
		}
	}

	/**
	 * onHitRobot:  Move away a bit.
	 */
	public void onHitRobot(HitRobotEvent e) {
		// 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) {
		double absBearing = e.getBearingRadians()+getHeadingRadians();// The enemies location relative to us
		double latVel = e.getVelocity()*Math.sin(e.getHeadingRadians()-absBearing);// The enemies lateral velocity
		double radarTurn = absBearing - getRadarHeadingRadians();// The amount to turn our radar
		double HGRating = (double) HGHits / HGShots;
		double LGRating = (double) LGHits / LGShots;
		newEnergy=e.getEnergy();
		energyChange=oldEnergy-newEnergy;
		if(energyChange>=0.1 && energyChange<=3&&Math.random()>.9){
			moveDirection=-moveDirection;
		}
		if (getRoundNum() == 0){ // In the first round use linear targeting
			double bulletPower = Math.min(3, e.getEnergy() / 4);
			setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians() + Math.asin(latVel / (20 - 3 * bulletPower))));// Turn our gun
			LGShots++;
			gunIdent = true;
			setFire(bulletPower);// Fire the minimum amount of energy needed to finish off the other robot
			}
		 else if(getRoundNum()==1){ //in the second use head-on
			setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()));
			HGShots++;
			gunIdent = false;
			setFire(e.getEnergy() / 4);// Fire the minimum amount of energy needed to finish off the other robot
		}
		else{
			if(LGRating>HGRating){
				double bulletPower = Math.min(3, e.getEnergy() / 4);
				setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians() + Math.asin(latVel / (20 - 3 * bulletPower))));// Turn our gun
				LGShots++;
				gunIdent = true;
				setFire(bulletPower);// Fire the minimum amount of energy needed to finish off the other robot
			}
			else{
				setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()));
				HGShots++;
				gunIdent = false;
				setFire(e.getEnergy() / 4);// Fire the minimum amount of energy needed to finish off the other robot
			}
		}
		setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn) * 2);// Make the radar lock on
		oldEnergy=e.getEnergy();
	}

	public void onBulletHit(BulletHitEvent e){//add a hit to the ranking of whatever gun we were using
		if(gunIdent){
			LGHits = LGHits+1;
		}
		else{
			HGHits = HGHits+1;
		}
	}
	public void onHitWall(HitWallEvent e){
		if(moveDirection==1){
			turnRight(90);
		}
		else{
			turnLeft(90);
		}
	}
}