Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 23 Oct 2019, 21:48
All times are UTC - 4
 Forum index » Advanced Topics » Additional Software (PETs, n' stuff) » Educational
Robocode - make own bot and learn Java!
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [1 Post]  
Author Message
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Tue 13 Nov 2018, 14:11    Post subject:  Robocode - make own bot and learn Java!  

Quote:
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:
/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:
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:
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!
1.jpg
 Description   Two bots on battle arena
 Filesize   66.13 KB
 Viewed   239 Time(s)

1.jpg

2.jpg
 Description   Template bot code
 Filesize   49.68 KB
 Viewed   249 Time(s)

2.jpg

3.jpg
 Description   Puppy_Apprentice bot code
 Filesize   48.98 KB
 Viewed   248 Time(s)

3.jpg

4.jpg
 Description   Compiler preferences
 Filesize   26.73 KB
 Viewed   240 Time(s)

4.jpg

5.jpg
 Description   Puppy_Apprentice on battle arena ;)
 Filesize   66.78 KB
 Viewed   240 Time(s)

5.jpg

6.jpg
 Description   After battle statistics
 Filesize   25.12 KB
 Viewed   240 Time(s)

6.jpg

Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [1 Post]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Advanced Topics » Additional Software (PETs, n' stuff) » Educational
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0734s ][ Queries: 13 (0.0196s) ][ GZIP on ]