robocode机器人1.0版

问题及代码:
package com.anbo.g1;
import robocode.*;

import java.awt.*;
import java.awt.Event;
import javax.lang.model.element.ExecutableElement;
import javax.swing.text.Document;
import robocode.AdvancedRobot;
import robocode.HitByBulletEvent;
import robocode.HitWallEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
public class YanAn extends AdvancedRobot
{
	private double eDist; //对方的距离
	private double move; //移动的距离
	private double radarMove = 45; //雷达移动的角度
	private double dFirePower; //火力
	boolean movingForward;

	/**
	 * run: Crazy's main run function
	 */
	public void run() 
	{
		eDist = 300;
		// 颜色
		setColors(Color.ORANGE, Color.BLUE, Color.RED, Color.WHITE, Color.GRAY);
		while(true)
		{
		    //每过一个周期,运动随机的距离
			double period = 4*((int)(eDist/80)); //周期;敌人越接近,周期越短,移动越频繁
			//周期开始,则移动
			if(getTime()%period == 0)
			{
				move = (Math.random()*2-1)*(period*8 - 25);
			    setAhead(move + ((move >= 0) ? 25: -25));
			}
			//避免撞墙
			double heading = getHeadingRadians(); //取得bot方向的弧度数
			double x = getX() + move*Math.sin(heading); //移动move后将要达到的x坐标
			double y = getY() + move*Math.cos(heading); //移动move后将要达到的y坐标
			double dWidth = getBattleFieldWidth(); //战场的宽度
			double dHeight = getBattleFieldHeight(); //战场的长度
			//当(x,y)超过指定的范围,则反向移动move
			if(x < 30 || x > dWidth-30 || y < 30 || y > dHeight-30)
			{
				setBack(move);
			}
		    turnRadarLeft(radarMove); //转动雷达
		    execute();
		}
	}

	public void onHitWall(HitWallEvent e) 
	{
		// Bounce off!
		reverseDirection();
	}

	public void reverseDirection() 
	{
		if (movingForward) {
			setBack(40000);
			movingForward = false;
		} else {
			setAhead(40000);
			movingForward = true;
		}
	}

	/**
	 * onScannedRobot:  Fire!
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		eDist = e.getDistance(); //取得对方距离
	    radarMove = -radarMove; //设置雷达
	    double eBearing = e.getBearingRadians(); //取得和对方相对角度的弧度数
	    //将bot转动相对的角度,以后bot的运动将是以对方为圆心的圆周运动
	    setTurnLeftRadians(Math.PI/2 - eBearing);
	    //转动炮管指向对方
	    setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(getHeadingRadians() + eBearing - getGunHeadingRadians()));
	    //根据对方距离射击
	    dFirePower = 400/eDist;
	    if (dFirePower > 3)
	    {
	       dFirePower = 3;
	    }
	    fire(dFirePower);
	}

	/**
	 * onHitRobot:  Back up!
	 */
	public void onHitRobot(HitRobotEvent e) 
	{
		// If we're moving the other robot, reverse!
		if (e.isMyFault()) 
		{
			reverseDirection();
		}
	}
}



    这是根据robcode所提供的机器人样板源代码及网上教程所编写的,还有很多不足,在移动走位上有很大缺陷

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package ms; import robocode.*; import java.util.*; //******************************************************************************************/ // GoodBot: determine, if a bot is a bastart or not // // needed: public void run() // GoodBot.Reset(); // public void onRobotDeath(RobotDeathEvent e) // GoodBot.BotDeath(e.getName(), this); // // example: public void onScannedRobot(ScannedRobotEvent e) // if (GoodBot.badBot(e.getName(), this)) //******************************************************************************************/ class GoodBot { static private Hashtable Bots = new Hashtable(); static private int GoodBots1 = 1; static private int GoodBots2 = 0; static private String bot; static public int KilledGoodBots1 = 0; static public int KilledGoodBots2 = 0; //******************************************************************************************/ // BadBot: is it a bad bot? //******************************************************************************************/ public static boolean badBot(String Name, AdvancedRobot in_this) { bot = (String)Bots.get(Name); if (bot == null) classBot(Name, in_this); int others = in_this.getOthers(); if ( (bot.compareTo("1") == 0) && (others >= GoodBots1 - KilledGoodBots1) ) return false; else if ( (bot.compareTo("2") == 0) && (others >= GoodBots1 - KilledGoodBots1 + GoodBots2 - KilledGoodBots2) ) return false; else return true; } public static void BotDeath(String Name, AdvancedRobot in_this) { bot = (String)Bots.get(Name); if (bot == null) classBot(Name, in_this); if ( bot.compareTo("1") == 0 ) KilledGoodBots1++; else if ( bot.compareTo("2") == 0 ) KilledGoodBots2++; } public static void Reset() { KilledGoodBots1 = 0; KilledGoodBots2 = 0; } private static void classBot(String Name, AdvancedRobot in_this) { int M_GoodBots = GoodBots1; String Gegnertyp = botTyp(Name); String Mytyp = botTyp(in_this.getName()); if (Gegnertyp.compareTo(Mytyp) == 0) { bot = "1"; if (GoodBots1 > M_GoodBots) in_this.out.println("I've found " + (GoodBots1 - 1) + " more " + Mytyp + "-Bot(s)."); } else { GoodBots1 = M_GoodBots; String Gegnerpack = botTyp2(Name); String Mypack = botTyp2(in_this.getName()); if (Gegnerpack.compareTo(Mypack) == 0) { bot = "2"; GoodBots2++; in_this.out.println("I've found " + (GoodBots2) + " more " + Mypack + "-Bot(s)."); } else bot = "0"; } Bots.put(Name, new String(bot)); } private static String botTyp(String BotName) { int k=BotName.indexOf("("); if (k != -1) { int Nr=Integer.parseInt(BotName.substring(k+1, BotName.indexOf(")")),10); if (GoodBots1 < Nr) GoodBots1 = Nr; return BotName.substring(0, k-1); } else return BotName; } private static String botTyp2(String BotName) { int k=BotName.indexOf("."); if (k != -1) { return BotName.substring(0, k); } else return BotName; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值