Java Training Robocode: Java Can Be Fun - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Java Training Robocode: Java Can Be Fun

Description:

so our seesaw might avoid a future shot. public void onHitByBullet(HitByBulletEvent e) ... turnRadarRight(double degree) turnRadarLeft(double degree) turns the ... – PowerPoint PPT presentation

Number of Views:268
Avg rating:3.0/5.0
Slides: 27
Provided by: cexpem
Category:

less

Transcript and Presenter's Notes

Title: Java Training Robocode: Java Can Be Fun


1
Java TrainingRobocode Java Can Be Fun
  • Written by Jeff Smith(portions borrowed from
    RoboCode website)

2
Introduction
  • Java Libraries
  • A common task for a Java programmer is to
    identify a library, download it, and then learn
    how to use it
  • Javadocs are useful here
  • Google is too! For example, if I wanted to use
    JDBC (Java Database Connectivity) and needed a
    code example, I could Google this
  • JDBC Java example
  • Or
  • JDBC Java tutorial

3
RoboCode What is It -1
  • Robocode is a Java library and is free
  • Was originally developed by Mathew A. Nelson in
    2001- 2005
  • Now been taken over by Flemming N. Larsen in 2006
  • Was created as a tool for teaching Java
  • You can either extend a simple Robot class or an
    AdvancedRobot class
  • You guessed it, AdvancedRobot extends Robot
  • Well use the simpler class, Robot, for our
    exercise

4
RoboCode What is It -2
  • A Robot/Tank battle simulator
  • Basically, you can extend a Robot class and
    implement a few methods (like fire()).
  • In a few lines of code, you can define how your
    Tank will track and fire on enemies
  • You can compete in the Battle Simulator
  • You define a simple (or not so simple, if you
    choose) Robot class and the RoboCode Simulator
    does all the graphics and time steps for you
  • You start out with a certain amount of energy,
    and you
  • lose a lot of energy when you get hit by another
    robot
  • some energy when you fire your gun
  • a little energy as time ticks by

5
RoboCode What is It -3
6
RoboCode Intro -1
  • Body - Can move ahead and back, as well as turn
    left or right.
  • Gun - Mounted on the body. Can turn left or
    right, and fire energy bullets.
  • Radar - Mounted on the gun. Can turn left or
    right. Generates events when other robots are
    detected.

7
RoboCode Intro -2
  • run() method
  • The main method of your Robot class
  • This method gets called by the Battle Simulator
    in turns or time slices
  • your robot gets to run, then your opponent gets
    to run
  • Some things to do in the run() method
  • Move ahead or back
  • Turn
  • Scan
  • Fire

8
RoboCode Intro -3
  • How do I know what Robot methods I can call?
  • Javadocs, of course!
  • Ive put a link to the Javadoc for Robot.java on
    the Fun Stuff pagehttp//www-ad.fsl.noaa.gov/a
    c/javazone/FunStuff.html

9
RoboCode Intro -4
10
My First Robot Class -1
package sample import robocode. /
MyFirstRobot by Mathew Nelson. Moves in seesaw
motion, and spins the gun around at each
end / public class MyFirstRobot extends Robot
public void run() while (true)
//run this loop forever ahead(100)
// Move ahead 100 turnGunRight(360) //
Spin gun around, scan back(100) // Move
back 100 turnGunRight(360) // Spin gun
around, scan
11
My First Robot Class -2
/ Fire when we see a robot our
gun radar are pointed in same direction /
public void onScannedRobot(ScannedRobotEvent e)
fire(1) //fire with power1
/ We were hit! Turn perpendicular to the
bullet, so our seesaw might avoid a future
shot. / public void onHitByBullet(HitByBulle
tEvent e) turnLeft(90 -
e.getBearing())
12
My First Robot Class -3
  • Basically, my first robot goes forward 100 units
    and then backwards 100 units (some kind of
    movement is important so you can avoid being hit
    by another Robots gun!)
  • It also turns its gun (and radar) around in
    circles, so it can detect enemy robots
  • When an enemy is detected by this radar
    rotation/scan, the onScannedRobot() method is
    invoked automatically by the Battle Simulator
  • Since the gun and radar are in sync (pointing in
    the same direction), all you need is a call to
    the fire() method when you detect the enemy robot

13
Robot Class Events Methods -1
  • In OOP, event methods are generally invoked by
    some external piece of code (perhaps another
    class)
  • You just write the code to respond to the event
  • onScannedRobot() - called when the radar detects
    a robot
  • onHitByBullet() - called when the robot is hit by
    a bullet
  • onHitRobot() - called when your robot hits
    another robot
  • onHitWall() - called when your robot hits a wall

14
Robot Class Events Methods -2
  • You generally dont need to invoke these event
    methods directly (in your own code)they are
    invoked on your behalf by the Battle Simulator
  • You can choose whether or not to override these
    event methods in your own Robot subclass
  • For example, if you want to execute some special
    code when you hit a wall, implement an
    onHitWall() method.
  • If you dont care about hitting walls, dont
    implement the method

15
Other Robot Class Methods -1
  • turnRight(double degree)
  • turnLeft(double degree) turn robot by a specified
    degree
  • ahead(double distance)
  • back(double distance) move robot by the specified
    distance
  • Note ahead() and back() returns early if robot
    hits a wall or
  • another robot. So if you made a call to
    ahead(9999999), the
  • call would return once your robot hit either a
    wall or another
  • robot (it wouldnt go ahead forever).

16
Other Robot Class Methods -2
  • turnGunRight(double degree)
  • turnGunLeft(double degree) turns the gun,
    independent of the vehicle's direction
  • turnRadarRight(double degree)
  • turnRadarLeft(double degree) turns the radar on
    top of the gun, independent of the gun's
    direction (and the vehicle's direction)
  • getHeading() returns your current heading in
    degrees

17
Other Robot Class Methods -2
  • turnGunRight(double degree)
  • turnGunLeft(double degree) turns the gun,
    independent of the vehicle's direction
  • turnRadarRight(double degree)
  • turnRadarLeft(double degree) turns the radar on
    top of the gun, independent of the gun's
    direction (and the vehicle's direction)
  • getHeading() returns your current heading in
    degrees

18
What Does This Robot Do? -1
public class MysteryRobot extends Robot
public void run() turnLeft(getHeading())
for () ahead(99999)
turnRight(90)   public void
onScannedRobot(ScannedRobotEvent e)
fire(1) public void onHitByBullet(HitByBu
lletEvent e) turnLeft(180)
19
What Does This Robot Do? -2
  • This robot gets its initial heading, then turns
    left by that amount so that it faces straight
    NorthturnLeft(getHeading())
  • Next it starts moving Northahead(99999)
  • Whenever it runs into something, the ahead()
    method returns and this code turns it right by 90
    degrees and starts moving in this
    directionturnRight(90)
  • Whenever it detects an enemy in front of it, it
    firesfire(1) //fires with powerlevel1
  • Whenever it is hit by an enemy bullet, it goes in
    the opposite directionturnLeft(180)

20
Installing Robocode
  • You download the Robocode JAR (3 Mb) from
    herehttp//robocode.sourceforge.net/
  • You install it by running the JARjava jar
    robocode-setup-1.2-Beta.jar
  • (or whatever the name is of the JAR file you
    download)

21
Running Robocode -1
Go To Battle Menu, New to choose robots for
your battle
22
Running Robocode -2
After you select your robots, click the Start
Battle button, and then the battle begins!
23
Robocode Editor
Go to the Robot Menu/Editor, to open the Editor
window. Select File Menu/New to create a new
robot. You can also use Eclipse to create your
robots.
24
Robocode and Eclipse
Go to the Options Menu, Preferences, and then go
tothe Development Options tab. Add you Eclipse
project to the list. When you create a New
Battle, remember to hit F5 (refresh)
25
Debugging Your Robot
  • You can watch your robot in action by creating a
    New Battle
  • You might want to compete against Sitting Duck
    in order to test some of your code
  • To see how your robot does against a sample
    robot, you can create a new battle and watch the
    action.
  • If you want to see the final results (for
    example, how many times out of 10 did you win),
    just minimize the robocode window. The game keeps
    going, but MUCH faster.
  • You can generate messages to the robocode console
    window by calling out.println like
    soout.println(Here is my message)
  • To view the messages, click on your robot name
    (button) in the upper right of the Robocode window

26
Exercise
  • Create a Robot subclass that can defeat the
    sample robot, MyFirstRobot
  • You can look at the other sample robot classes to
    get ideas on how to do this
  • You can either use the built in RoboCode editor
    or Eclipse. Put your code in a package with your
    name in it
  • For example robocode.jeff or gov.noaa.robocode.jef
    f
  • This way, we wont have package/class name
    conflicts when we have tournaments
Write a Comment
User Comments (0)
About PowerShow.com