BlackWidow

From Robowiki
Jump to navigation Jump to search

Background Information

BlackWidow
Blackwidow.jpg
Its venom is stronger than a cobra's.
Author(s) User:Robar
Targeting SymbolicPatternMatching
Movement Stop And Go + Random Movement
Released March, 2009
Current Version 1.3
Code License RWLPCL
Download


Bot Name
BlackWidow
Author
User:Robar
Extends
AdvancedRobot
What's special about it?
It's a Stop And Go & Random Movement plus SymbolicPatternMatching nano.
Great, I want to try it. Where can I download it?
1.0 - http://robocode-archive.strangeautomata.com/robots/robar.nano.BlackWidow_1.0.jar
1.15 - http://robocode-archive.strangeautomata.com/robots/robar.nano.BlackWidow_1.15.jar
1.2 - http://robocode-archive.strangeautomata.com/robots/robar.nano.BlackWidow_1.2.jar
1.3 - http://robocode-archive.strangeautomata.com/robots/robar.nano.BlackWidow_1.3.jar
How competitive is it?
Very. In Nano Rumble it's 6th overall, 6th in Survival and 3rd in PL. 40th in MicroRumble and 250th in General Rumble. (27-05-2009)

Strategy

How does it move?
Stop And Go + Random Movement
How does it fire?
SymbolicPatternMatching.
How does it dodge bullets?
Stop And Go, if fails, it switches to Random Movement, which changes direction randomly when enemy fires.
How does the melee strategy differ from One-on-one strategy?
It's only a one-on-one bot.
What does it save between rounds and matches?
Static variables, as always.

Additional Information

Where did you get the name?
Small, but very dangerous - this's the perfect name for a spider-geek. :P
Can I use your code?
RWLPCL I commented the PM code in much detail in order to make beginners understand the code easier.
What's next for your robot?
I must have left out setAdjustGunForRobotTurn to get under 250 bytes. I should squeeze the code more to put it back, because it scores badly against stopNgo. (75-80% instead of 90-95) (I realised that without setAdjustGun it DOES score better than with it)
Optimize firepower and movement code.
Does it have any White Whales?
NanoAndrew, NanoLaulektric, NanoSatan, Acero, Splinter, NanoDeath, WeekendObsession etc...
What other robot(s) is it based on?
It has FunkyChicken's gun.
It has WeekendObsession's pm gun with some little modifications.


Version history

1.0

  • Symbolic Pattern Matcher gun, 36px Stop And Go, enemy fire-sensitive random movement, 2.5 firepower.
The code 1.0
package robar.nano;

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

public class BlackWidow extends AdvancedRobot{

	//Made by HUNRobar
	//You can contact me at testwiki.roborumble.org, or via e-mail to robar06@gmail.com
    //Global
    
    static StringBuffer enemyLog = new StringBuffer("000000000000000000000000000000"); //The list where enemy lateral velocites are stored

    //Constants
    static final double FIREPOWER = 2.5; //The firepower of the bot
    static final double BULLETVEL = 20-3*FIREPOWER; //The velocity of the bullet fired at FIREPOWER
    static final int PATTERN_DEPTH = 30; //The lenght of the pattern we try to find each turn in enemyLog
    static final double MOVEAMOUNT = 36; //The amount of pixels the bot will move in stop and go

    //Variables
    static double prevEnergy; //It stores the energy of the enemy
    static double direction = MOVEAMOUNT; //It's the direction and the movement packed together
    static int deathCount; //The number of deaths of our robot

    public void run(){
        //setAdjustGunForRobotTurn(true); //This sets the gun independent from the body. Extracted it due to codesize

    	//Black Widow-like colors: ;)
    	//setAllColors(Color.black);
    	//Infinite radar-lock
        setTurnRadarRight(Double.POSITIVE_INFINITY);
    }

    public void onScannedRobot(ScannedRobotEvent e){

            //Local variables

            double absB; //absolute bearing
            int index; //index of the match of the pm gun
            int matchLenght = PATTERN_DEPTH; //the number of data a pattern contains


            /*The pattern matcher gun
             *
             *It's a bit codesize-optimized version of Assertive's gun which is descend from FunkyChicken
             *I comment the gun in detail in order to make new robocoders more understandable. I'd have been
             *very glad if there had been such comments when I was learning symbolic pm. ;)
            */

            //Adding a new entry in the beginning of our pattern-list
            enemyLog.insert(0, (char)((int)(Math.sin(e.getHeadingRadians() - (absB=e.getBearingRadians()+getHeadingRadians()))*e.getVelocity())));

            //Reducing pattern lenght until there is a match in the list. The pattern is the first 'matchlenght' entries in the list.
            while ((index = enemyLog.toString().indexOf(enemyLog.substring(0, matchLenght--), 1)) < 0);

            //Now matchLenght will be the index of the pattern we estimate the enemy will do in the next turns.
            //The lenght of the etimated pattern equals the time our bullet reaching the enemy.
            matchLenght = index - (int)(e.getDistance()/BULLETVEL);


            //Converting lateral velocities of the estimated pattern to angular velocities, then adding them to absolute bearing
            do{
                absB += Math.asin(((byte)enemyLog.codePointAt(index--))/e.getDistance());
            }while(index >= Math.max(0, matchLenght));

            //Turning gun
            setTurnGunRightRadians(Utils.normalRelativeAngle(absB-getGunHeadingRadians()));

            //Fire in the hall!
            setFire(FIREPOWER);

            
            //Movement: Stop and Go, if it fails, simple random movement

            if(prevEnergy > (prevEnergy = e.getEnergy()) ){

                if(deathCount > 2)  direction = (Math.random()*10000-5000);
                setAhead(direction);
                
            }

            setTurnRightRadians(Math.cos(e.getBearingRadians()));

            setTurnRadarLeft(getRadarTurnRemaining());
    }
    public void onHitWall(HitWallEvent e){
        direction=-direction;
    }

    public void onDeath(DeathEvent e){
        deathCount++;
    }
}

1.3

  • WeekendObsession's gun, improved starter log, new log entries are rounded instead of calling (int), stopNgo moves 37px, stopNgo works for 2 deaths, added nice colors. ;)
The code 1.3
//NOTE: I DELETED ALL UNNECESSARY COMMENTS AND CORRECTED MISTAKES IN THE WIKI VERSION OF THE CODE. THE CODE INCLUDED THE JAR FILE IS LESS UNDERSTANDABLE.
package robar.nano;
import robocode.*;
import robocode.util.*;
import java.awt.*;

public class BlackWidow extends AdvancedRobot{

	//Made by HUNRobar
	//You can contact me at testwiki.roborumble.org, or via e-mail to robar06@gmail.com
	//Credits: for the whole wiki community! Especially for Simonton's WeekendObsession for the gun and the structural idea.
	
	/*VERSION HISTORY:
	 * 1.3 - With WeekendObsession's gun, rounded lateral velocities, improved starting log, nice colors, stopNgo death limit is 2 instead of 3
	 * 
	 * 1.2 - Turned back to original random movement, kept setAdjust but left out distance-control. Little energy management added.
	 * 
	 * 1.15 - Simplified distance-control, normal BFTIME prediction redone. 
	 * 
	 * 1.1 - Replaced the gun with WeekendObsession's. Managed to add distance-control and setAdjustGun. 2 days of hard code-squeezing work. (:^¤
	 *
	 * 1.0 - StopNGO plus Random movement, FunkyChicken's gun.
	 */
    //Global   
    static String enemyLog = "000000000000000000000000000000888888888888888765432100888765432101234567888765432100"; //The list where enemy lateral velocities are stored

    //Constants
    static final double FIREPOWER = 2.5; //The firepower of the bot
    static final double BULLETVEL = 20-3*FIREPOWER; //The velocity of the bullet fired at FIREPOWER
    static final int PATTERN_DEPTH = 30; //The length of the pattern we try to find each turn in enemyLog
    static final double MOVEAMOUNT = 37.0; //The amount of pixels the bot will move in stop and go
    static final int DEATHLIMIT = 1; //Do StopNGo until 2 deaths

    //Variables
    static double prevEnergy; //It stores the energy of the enemy
    static double direction = MOVEAMOUNT; //It's the direction and the movement packed together
    static int deathCount; //The number of deaths of our robot

    public void run(){

    	//Black Widow-like colors: ;)
    	setColors(Color.black, Color.red, Color.black);
    	
        //Infinite radar-lock
        setTurnRadarRight(Double.POSITIVE_INFINITY);
    }

    public void onScannedRobot(ScannedRobotEvent e){

            //Local variables 
   		int matchLenght = PATTERN_DEPTH; //the number of data a pattern contains, then the counter in the absB updater loop  		 
    		double absB; //absolute bearing 
    		int i; 		 		    		
    		int index; //index of the match of the pm gun

    		  
                          
            
            
    	  //Note: the pm gun is now derived from WeekendObsession. It's located under the movement now to save some codesize on absB.
              
          //Movement: Stop and Go, if it fails, simple random movement

            if(prevEnergy > (prevEnergy = e.getEnergy()) ){
                if(deathCount > DEATHLIMIT){
                	//direction = (40000.0*(Math.random() - Math.random()));
                	direction = (Math.random()*100000-50000);
                	//setMaxVelocity(16.0*Math.random());
                	//direction = Math.random()*80000-60000;
                }
                setAhead(direction);
            }
            setTurnRightRadians((Math.cos(absB = e.getBearingRadians())));

            // Now with the pm gun of Simonton's WeekendObsession
            
            //Inserting the newest lateral velocity into the beginning of our log
            enemyLog = String.valueOf( (char)Math.round(e.getVelocity() * Math.sin(e.getHeadingRadians() - ( absB+=getHeadingRadians() )))).concat(enemyLog);

            //Reducing pattern depth until find a match. Store the index of the match.
            while((index = enemyLog.indexOf(enemyLog.substring(0, matchLenght--), (i = (int)((e.getDistance())/BULLETVEL)))) < 0);

            //Add the angular velocities of the first BFTIME lateral velocities to absolute bearing.  
            do{
                absB += Math.asin(((byte)enemyLog.charAt(index--))/e.getDistance());
            }while(--i > 0);

            //Turning gun to head the predicted absolute bearing
            setTurnGunRightRadians(Utils.normalRelativeAngle(absB-getGunHeadingRadians()));
            
            //Fire in the hall!
            setFire(FIREPOWER);

            setTurnRadarLeft(getRadarTurnRemaining());
    }
    public void onHitWall(HitWallEvent e){
        direction =-direction;
    }

    public void onDeath(DeathEvent e){
        deathCount++;
    }

}