Robocode Learning Notes (IV.)

Source: Internet
Author: User
Tags radar
-->
-->

The last time I mentioned the avoidance method, there is a problem. As soon as the other bullet was fired, my bot moved, and the move was regular and moving back and forth. If you move a short distance, you may hit each other's bullets when you come back, and if you move a long distance, you are doing a straight line motion, and the other side can easily compute the trajectory of the bot motion. There is also a problem, avoid the time is very likely to hit the wall ... (The wall is to reduce energy: ~ ()

In response to the above questions, I wrote another bot. The code is as follows:

Import robocode.*;

public class Hanicbot extends advancedrobot{private double edist;//each other's distance private double move;//moving distance private double Radarmove = 45;  Angle of the radar movement private double dfirepower; Fire

 /**   * main func run ()    */  public void Run () {    edist = 300; &nbs p;  while (true) {     //per cycle, motion random distance       Double period = 4 * ((int) (EDIST/80)); //cycle; The closer the enemy, the shorter the cycle, the more frequent the movement      //cycle begins, then move        if (getTime ()%period = 0) {        move = (Math.random () *2-1) * (period*8-25) ;        setahead (move >= 0) ( -25));      }     //Avoid hitting the wall       double heading = Getheadingradians (); Get the number of radians in the bot direction       Double x = GetX () + move*math.sin (heading); The x-coordinate       double y = GetY () + move*math.cos (heading) that will be reached after moving the move. The y-coordinate to be reached after moving move       double dwidth = getbattlefieldwidth (); //Battlefield width      &nBsp Double dheight = getbattlefieldheight (); //Battlefield length      ///When (x,y) exceeds the specified range, moves move backward       if (x < | | | x > dWidth-30 | | | y < | | | y > dHeight-30) {   &nbsp ;    setback (move);     }      turnRadarLeft ( Radarmove); Rotary Radar    } }//end run ()

<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.