Skip to content
Snippets Groups Projects
BadBot.java 4.8 KiB
Newer Older
Martin Vítek's avatar
Martin Vítek committed
/*
 * ----------------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <martinv@students.zcu.cz> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Martin Vítek
 * ----------------------------------------------------------------------------------
 */
package cz.zcu.martinv.MyRobocodeRobots;

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


/**
 * Simple robot, which is realy bad.
 * Robots bahaviour is based on hard-coded behaviour and state machine.
 * 
 * @author Martin Vítek
 */
public class BadBot extends AdvancedRobot
{
    private enum States
    {
        SCANNING, APROACHING, FIGHTING, RUNNING, RAMMING, UNCOLISIONING;
    }

    private RobotStatus status;
    private States state;
    private ArrayList<EnemyRobotStatus> enemyRobots;
    
    /**
     * Main method - here is all robot logic.
     * It's called by RoboCode.
     */
    @Override
    public void run()
    {
        out.format("Hi, I'm your bad robot!%n");
        
        //Robot initialization
        setBodyColor(Color.GREEN);
        setRadarColor(Color.WHITE);
        setGunColor(Color.BLACK);
        setScanColor(Color.YELLOW);
        
        setAdjustGunForRobotTurn(true);     //Gun movement is independent on body
        setAdjustRadarForGunTurn(true);     //Radar movement is independent on body
        setAdjustRadarForRobotTurn(true);   //Radar movement is independent on gun
        
        enemyRobots = new ArrayList<EnemyRobotStatus>(2);
        
        //Start scaning other robots
        
        state = States.SCANNING;
        
        while(true)
        {
            switch(state)
            {
                case SCANNING: scanning();
                    break;
                
                case APROACHING: aproaching();
                    break;
             /*       
                case FIGHTING: fighting();
                    break;
                    
                case RUNNING: running();
                    break;
                    
                case RAMMING: ramming();
                    break;
                    
                case UNCOLISIONING: uncolisioning();
                    break;
            */
            }
        }
    }
    /**
     * This method is called when another robot is scanned.
     * It's called by RoboCode.
     * @param e passed by RoboCode 
     */    
    @Override
    public void onScannedRobot(ScannedRobotEvent e)
    {
        out.format("Found: %s%n", e.getName());
        
        switch(state)
        {
            case SCANNING: scanningOnScannedRobot(e);
                    break;
                
                case APROACHING: aproachingOnScannedRobot(e);
                    break;
        }
            
        
        //scan();
    }
    
    /**
     * This method is called every turn.
     * It's called by RoboCode.
     * @param e passed by RoboCode 
     */
    @Override
    public void onStatus(StatusEvent e)
    {
        status = e.getStatus();
        
        //checkWalls();
    }
    
    @Override
    public void onBulletHit(BulletHitEvent e)
    {
        
    }
    
    @Override
    public void onHitByBullet(HitByBulletEvent e)
    {
        
    }
    
    @Override
    public void onHitRobot(HitRobotEvent e)
    {
        
    }
    
    @Override
    public void onHitWall(HitWallEvent e)
    {
        turnRight(180.0);
    }
    
    /**
     * This method is called when this robot wins.
     * It's called by RoboCode.
     * @param e passed by RoboCode
     */
    @Override
    public void onWin(WinEvent e)
    {
        turnRight(360);
    }
    
    private void scanning()
    {
        stop();
        turnRadarRight(36.0);
    }
    
    private void scanningOnScannedRobot(ScannedRobotEvent e)
    {
        if (enemyRobots.contains(new EnemyRobotStatus(e))) return;
        
        enemyRobots.add(new EnemyRobotStatus(e));
        
        //We have all robots in current battle
        if (enemyRobots.size() == getOthers())
        {
            out.format("We have scanned all (%d) enemy robots%n", enemyRobots.size());
            state = States.APROACHING;
        }
    }
    
    private void aproaching()
    {
        ahead(500.0);
    }
    
    private void aproachingOnScannedRobot(ScannedRobotEvent e)
    {
        
    }
    
    private void checkWalls()
    {
        if ( (status.getX() < 1.8*getWidth()) ||
             (status.getX() > (getBattleFieldWidth()-1.8*getWidth())) ||
             (status.getY() < 1.8*getHeight()) ||
             (status.getY() > (getBattleFieldHeight()-1.8*getHeight())) )
        {
            stop();
            turnRight(90.0);
            ahead(40000);
        }
    }
}