Write your own robots with Java, and then "kill"

zhaozj2021-02-16  44

Just for the fun - Robocode (write your own robot with Java, then "killing")

Here's this article was originally published on the Newsletter issued on January 15, 2002, which is written during Winfield Allen work.

What is Robocode?

Recently I read a list of emailings on Extreme Programming (XP), some people proposed to build a developer who learned more XP methodology to build a workshore. The author also suggested that a group of people together and divided into a team to study the methodology. Then there is an experienced XP coach to manage the workshop to help participants understand their principles.

It is recommended that the projects adopted by the groups is to create a Robocode robot.

I stopped and reminded this passage again. In fact, I have always been interested in robots. When I or a child is time, I remember that there is a program on APLLE II, we use it to write a simple robot program and then fight each other.

Robocode is such a thing, but it is better. It is a Java-based robot combat game. The writing and modeling of their code are good, and it is fun to play.

If you read this newsletter for a long time, you may ask yourself "He will java?". Yes, I can. Although I often complain about Java, I don't really hate Java. What I hate is just the language and the liberality of the platform and can be upgraded. I always think that as a language, Java is the most superior lightweight C , this idea until I found C #, and this is another story.

Robocode's API

Robocode is written by IBM's AlphaWorks project member Matthew Nelson. Starting from the first version, Matthew has made considerable improvements to the API of the software. I thought that the software provided a perfect framework for the program. The base class for creating robots is called Robot. Your robots are inherited from this class. The ROBOT class provides all the method functions required to interact with the game.

Let us first understand some terms before the API. The robot is basically some small tanks. These tanks can be rotated, forward or backward. There is a artillery for shooting. There is also a radar system on the artillery, used to detect other robots. The robot's chassis, artillery and radar systems can move independently, or "lock" with each other. In other words, if the radar is locked down, it will move along with the artillery; if the artille is locked down, it will move along the chassis of the robot. Interesting?

Here are the most ways to use the initial use of robots: ahead (), back () - forward and backward mobile fire () - Fire SetadjustGunForrobotturn () - Controls whether the artillery is locked down SetAdjustRadarforgunturn ( ) - Controlling whether the radar is down to lock turnright (), turnleft () - realizes Rotary Turngunright (), Turngunleft () - Rotate Turnradarleft (), TurnRadarright () - Rotate Radar Mirror

Of course, this is not all APIs, but it is the most important way you need to create the first robot. So far, we only discuss the action you can trigger. We have not discussed how to discover what happened around your robot. The ROBOT class provides multiple events that you can use these events to determine if your robots are scanned to other robots with radar, whether it is hit, whether it is collided with another robot and whether it is hit with a wall. You can override the following method functions to provide event handler for your robots. Onhitbybullet () - When your robot is hitted onhitrobot () - When your robot is hit by other robots () - When your robot hits the wall, onScannedRobot () - use radar When scanning to other robots

You can write our first robot by simple events and actions talked about above. (A complete API can be obtained from the document included with Robocode.)

Write your first robot

Ready? Start the Robocode, call up the Robot Editor. (In fact, I don't use the editor of the Robocode, here is easy to explain, it is easy to explain.)

To create a robot, you can choose the new robot command for the File menu. At this time, the system will ask you to enter the name of the robot. You can enter simplerobot and click on the OK button. Then, you need to enter a package name, Robocode recommends using the name of the letter to abbreviate, but we use "Newsletter" here. After that, a window containing the initial code of the new robot will be opened.

Take a little time to read these automatically generated code. This robot is not the smartest robot in the world, but it has made a good demonstration on how to assemble a Robocode robot.

The first method defined in our robot is public void run (). This is the heart of the robot. This method will be called when the game engine is initially started. Generally, you can define the actions of robots with an endless While loop.

The While cycle in the robot simplerobot is four things:

Ahead (100); mobile 100 pixels TurngunRight (360); rotate the artille 360 ​​degree back (100); move 100 pixels TurngunRight (360) backwards; 360 degrees to the right

These orders are simple to say "to move 100 pixels in front and watch it around, and then return 100 pixels, look around. Then, continue to repeat".

Note We never explicitly execute a radar scan action. It is because the radar mirror moves with the artillery in the scanning state by default. Therefore, rotating the artillery can achieve the scan of radar. When the radar detects the target, the second method function as defined in our robots will call public void onsannedRobot (...).

This event handles functions don't do. Only the "shooting" action is executed. Since the radar is always consistent with the direction of the artillery, this function is called, we should just aim at the goal.

The only other method is the onhitbybullet event handler. This function is called when we have hit by other robots. The role of the code is to rotate the robot to the bullet to a 90 degree angle, so that when we move forward, it can avoid the direction of the second shot of the bullet.

Save and select the compile command to compile your robot. Because we don't have any changes to the code, there should be no mistakes.

Duel with other robots

Your first battle

Your robot is ready, it is time to fight! Robocode comes with a large number of sample robots that can be used to hold battles, Target is the simplest one.

Suppose you want to fight between Target and the robots we just created. Then turn off the Robot editor and select the new command of the Battle menu in the main window. Find "Newsletter.SIMPLEROBOT" in the New Battle dialog and click the Add button. Then find "Sample.target", click again and click the Add button. There are other options in the dialog, but we will wait anyway. Click Finish to start fighting. After a few seconds, you will see that the two robots appear on the screen. Waiting for time depends mainly on the speed of the computer. Soon, the battle started. First, our robot moves forward a small distance, then rotates the artille for one week. When it discovers Target, it will shoot it. After several times, Target will avoid it. Our robots move backwards and scanned until Target is found. Then continue shooting.

Target won't shoot, so we will eventually win this battle (the robot that will not shoot will make me very annoyed). Therefore, our OnHitbyBullet event should never be called. After the battle is over, you can try again with other sample robots. But don't expect this simple robot to defeat them.

game

I don't recommend joining the game at the beginning, but I will eventually think that your robot is enough, you can join the game. There are a large number of different group organization competitions. You can find them online. If you don't like to participate in the public game, you can also find a few friends to organize your own game.

There are two main forms in the form of competition. The mixed competition is two or more robots for fighting; a one-on-one game is a robot to deal with another robot, just like it is like a gun.

Usually, the mixed competition is more fighting and winner, and a one-to-one game is less, or one is wins, or two defeats.

Senior robot

The Robocode API also defines another base class named as AdvanceDrobot. Different performances in AdvancedRobot with Robot are in many ways. First, you can choose to call non-blocking. When you call AHEAD (100), the robot moves 100 pixels immediately. The next line of code is performed after the action is completed. This will not achieve curve movement. But now there is a non-obstructive version of the AHEAD () and back () function - setAhead () and setback (), can return to execute immediately. They can imagine to join a directive to the stack, and instructions in the stack will not be executed unless you perform a function call similar to Fire () - this will cause execution of instructions in the stack. Another one is called an Execute () method function that can be explicitly running stack instructions.

ADVANCEROBOT-based robots have more advanced IQ. No longer a simple "moving, scanning, removal, re-scan". The robot you made can reconoct throughout the battlefield, think about the next action, and then perform the appropriate instructions. This "Scout, Thinking, Action" model can make your robots adaptive and learning.

For example, if your robot can continuously reconnaissance throughout the battlefield and record the location and direction of other robots, then you can get a specific robot's mobile approach. If the enemy avoids the left or right in a presence manner, then you can predict this and shoot to the enemy will appear. This power will be more powerful, of course, more complicated.

in conclusion

This article is not enough to be a complete guide to prepare the optimal robot, but it should be able to help you get started. When you write a few simple robots, you will find some new feelings. Maybe you will participate in the game, or download other robots from the Internet. In this way, you have the opportunity to see how others write robots.

Robocode is not only a game, but also a fun way to abrasive programming skills. You can experiment with different design and modes, you can create a test package with JUnit. In fact, this game limit is only your imagination. I only have a request: If you don't work because of playing roboccode, if you can't blame me.

(Welcome to visit the translator's personal "http://www22.brinkster.com/cymanhome/index.asp)

转载请注明原文地址:https://www.9cbs.com/read-27951.html

New Post(0)