Robocode - make own bot and learn Java!

Programs intended to teach, or to help one learn or study a specific subject - this includes educational games.
Post Reply
Message
Author
User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

Robocode - make own bot and learn Java!

#1 Post by puppy_apprentice »

Robocode is a programming game, where the goal is to develop a robot battle tank to battle against other tanks in Java or .NET. The robot battles are running in real-time and on-screen.
More on Robocode Home Page

Instalation:

1) Load Java 6 .sfs or better (i've loaded jre-1.8.0.05.sfs on my Slacko 7.0)
2) Click on robocode-1.9.3.3-setup.jar (download)
3) Choose directory to install.
4) To run click on robocode.sh in /yourpath/robocode or in terminal:

Code: Select all

/yourpath/robocode/robocode.sh
I. First we will run battle with bots installed with application:

1) Battle/New
2) Packages/Sample, Robots/Crazy and Robots/SpinBot
3) Start Battle

Picture 1

II. Now we make own bot:

1) Robot/Source Editor
2) File/New/Robot
3) Enter Name for your bot
4) Enter Package Name for your bot

You will see template code for your bot.

Picture 2

Code: Select all

package Puppy;
import robocode.*;
//import java.awt.Color;

// API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html

/**
 * Puppy_Apprentice - a robot by (your name here)
 */
public class Puppy_Apprentice extends Robot
{
	/**
	 * run: Puppy_Apprentice's default behavior
	 */
	public void run() {
		// Initialization of the robot should be put here

		// After trying out your robot, try uncommenting the import at the top,
		// and the next line:

		// setColors(Color.red,Color.blue,Color.green); // body,gun,radar

		// Robot main loop
		while(true) {
			// Replace the next 4 lines with any behavior you would like
			ahead(100);
			turnGunRight(360);
			back(100);
			turnGunRight(360);
		}
	}

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		// Replace the next line with any behavior you would like
		fire(1);
	}

	/**
	 * onHitByBullet: What to do when you're hit by a bullet
	 */
	public void onHitByBullet(HitByBulletEvent e) {
		// Replace the next line with any behavior you would like
		back(10);
	}
	
	/**
	 * onHitWall: What to do when you hit a wall
	 */
	public void onHitWall(HitWallEvent e) {
		// Replace the next line with any behavior you would like
		back(20);
	}	
}
And this my code for Puppy_Apprentice bot after small changes (to make more advanced bot you have to read Robocode Wiki or watch Video Tutorials):

Picture 3

Code: Select all

package Puppy;
import robocode.*;
import java.awt.Color;

// API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html

/**
 * Puppy_Apprentice - a robot by (your name here)
 */
public class Puppy_Apprentice extends Robot
{
	/**
	 * run: Puppy_Apprentice's default behavior
	 */
	public void run() {
		// Initialization of the robot should be put here

		// After trying out your robot, try uncommenting the import at the top,
		// and the next line:

		setColors(Color.black,Color.black,Color.gray); // body,gun,radar

		// Robot main loop
		while(true) {
			// Replace the next 4 lines with any behavior you would like
			ahead(200);
			turnRight(90);
			turnGunLeft(360);
			
			ahead(200);
			turnLeft(90);
			turnGunRight(360);
		}
	}

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		// Replace the next line with any behavior you would like
		fire(1);
	}

	/**
	 * onHitByBullet: What to do when you're hit by a bullet
	 */
	public void onHitByBullet(HitByBulletEvent e) {
		// Replace the next line with any behavior you would like
		back(50);
	}
	
	/**
	 * onHitWall: What to do when you hit a wall
	 */
	public void onHitWall(HitWallEvent e) {
		// Replace the next line with any behavior you would like
		turnRight(90);
		back(50);
	}	
}
To use our bot in battle we have to compile our bot code first.
So in Source Editor choose Compiler/Preferences and change -version in Compiler Options field into -showversion.

Picture 4

Now choose Compiler/Compile.

To assign your bot to battle look at point I.

Picture 5

After battle you will get some statistics.

Picture 6

Build the best, destroy the rest!
Attachments
1.jpg
Two bots on battle arena
(66.13 KiB) Downloaded 326 times
2.jpg
Template bot code
(49.68 KiB) Downloaded 342 times
3.jpg
Puppy_Apprentice bot code
(48.98 KiB) Downloaded 340 times
4.jpg
Compiler preferences
(26.73 KiB) Downloaded 331 times
5.jpg
Puppy_Apprentice on battle arena ;)
(66.78 KiB) Downloaded 329 times
6.jpg
After battle statistics
(25.12 KiB) Downloaded 326 times

Post Reply