Difference between revisions of "SuperWalls"

From Robowiki
Jump to navigation Jump to search
(New page: 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. == Moveme...)
 
Line 13: Line 13:
  
 
<pre>
 
<pre>
package wiki.superSampleBot;
+
package wiki.SuperSampleBot;
  
 
import robocode.HitRobotEvent;
 
import robocode.HitRobotEvent;
Line 100: Line 100:
 
LGRating=LGHits/LGShots;
 
LGRating=LGHits/LGShots;
 
if(roundNum==1){//In the first round use linear targeting
 
if(roundNum==1){//In the first round use linear targeting
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/11));//Turn our gun
+
if(e.getEnergy()>=16){
 +
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/11));//Turn our gun
 +
}
 +
else{
 +
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/(e.getEnergy()/4)));//Turn our gun
 +
}
 
LGShots=LGShots+1;
 
LGShots=LGShots+1;
 
gunIdent=111;
 
gunIdent=111;
 +
setFire(e.getEnergy()/4);//Fire the minimum amount of energy needed to finish off the other robot
 
}
 
}
 
else if(roundNum==2){//in the second use head-on
 
else if(roundNum==2){//in the second use head-on
Line 108: Line 114:
 
HGShots=HGShots+1;
 
HGShots=HGShots+1;
 
gunIdent=222;
 
gunIdent=222;
 +
setFire(e.getEnergy()/4);//Fire the minimum amount of energy needed to finish off the other robot
 
}
 
}
 
else{//after that...
 
else{//after that...
 
if(LGRating>HGRating){//use whichever gun has the highest rating
 
if(LGRating>HGRating){//use whichever gun has the highest rating
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/11));//Turn our gun
+
if(e.getEnergy()>=16){
 +
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/11));//Turn our gun
 +
}
 +
else{
 +
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/(e.getEnergy()/4)));//Turn our gun
 +
}
 
LGShots=LGShots+1;
 
LGShots=LGShots+1;
 
gunIdent=111;
 
gunIdent=111;
 +
setFire(e.getEnergy()/4);//Fire the minimum amount of energy needed to finish off the other robot
 
}
 
}
 
else if(LGRating<=HGRating){
 
else if(LGRating<=HGRating){
Line 119: Line 132:
 
HGShots=HGShots+1;
 
HGShots=HGShots+1;
 
gunIdent=222;
 
gunIdent=222;
 +
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
setFire(e.getEnergy()/4);//Fire the minimum amount of energy needed to finish off the other robot
 
 
}
 
}
 
public void onBulletHit(BulletHitEvent e){//add a hit to the ranking of whatever gun we were using
 
public void onBulletHit(BulletHitEvent e){//add a hit to the ranking of whatever gun we were using
Line 134: Line 147:
 
}
 
}
 
}
 
}
 +
 
</pre>
 
</pre>

Revision as of 19:22, 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.


Movement

SuperWalls uses the exact same movement as Walls.


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.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import robocode.*;
import java.awt.*;
import robocode.util.Utils;

/**
 * SuperWalls - a sample robot by CrazyBassoonist based on the sample robot Walls by Mathew Nelson and maintained by Flemming N. Larsen
 * <p/>
 * Moves around the outer edge with two targeting systems
 */
public class SuperWalls extends AdvancedRobot {
	static double HGShots;//Number of shots with Head-On Targeting
	static double LGShots;//Number of shots with Linear Targeting
	static double HGHits;//Number of hits with Head-On Targeting
	static double LGHits;//Number of hits with Linear Targeting
	static double HGRating;//Head-on Targeting gun's rating
	static double LGRating;//Linear gun's rating
	static double roundNum;//Number of rounds
	double gunIdent;//Used to tell which gun we are using
	boolean peek; // Don't turn if there's a robot there
	double moveAmount; // How much to move

	/**
	 * run: Move around the walls
	 */
	public void run() {
		roundNum=roundNum+1;//update number of rounds
		setAdjustRadarForRobotTurn(true);//keeps our radar from moving with our gun
		setAdjustRadarForGunTurn(true);//keeps our radar from moving with our robot
		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;
		turnRight(90);
		while (true) {
			setTurnRadarRightRadians(10000);
			// 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);
		}
	}

	/**
	 * 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 =getHeadingRadians() + e.getBearingRadians() - getRadarHeadingRadians();//The amount to turn our radar
		HGRating=HGHits/HGShots;//recalculate both gun's ratings
		LGRating=LGHits/LGShots;
		if(roundNum==1){//In the first round use linear targeting
			if(e.getEnergy()>=16){
				setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/11));//Turn our gun
			}
			else{
				setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/(e.getEnergy()/4)));//Turn our gun
			}
			LGShots=LGShots+1;
			gunIdent=111;
			setFire(e.getEnergy()/4);//Fire the minimum amount of energy needed to finish off the other robot
		}
		else if(roundNum==2){//in the second use head-on
			setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()));
			HGShots=HGShots+1;
			gunIdent=222;
			setFire(e.getEnergy()/4);//Fire the minimum amount of energy needed to finish off the other robot
		}
		else{//after that...
			if(LGRating>HGRating){//use whichever gun has the highest rating
				if(e.getEnergy()>=16){
					setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/11));//Turn our gun
				}
				else{
					setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/(e.getEnergy()/4)));//Turn our gun
				}
				LGShots=LGShots+1;
				gunIdent=111;
				setFire(e.getEnergy()/4);//Fire the minimum amount of energy needed to finish off the other robot
			}
			else if(LGRating<=HGRating){
				setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()));
				HGShots=HGShots+1;
				gunIdent=222;
				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
	}
	public void onBulletHit(BulletHitEvent e){//add a hit to the ranking of whatever gun we were using
		if(gunIdent==111){
			LGHits=LGHits+1;
		}
		else{
			HGHits=HGHits+1;
		}
	}
}