Talk:CalculatingScore/ScoreWithRamming

From Robowiki
Jump to navigation Jump to search
package <package>;

import java.util.Iterator;
import java.util.Vector;

import robocode.*;

/**
 * This is the Score class for duels.
 * 
 * Instantiate this once on match start, and at the top of all the needed events
 * just pass them to the instance. Call printScore at any time to print the
 * scores to the console. Call getScore(id) at any time to get the score of the
 * requested robot (0 = self, 1 = enemy).
 * 
 * If you'd like to use this in your robot, feel free to, but give credit!
 * 
 * @author Vuen (original)
 * @author Jab (contributor)
 */
public class Score {

	AdvancedRobot robot;

	public Score(AdvancedRobot robot) {
		this.robot = robot;
	}

	public double enemyEnergy;
	public double myEnergy;
	public String enemyName;

	public static double[] total = new double[2];

	public double[] survivalScore = new double[2];

	public double[] bulletDamageBonus = new double[2];
	public double[] bulletDamage = new double[2];

	public double[] ramDamageBonus = new double[2];
	public double[] ramDamage = new double[2];

	public void onScannedRobot(ScannedRobotEvent e) {
		myEnergy = robot.getEnergy();
		enemyEnergy = e.getEnergy();
		if (enemyName == null)
			enemyName = e.getName();
	}

	public void onBulletHit(BulletHitEvent e) {
		if (e.getEnergy() < 0.001) {
			System.out.println("Score.onBulletHit()");
			return;
		} //ignore if enemy dead
		bulletDamage[0] += robocode.Rules.getBulletDamage(e.getBullet()
				.getPower());
	}

	public void onHitByBullet(HitByBulletEvent e) {
		if (robocode.Rules.getBulletDamage(e.getPower()) > myEnergy) {
			System.out.println("Score.onHitByBullet()");
			return;
		} //ignore if self dead
		bulletDamage[1] += robocode.Rules.getBulletDamage(e.getBullet()
				.getPower());
	}

	public void onHitRobot(HitRobotEvent e) {
		ramDamage[e.isMyFault() ? 0 : 1] += robocode.Rules.ROBOT_HIT_DAMAGE;
	}

	
	public void onWin(WinEvent e) {

		Vector<Event> v = robot.getAllEvents();
		Iterator<Event> i = v.iterator();
		while (i.hasNext()) {
			Event event = i.next();
			System.out.println("Missed event: " + event.getClass());
			if (event instanceof BulletHitEvent)
				onBulletHit((BulletHitEvent) event);
			if (event instanceof HitByBulletEvent)
				onHitByBullet((HitByBulletEvent) event);
			if (event instanceof HitRobotEvent)
				onHitRobot((HitRobotEvent) event);
		}

		survivalScore[0] = 60;
		if (v.size() > 0)
			if (v.lastElement() instanceof BulletHitEvent)
				bulletDamageBonus[0] = (bulletDamage[0] + ramDamage[0]) * .2;
			else if (v.lastElement() instanceof HitRobotEvent)
				ramDamageBonus[0] = (bulletDamage[0] + ramDamage[0]) * .3;
		ramDamage[0] *= 2;
		ramDamage[1] *= 2;

		total[0] += survivalScore[0] + bulletDamageBonus[0] + bulletDamage[0]
				+ ramDamageBonus[0] + ramDamage[0];
		total[1] += survivalScore[1] + bulletDamageBonus[1] + bulletDamage[1]
				+ ramDamageBonus[1] + ramDamage[1];
		this.printScore();
	}

	
	public void onDeath(DeathEvent e) {

		Vector<Event> v = robot.getAllEvents();
		Iterator<Event> i = v.iterator();
		while (i.hasNext()) {
			Event event = i.next();
			System.out.println("Missed event: " + event.getClass());
			if (event instanceof BulletHitEvent)
				onBulletHit((BulletHitEvent) event);
			if (event instanceof HitByBulletEvent)
				onHitByBullet((HitByBulletEvent) event);
			if (event instanceof HitRobotEvent)
				onHitRobot((HitRobotEvent) event);
		}

		survivalScore[1] = 60;
		if (v.size() > 0)
			if (v.lastElement() instanceof BulletHitEvent)
				bulletDamageBonus[1] = (bulletDamage[1] + ramDamage[1]) * .2;
			else if (v.lastElement() instanceof HitRobotEvent)
				ramDamageBonus[1] = (bulletDamage[1] + ramDamage[1]) * .3;
		ramDamage[0] *= 2;
		ramDamage[1] *= 2;

		total[0] += survivalScore[0] + bulletDamageBonus[0] + bulletDamage[0]
				+ ramDamageBonus[0] + ramDamage[0];
		total[1] += survivalScore[1] + bulletDamageBonus[1] + bulletDamage[1]
				+ ramDamageBonus[1] + ramDamage[1];
		this.printScore();
	}

	/** returns the score of the requested robot: 0=self, 1=enemy */
	public int getScore(int id) {
		return (int) Math.round(total[id]);
	}

	/** prints the score-card to the console */
	public void printScore() {
		System.out.println("Total: " + Math.round(total[0]) + "  "
				+ robot.getName());
		double totalMe = survivalScore[0] + bulletDamageBonus[0]
				+ bulletDamage[0] + ramDamageBonus[0] + ramDamage[0];
		System.out
				.println("Total Survival  BulletDmg   BulletBonus  RamDmg*2  RamDmgBonus");
		System.out.println(Math.round(totalMe) + "   "
				+ Math.round(survivalScore[0]) + "         "
				+ Math.round(bulletDamage[0]) + "         "
				+ Math.round(bulletDamageBonus[0]) + "           "
				+ Math.round(ramDamage[0]) + "     "
				+ Math.round(ramDamageBonus[0]));

		System.out.println();

		System.out.println("Total: " + Math.round(total[1]) + "  " + enemyName);
		double totalEnemy = survivalScore[1] + bulletDamageBonus[1]
				+ bulletDamage[1] + ramDamageBonus[1] + ramDamage[1];
		System.out
				.println("Total Survival  BulletDmg   BulletBonus  RamDmg*2  RamDmgBonus");
		System.out.println(Math.round(totalEnemy) + "   "
				+ Math.round(survivalScore[1]) + "         "
				+ Math.round(bulletDamage[1]) + "         "
				+ Math.round(bulletDamageBonus[1]) + "           "
				+ Math.round(ramDamage[1]) + "     "
				+ Math.round(ramDamageBonus[1]));
	}

}
There are no threads on this page yet.