If you do not know what eclipse is, please refer to this article http://www.loujing.com/Article/ShowArticle.asp? Article ID = 30, or related information on the Internet.
If you do not know what robocode is, please refer to this article http://www.loujing.com/Article/ShowArticle.asp? Article ID = 32, or related information on the Internet.
Although robocode comes with a robot Editor, its functions are slightly inadequate. Just recently we are studying eclipse. Let's take a look at how to create a robot in eclipse.
The development environment in this article: Windows XP Professional SP2 + j2sdk1.4.2 _ 10 + eclipse 3.1.1 + robocode 1.0.6. First, ensure that the related development kit is installed on your machine.
1. Eclipse settings:
First, we will introduce that the workspace of eclipse on my machine is E:/mywork/Java/exercise/eclipse; the installation path of robocode on my machine is C:/program files/robocode.
(1) create a project: After starting eclipse, click "file" ----- "new" ----- "project" in the menu, start the wizard, and select "Java project ", the project name is "myrobocode ".
(2) create a package in the project: Right-click the "myrobocode" project in the "package Explorer" and choose "New" ----- "package ", create a new package, as shown in:
Figure 1
The package name is "com. loujing ", as shown in (Java suggests that, to create a unique package in the world, please reverse your domain name as the package name, all in lower case, my domain name is loujing.com ):
Figure 2
The reason for creating a package is that the robots you write need to compete with robots compiled by other programmers all over the world, so your robot name must be unique. Robocode uses "package name + machine human name" for identification.
(3) import the robocode Class Library: because most of the Code has been written for you in the robocode toolkit, if you want to create your own robot, you must add the class library to the project, right-click the "robocode" project in "package Explorer" and choose "properties", as shown in:
Figure 3
In the displayed dialog box, select "Java build path" on the left, select the "Libraries" page on the right, and click "add external jars ..." Click the installation path of robocode (C:/program files/robocode on my machine), select "robocode. Jar", and click OK, as shown in:
Figure 4
(4) Create your own robot: everything is ready, and finally begin to write robot code. In package explorer, right-click the com. loujing package in myrobocode, and choose new ----- select file, as shown in:
Figure 5
In the displayed dialog box, enter the file name "airobot. Java", as shown in:
Figure 6
Then, type the following code in airobot. Java:
Package com. loujing;
Import robocode. *; // import the package
Public class airobot extends robot // inherits from the robot class
{
Public void run () {// required method to determine the basic behavior of the machine Tank
While (true ){
Turngunright (360); // turn right on the barrel 360
}
}
Public void onscannedrobot (scannedrobotevent e) {// when scanning other machine tanks
Ahead (100); // advance 100
Turnleft (90); // The left turn of the machine tank is 90
}
}
Isn't it an unexpected simplicity? We barely write a few lines of code. It's just a basic framework. In eclipse, when you move the mouse over an identifier, the robocode API help is displayed in real time, for example, the onscannedrobot method description is displayed:
Figure 7
Ii. robocode settings:
After we compile airobot. Java, airobot. Class is generated. Let's take a look at how to call your *. Class file in robocode. You have two methods:
(1) copy the generated *. Class file to robots in the robocode installation path. For example, I will copy airobot. Class to C:/program files/robocode/robots.
(2) there is actually a simpler method. robocode supports eclipse well. We can set the path directly in robocode. Start robocode, select "option" menu ----- click "Preferences", select the "development options" page in the displayed dialog box, and fill in the path of your previously created Eclipse project, click the "finish" button (the project path I just created is E:/mywork/Java/exercise/Eclipse/myrobocode), as shown in:
Figure 8
In addition, on the "view options" page, there is a check box "visible scan arcs", you can select it, so that the robot can see the ray radar during scanning, that's cool.
3. Test your own robot: Everything is ready. Now let's test the mentally retarded robot we wrote.
Start robocode and select the "battle" menu ----- click "new" (create a new battle). The displayed dialog box is shown in:
Figure 9
In the packages list, we can see all the packages, including the com. the loujing package. In the robots list, all robots are listed, including the airobot we created. We use the Add button to add two airobots (of course, you can add more robots. If you select only one, the system will prompt you that you cannot fight, you can only observe the movements of one robot.
Number of rounds indicates the number of rounds in a battle. The default value is 10 rounds. When everything is selected, click the Start battle button, as shown in:
Figure 10
The middle area is the station. The buttons below can be used to pause, resume, and stop the competition at any time. You can take a closer look at the two airobots, as we have previously written. First, right-turn the canal at 360 degrees. Then, when scanning another robot, you can go 100 first, turn left 90 degrees, and all actions are implemented by programming in advance.
Now, you can modify the original airobot. java. For example, add "Fire (1);" in the onscannedrobot method, which means that when scanning other robots, it will open fire.
You can go to the robots folder under the installation path of robocode to open *. java file to check how other people's robot programs are written, or download them online. If you have any questions about library functions, click the Help menu of the robocode program and select "robocode API ", alternatively, double-click javadoc/index.html in the installation path to open the help. You need to focus on the robot class and check the methods for controlling robots and how to use them, as shown in:
Figure 11
If you are interested in creating your first robot at a specific time, for example, it is artificial intelligence to avoid, open fire, and turn back to the wall.
My girlfriend has gone to Hainan. She came back at this morning. I don't even know it was. It's big today.
Building competing http://www.loujing.com