当前位置:网站首页>Robocode tutorial 8 - advanced robot

Robocode tutorial 8 - advanced robot

2022-04-23 18:08:00 dawnsun001

In this tutorial , We are going to learn AdvancedRobot and Robot The difference between , Learn to AdvancedRobot Is the premise for us to write high IQ robots .

         Robots that can run efficiently are inherited AdvancedRobot, because Robot It's thread blocking ,AdvancedRobot It is thread non blocking . The difference between them is obvious , analysis while(true){} It's easier to understand thread blocking and non blocking with the code inside .

   public void run() {

      while(true) {

         ahead(100);

         turnGunRight(90);

      }

}

stay Robocode Each robot in the is an independent thread , We know that whether it's extends Thread still implements Runnable We must achieve public void run() Method , This is essential . In order to ensure that the robot continuously runs its own program , So... Is used in the thread while(true) loop . In each cycle ,ahead(100) machine

The robot moves forward 100 pixel , next turnGunRight(90) Right turn gun 90 degree , You will find that the robot executes according to strict sentence by sentence code , Only after the execution ahead(100) after , Will execute turnGunRight(90), Then cycle . Our robots will walk a square line on the battlefield .

         Let's analyze another code , This code is inherited from AdvancedRobot, You will find that each method is preceded by set, This is to facilitate and Robot Make a difference .

            public void run() {

      while(true) {

         setAhead(100);

         setTurnGunRight(90);

         execute();

      }

}

Almost the same code as above , Just one more sentence execute(); And this execute() Is the key to non blocking code , This involves java Multithreaded programming , We won't talk much about , We can think of it this way execute(), It's equivalent to a sign , The program records the contents of all the previous code to be executed , But not implemented. , Only received execute() After the command , Previously recorded commands will run alternately , For the above code , It's a small step forward , Turn a small angle , Take a small step forward , Turn a small angle , So circular . So on the battlefield, our robot will walk a circular video .

Okay , About Robocode That's all the basic content of , With this knowledge, I can write some good robots . But to write smarter actual combat robots , We still need a lot of algorithms , Later, we will introduce these algorithms , Including random motion , Pseudo random motion , Linear prediction aiming , Circular prediction aiming , Statistical aiming , Virtual wave aiming and so on .

版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544289368.html