
The habrasoobschestvo supported the idea of writing reviews of games for programmers, so I continue this series of articles. Let me remind you, last time I told you about the games
Colobot and Ceebot .
Today you will learn about the excellent game Robocode. It is designed for advanced Java learning, and has great functionality, despite its apparent outward simplicity.
Robocode is an open source educational game developed by Mathew Nelson and Flemming Larsen. Its sole purpose was to facilitate learning the Java programming language.
')
A game
The main driving element in learning was to be competition. Each participant writes Java code that controls a small tank, and fights with other similar tanks.
Your goal is to destroy all the other tanks on the field. No compromise. Your tank must achieve absolute superiority, and show how pathetic losers your friends are.
The developers strongly draw your attention to the fact that there is no blood in Robocode, no people and no policy. There are explosions in the game, but if you care about your own mental health, you can easily turn them off.
The tanks have great opportunities: they can move around the playing field, shoot, find out the location of each other, crash into walls and other robots, find out where the flying bullets are and use the full power of the Java language.
Naturally, there is no simple strategy for winning. There are a huge number of different tactics, each of which has its own advantages and disadvantages. The size of the code in various tanks ranges from ten lines to several thousand. Some even manage to resort to statistical analysis and neural programming.
An interesting approach to writing a robot at ITMO. The tank was developed using SWITCH-technology (a mixture of automaton and object-oriented programming) and serious project documentation is attached to the project.
You can download it and the source code of the tank absolutely free on
the project page .
Game installation
On the
official site of the project there is a huge amount of various materials. If you want to start learning, this is the best place to start.
The game itself is distributed as a jar-archive, which can be downloaded from the
download page of the game.
The game is cross-platform, as it is written in Java. It can be played on Windows, Linux, FreeBSD and on any other system on which the Java machine is ported.
Before installing the game, do not forget to install
Java .
Your first robot
Ready to build your first robot? I am sure it will be easy, entertaining and just interesting!
Creating a robot is easy. But to make him a winner is not.
Built-in editor
The developers have included their own editor in the game. Therefore, you already have a ready development environment with syntax highlighting.
This program is called Robot Editor, and it is available in the menu Robot -> Editor.
Let's now create a robot workpiece. To do this, go to the menu item File -> New Robot. Think of a name for your future robot and enter your initials (nickname).
Voila! Now you see a sketch of the code of your future robot.
Note: If you want to use Eclipse or another IDE, you can easily find the
appropriate guide for integrating Robocode on the Internet.
New robot
In its simplest form, the code should look like this:
// . .
package v673;
// Java, Robocode .
import robocode.*;
// Java: ", , Robot.
// MyFirstRobot".
public class MyFirstRobot extends Robot
{
// run(), .
public void run()
{
// .
}
// , .
}
It's time to do something!
Let's make our robot do something. Add the following lines to the run () method:
// while (true) , , .
while ( true )
{
// 100 .
ahead(100);
// 360 .
turnGunRight(360);
// 100 .
back(100);
// 360 .
turnGunRight(360);
// .
}
Our robot will perform these actions over and over until it dies. Not bad, huh?
The fire!
When the tank's radar finds an enemy robot, we start shooting:
public void onScannedRobot(ScannedRobotEvent e)
{
fire(1);
}
As you can see, a
ScannedRobotEvent object is sent to the function, which contains information about the enemy (or friendly) robot - how much health it has, where it is located, how fast it moves, etc.
But since we have a simple robot, we will not spend much time on it.
Compiling a robot
First of all, save your work:
File ->
Save .
Now compile the robot:
Compiler ->
Compile .
If your robot has compiled without errors, you can start the battle. Start a new battle by selecting
Battle ->
New in the game menu. If you do not see your own robot - update the list by pressing F5. Add your robot to the battle with another robot. For example, with the tank
Sample .
Start the battle by clicking
Start Battle .
Enjoy!
Robot anatomy
We have just created our own robot. Let's now take a closer look at the design features of the tanks.
The tank consists of three parts: the body of the tank, the gun and the radar.
Each of these parts can move independently of each other. The tank moves most slowly, the gun moves faster, and the radar is the fastest element of the robot.
Battlefield
The battlefield is a rectangle. And its size is set when creating a battle.
Coordinate system:
Notice that even if you execute ahead (50,000), the team will stop its execution at the moment when the robot hits a wall.
In RoboWiki, you can read more about
game physics .
Deflection angle
Sometimes it is convenient to use a relative angle. For example, to rotate to another robot, you can use the following command:
turnRight( event .getBearing());
That is, getBearing () indicates how many degrees you need to deviate from the current position in order to see, for example, an enemy tank.
Note: If you pass a negative value to the turnRight () function, the robot will turn to the left.
Feelings of the robot
No, we will not talk about the vulnerable soul of the tank. We consider the functions by which the robot can recognize the influence of external factors.
Your robot knows when:
- He hits the wall: onHitWall ()
- An enemy bullet hits it: onHitByBullet ()
- He bumps into another robot: onHitRobot ()
- And also a number of exotic features, for example, when your bullet hits the bullet of an enemy robot.
Based on these functions, all robots in Robocode are built. For more information on all the functions, please refer to the
API documentation .
What's next?
Most of the educational material collected on the
official website of the game .
Unfortunately, there are not so many Russian materials about Robocode. But I hope that English is not a big problem for you.
I recommend to get acquainted with the course on Robocode from Mark Whitley:
CS 3230 - Robocode Project . By the way, there is a
collective Russian translation thanks to the
translated.by project. But, unfortunately, it is not yet in a separate PDF file, there are no pictures. Therefore, I, at the moment, I advise you to study the original.
There are also two excellent articles from Sing Li about Robocode:
Rock 'em, sock' em Robocode! Round 1 ,
Rock 'em, sock' em Robocode! Round 2RoboWiki collected a huge amount of high-quality material about RoboCode. Starting from simple guides, and ending with an explanation of various fine points.
In addition, on the official website you will find
many links to other excellent articles about Robocode.
You can even read articles about the use of
genetic algorithms in RoboCode .
The
RoboCode repository contains a huge number of different robots. Some have source codes.
There is an official blog of developers:
robo-code.blogspot.com .
On this our review, I would like to finish.
If you are interested in my articles, then there is a hack that allows you to read them before the release on habrahabr: subscribe to
my blog , well, or
twitter .
I wish you success in understanding the subtleties of Robocode!