Presentation Transcript
ROBOCODE CONTEST :1 ROBOCODE CONTEST Dr. Xiang Fu, Georgia Southwestern State University
Sponsored by GSW-ACM Chapter, CIS School
Acknowledgements :2 Acknowledgements Robocode is the brainchild of Mathew Nelson, a software engineer in the Advanced Technology, Internet division at IBM.
OUTLINE :3 OUTLINE Introduction (10 min)
Game Physics & API (15 min)
Practices (20 min)
Circular Walker
Smart Shooter
JBuilder Setup (5 min)
Contest Rules (3 min)
What is Robocode? :4 What is Robocode? Programming Game
1 rectangular arena
2 teams of robots
Survivor the winner
Unique feature
Player must write Java code
Mechanics
Tactics
Strategy
Quality of code matters
Why Robocode? :5 Why Robocode? It’s fun!
Arena to sharpen skills!
Great trophy waiting for you!
ACM student pack, ...
More potential opportunities!
How to Play? :6 How to Play? Install Robocode
Requirement:
Any PC Box with popular OS (windows, Linux, Solaris, ...)
Java JDK1.4.2 above installed
(freely available at
http://java.com/en/download/index.jsp)
Read Documentation
Program Your Robots
Then Have fun!
Installation (for CWH 221) :7 Installation (for CWH 221) This slide is for CWH 221 only!
Skip this slide for your own computer!
Set up Path
Control Panel
System
Advanced
System Variables
Add “PATH”
set value: “c:\Borland\JBuilder2005\JDK1.4\bin”
Installation :8 Installation Visit http://sourceforge.net/
Download robocode-setup-1.0.7.jar
Three ways to install:
(1) double click on the jar file
(2) in cmd window, type
“java –jar robocode-setup-1.0.7.jar”
(3) in cmd window
create a folder “robocode” to hold .jar
“jar –xvf robocode-setup-1.0.7.jar”
“jar –xvf extract.jar”
To run: type “robocode.bat”
Game Physics :9 Game Physics
Robot Anatomy :10 Robot Anatomy Tank Body
ahead, back, rotate Gun
fire, rotate Radar
rotate, scan, by default, rotate with gun
Tank Ability :11 Tank Ability Tank Speed: 1 – 5 pixels per round
Energy Points: 100
Ammunition NOT unlimited!
Each bullet costs 1 to 3 pt
speed: 20 – 3 * power
damage: 4 * power or 6*power-2 if power > 1
hit reward: 3 * power
hit rate hurt energy!
hit rate die earlier than enemy
Tank Control Interface :12 Tank Control Interface Write a Java , extending
Robot
AdvancedRobot
Override function run()
Specify its routine behavior
e.g., sweep radar, go ahead, turn around 180 degrees, back up ...
Override event handler
e.g., onHitByBullet()
e.g., onScannedRobot()
Events :13 Events ScannedRobotEvent
Enemy distance to me
Enemy bearing
Enemy energy
Enemy velocity
BulletHitEvent
BulletMissedEvent
HitByBulletEvent
HitRobotEvent
HitWallEvent
MessageEvent
RobotDeathEvent
WinEvent
Blocking Calls :14 Blocking Calls void ahead(double distance)
void back(double distance)
void stop()
void resume()
void turnGunLeft(double degrees)
void turnGunRight(double degrees)
void turnLeft(double degrees)
void turnRight(double degrees)
void turnRadarLeft(double degrees)
void turnRadarRight(double degrees)
void waitFor(robocode.Condition condition) Call does not return until requested action completed or interrupted by events, or condition is met
Non-Blocking Calls :15 Non-Blocking Calls void setAhead(double distance)
void setBack(double distance)
void setResume()
void setStop()
void setTurnGunLeft(double degrees)
void setTurnGunRight(double degrees)
void setTurnLeft(double degrees)
void setTurnRadarLeft(double degrees)
void setTurnRadarRight(double degrees)
void setTurnRight(double degrees) Call returns immediately. Multiple actions can be executed simultaneously. Usually provided by AdvancedRobot class
Example :16 Example What does the code below do?
setTurnRight(360);
setAhead(200);
waitFor(new TurnCompleteCondition(this));
Many Other Calls :17 Many Other Calls double getEnergy()
double getHeight(), getWidth()
double getX(), getY()
double getVelocity()
int getOthers()
string getName()
double getBattleFieldWidth()
double getBattleFieldHeight()
Important Angles :18 Important Angles double getHeading()
double getGunHeading()
double getRadarHeading()
void setAdjustGunForRobotTurn(boolean)
void setAdjustRadarForGunTurn(boolean)
void setAdjustRadarForRobotTurn(boolean)
When Enemy Detected :19 When Enemy Detected ScannedRobotEvent
double getBearing() in degrees
double getBearingRadians() in radians Note: getBearing() NOT affected by enemy heading or gunHeading.
More Information? :20 More Information? Complete API available at
$installdir/javadoc/index.html
Read the following classes!
Robot
AdvancedRobot
ScannedRobotEvent, ... Event...
Practice Time :21 Practice Time
Task1: Rectangle Walker :22 Task1: Rectangle Walker Fire when enemy detected
Walk on the perimeter of a rectangle (edge length 200) Algorithm:
1. Adjust the heading to 0’
2. loop{
go ahead 200
turn right 90’
}
Source Code :23 Source Code public void run() {
//1. set the heading angle to 0'
turnLeft(this.getHeading());
//2. loop, walk and turn
while(true){
ahead(100);
turnRight(90);
}
}
Task2: CircleWalker :24 Task2: CircleWalker Hint: Use AdvancedRobot
Algorithm :25 Algorithm Hint: Use AdvancedRobot Algorithm:
loop{
setTurnRight(180)
setAhead(a big number)
wait until turn complete
turnLeft(90)
}
Source Code :26 Source Code public void run() {
while(true) {
turnLeft(this.getHeading());
while(true){
setTurnRight(180);
setAhead(30000);
waitFor(new TurnCompleteCondition(this));
setAhead(0);
turnLeft(90);
}
}
}
Who’s the BEST ROBOT? :27 Who’s the BEST ROBOT? Set up a battle with 3 rounds
Enlist all robots available (except teams)
Who’s the best performer?
Walls or SpinBot
Because they move fast and hard to aim!
Task3: WallKiller :28 Task3: WallKiller Design a Smart Shooter which can correct calculate the fire-angle to kill a wall robot easily!
We need a little bit Maths!
For DIGITAL COPY of Code :29 For DIGITAL COPY of Code Log in
ACM_GSW@hotmail.com
Passwd: abc123#001
Copy&Paste the file
Play with it!
Magic of Mathematics :30 Magic of Mathematics Known Facts:
enemy speed, distance, enemy heading, enemy velocity, bullet velocity formula, my heading, my gun heading
Known System Parameters :31 Known System Parameters Assume bullet travel time: t
Let v1 = 20-3*bulletPower
Let v2 = ScannedRobotEvent.getVelocity() v1 * t v2 * t d
Known System Parameters :32 Known System Parameters this.getHeading() ScannedRobotEvent.getHeading() beta = (180-Event.getHeading) + this.getHeading() + Event.getBearing()
Calculate Bullet Travel Time :33 Calculate Bullet Travel Time v1 * t v2 * t d
Calculate Firing Angle :34 Calculate Firing Angle v1 * t v2 * t d alpha?
The Real Firing Angle :35 The Real Firing Angle = this.getHeading() + Event.getBearing() + alpha
Source Code :36 Source Code private double getBulletSpeed(double power) {
return 20.0 - 3.0 * power;
}
Source Code :37 Source Code public void onScannedRobot(ScannedRobotEvent e)
{
//setting the parameters
double d = e.getDistance();
double beta = e.getHeading() +
(180- e.getBearing() + this.getHeading());
double v2 = e.getVelocity();
double v1 = this.getBulletSpeed(2);
double A = v2*v2-v1*v1;
double B = -2*d*v2*Math.cos(Math.toRadians(beta));
double C = d*d;
Source Code :38 Source Code //continued from the previous slide
double t = (Math.sqrt(B*B-4*A*C)-B)/(2*A);
double alpha = Math.acos(
(v2*v2*t*t+d*d-v1*v1*t*t)/(2*d*v2*t));
alpha = Math.toDegrees(beta);
double fireAngle = (this.getHeading() + e.getBearing() + alpha) - this.getGunHeading();
turnRight(fireAngle);
fire(2);
}
Dubug: JBuilder Setup :39 Dubug: JBuilder Setup
JBuilder :40 JBuilder Foundation version is Free!
Go to Borland site to download to your own computer
We have JBuilder installed in CWH 221!
Set Up JBuilder :41 Set Up JBuilder Create Library Reference for Robocode.jar
Tools Configure Libraries
Click “new button” on left bottom
Input name “robocode”, click “ok”
click “add” button on the right
scroll down and select “robocode/robocode.jar”
click “ok” and finish
Create Robot in JBuilder :42 Create Robot in JBuilder Close all OLD projects
File New Project
In Project Manager Window
Right click on “__your_proj_name__.jpx”
Select and click on “properties”
Select the 1st item, “path” as follows
Suppose $robocode is the installation dir of robocode
Set output path to “$rocode/robots”
Set working path to “$robocode”
In the tabcontrol, click “required libraries”
click “Add”, then add the “robocode” library
Set UP Debugging :43 Set UP Debugging click on menu item “run”
click on “configurations”
click “new” or “edit”
set “main class” be “robocode/robocode”
Now you can create a file and run.
You can also do step by step debugging!
Competition Rules :44 Competition Rules Single person team
Each team: up to 5 robots
Each team will battle against all registered teams
Results for each battle will be recorded
Winners are ranked according to the number of wins
The top 8 winner has $45 trophy each
Arena Size :45 Arena Size Each battle needs to be done in 2 sizes
800 * 800
2000 * 2000
Any tie is broken by have additional battles
GSW ACM Chapter has the final decision
Honesty Requirement :46 Honesty Requirement All work must be completed individually
The top 8 winners must be able to present the algorithms and explain the code
Should NOT copy source code from sourceforge, or robocode repository
Good Luck! :47 Good Luck! Competition starts at Jan. 27th, 2006!