User:Starrynte/Melee Evaluator Source

From RoboWiki
< User:Starrynte
Revision as of 08:38, 1 July 2010 by RednaxelaBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
package Put_Your_Package_Here;
import robocode.*;
import java.util.Vector;
 
public class MeleeEvaluate
{
	static private double rating[][]; //holds the ratings
	static private int currentStage; //the stage we're in
	static private int totalOthers; //total other bots (in the beginning of the battle)
	static private double tmpeval;
	/*
	 * Ratings:
	 *    Survival: The average energy gain during a stage
	 *    Hitrate: The average hitrate during a stage
	 *    Infliction: The average damage inflicted during a stage
	 *    Damage: The average damage encountered during a stage
	 *    Survival up through a stage: How often you survive up to and through an ENTIRE stage
	 *    Survival in a stage only: How often your survive in one SINGLE stage
	 *    Skipped turns: The average number of skipped turns during a stage
	 *    Hit walls: The average number of wall hits during a stage
	 *
	*/
	static private AdvancedRobot robot; //the robot
	static private int[] bulletsFired; //bullets fired so far, by stage
	static private int[] rounds; //number of rounds in each stage
	static private int stages; //total stages
	static private final int NUMSTATS=7; //number of ratings
	int others=10; //robot.getOthers(): to prevent multiple printing up stats at the end of a battle
 
 
	public MeleeEvaluate(){
		throw new RuntimeException("YOU MUST SPECIFY EXACTLY ONE (1) ADVANCED ROBOT AS A PARAMETER!");		
	}
 
	public MeleeEvaluate(Robot robot){
		this();
	}	
 
	public MeleeEvaluate(AdvancedRobot bot){
		currentStage=0;
		if(rating==null){
			stages=(int)Math.ceil((bot.getOthers()+1)/2);
			rating=new double[stages][NUMSTATS];//5 default stages (98,76,54,32,1) ... with ratings described above
			bulletsFired=new int[stages];//see above
			rounds=new int[stages];//see above
		}
 
		robot=bot;
		this.currentStage=0;
		this.totalOthers=this.others=bot.getOthers();
	}
 
	public void execute(){
		if(this.others>-1 || robot.getOthers()>0){//the next 3 lines of code, including this one, along with the last one in this function, prevent multiple printing of stats
			this.others=robot.getOthers();			
		}
		Vector v=robot.getAllEvents();
		for(int i=0,size=v.size();i<size;i++){
			Object obj=v.get(i);
			if(obj instanceof DeathEvent){ onDeath((DeathEvent)obj); continue; }
			if(obj instanceof RobotDeathEvent){ onRobotDeath((RobotDeathEvent)obj); continue; }
			if(obj instanceof SkippedTurnEvent){ rating[currentStage][5]++; continue; }
			if(obj instanceof HitByBulletEvent){ onHitByBullet((HitByBulletEvent)obj); continue; }
			if(obj instanceof BulletHitEvent){ onBulletHit((BulletHitEvent)obj); continue; }
			if(obj instanceof BulletMissedEvent){ onBulletMissed((BulletMissedEvent)obj); continue; }
			if(obj instanceof HitRobotEvent){ onHitRobot((HitRobotEvent)obj); continue; }	
			if(obj instanceof HitWallEvent){ rating[currentStage][6]++; continue; }
			if(obj instanceof BulletHitBulletEvent){ onBulletHitBullet((BulletHitBulletEvent)obj); }
		}
 
		if(this.others==0){ rating[currentStage][4]++; onDeath(null); this.others=-1; }
	}
 
	private static void onRobotDeath(RobotDeathEvent e){
		int tempStage=currentStage;
		currentStage=(totalOthers - robot.getOthers())>>1; //currentStage with 10 bots: 9,8 - 0 | 7,6 - 1 | 5,4 - 2 | 3,2 - 3 | 1 - 4 
		if(tempStage+1==currentStage && robot.getOthers()>0){ //if the stage changed AND there are robots left (no robots left handled later)						
			rating[tempStage][4]++; //update survival stage
			rounds[currentStage]++;	
		}
	}
 
	private static void onHitByBullet(HitByBulletEvent e){
		tmpeval=(((tmpeval=e.getBullet().getPower())>1) ? (tmpeval*4) + (2*(tmpeval-1)):tmpeval*4);
		rating[currentStage][0]-=tmpeval; //update survival using formula
		rating[currentStage][3]+=tmpeval; //update damage using formula
	}
 
	private static void onBulletHit(BulletHitEvent e){
		tmpeval=e.getBullet().getPower();
		rating[currentStage][0]+=tmpeval*2; //update survival: you lose <power> amount of energy for shooting, but you get 3*<power> energy back
		rating[currentStage][1]=((rating[currentStage][1]*((double)bulletsFired[currentStage]))+1)/(++bulletsFired[currentStage]); //update hitrate: first calculate the bullets hit based on hitrate before, then update new hitrate and bullets fired
		rating[currentStage][2]+=((tmpeval>1) ? (tmpeval*4) + (2*(tmpeval-1)):tmpeval*4); //update damage inflicted using formula
	}
 
	private static void onBulletMissed(BulletMissedEvent e){
		rating[currentStage][0]-=e.getBullet().getPower(); //update survival: energy expended for firing
		rating[currentStage][1]=(rating[currentStage][1]*((double)bulletsFired[currentStage]))/(++bulletsFired[currentStage]); //update hitrate: first calculate the bullets hit based on hitrate before, then update new hitrate and bullets fired
	}
 
	private static void onBulletHitBullet(BulletHitBulletEvent e){
		rating[currentStage][0]-=e.getBullet().getPower(); //update survival: energy expended for firing
		//don't need to update hitrate: bulletHitBullet isn't entirely your fault
	}
 
	private static void onHitRobot(HitRobotEvent e){
		rating[currentStage][0]-=0.6d; //update survival: robot loses 0.6 energy
		rating[currentStage][2]+=0.6d; //update infliction: hit robot loses 0.6 energy
		rating[currentStage][3]+=0.6d; //update damage: i lose 0.6 energy
	}
 
	private static void onDeath(DeathEvent e){
		rounds[0]++;
		printStats();
	}
 
	public double[][] getRawStats(){
		return rating;
	}
 
	public double[][] getStats(){
		double[][] stats=rating;
		int round=robot.getRoundNum()+1;
		for(int i=0;i<stages;i++){
			for(int j=0;j<NUMSTATS;j++){
				if(j==1){ continue; } //don't average over round for hitrate
				stats[i][j]/=round;
			}
		}
		return stats;
	}
 
 
	public static void printStats(){
		final int round=robot.getRoundNum()+1;		
		robot.out.println();
		for(int i=0;i<stages;i++){
			robot.out.println("Stage " + (i+1) + ":");
			robot.out.println("Average energy gain: " + (rating[i][0]/round));
			robot.out.println("Average hitrate: " + rating[i][1]);
			robot.out.println("Average inflicted damage: " + (rating[i][2]/round));
			robot.out.println("Average damage encountered: " + (rating[i][3]/round));
			robot.out.println("Average survival up through this stage: " + (rating[i][4]/round));
			robot.out.println("Average survival in this stage only: " + (rating[i][4]/rounds[i]));
			robot.out.println("Average skipped turns: " + (rating[i][5]/round));
			robot.out.println("Average wall hits: " + (rating[i][6]/round));
		}
	}	
 
}
Personal tools