Discussion:
Importing Custom Classes
o***@gmail.com
2015-04-25 22:31:55 UTC
Permalink
This may be a silly beginner question, but how do I access functions after
importing a custom class? When I compile I receive 'undefined method'
errors. Ideally I would like to clean up my code and make it more reusable.
Robowiki and repository are down, so my options to learn are pretty limited.


myUtils.java (compiles)
package mo;
import robocode.*;

public class myUtils extends AdvancedRobot{
public void spin() {
setTurnRight(10000);
}
}

myBot.java (doesnt compile)
package mo;
import robocode.*;
import mo.myUtils;
import java.awt.Color;

public class myBot {

public void run() {
while(true) {
spin();
}
}
}

Compile Error:
Compiling...
----------
1. ERROR in D:\MyFiles\Games\RoboCode\robots\mo\MyBot2.java (at line 11)
spin();
^^^^
The method spin() is undefined for the type MyBot2
----------
1 problem (1 error)
Compile Failed (-1)
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robocode+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
fnl
2015-04-26 09:17:55 UTC
Permalink
Hi Mike,

Your myBot class must extend your myUtils, which contains your spin()
method.
So you must write:

package mo;
import mo.myUtils;

public class myBot extends myUtils {

public void run() {
while(true) {
spin();
}
}
}

Also note that your class names should not start with a small letter, but a
big capital letter. That is a common Java convention.
So you should change 'myUtils' into 'MyUtils' and change 'myBot' into
'MyBot' in both class files. :-)

Best regards,
- Flemming
Post by o***@gmail.com
This may be a silly beginner question, but how do I access functions after
importing a custom class? When I compile I receive 'undefined method'
errors. Ideally I would like to clean up my code and make it more reusable.
Robowiki and repository are down, so my options to learn are pretty limited.
myUtils.java (compiles)
package mo;
import robocode.*;
public class myUtils extends AdvancedRobot{
public void spin() {
setTurnRight(10000);
}
}
myBot.java (doesnt compile)
package mo;
import robocode.*;
import mo.myUtils;
import java.awt.Color;
public class myBot {
public void run() {
while(true) {
spin();
}
}
}
Compiling...
----------
1. ERROR in D:\MyFiles\Games\RoboCode\robots\mo\MyBot2.java (at line 11)
spin();
^^^^
The method spin() is undefined for the type MyBot2
----------
1 problem (1 error)
Compile Failed (-1)
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robocode+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mike Oliver
2015-04-27 04:02:39 UTC
Permalink
Oh, of course, that makes complete sense. As I mentioned I'm new to Java,
so your answer is much appreciated.
Mike
Post by fnl
Hi Mike,
Your myBot class must extend your myUtils, which contains your spin()
method.
package mo;
import mo.myUtils;
public class myBot extends myUtils {
public void run() {
while(true) {
spin();
}
}
}
Also note that your class names should not start with a small letter, but
a big capital letter. That is a common Java convention.
So you should change 'myUtils' into 'MyUtils' and change 'myBot' into
'MyBot' in both class files. :-)
Best regards,
- Flemming
Post by o***@gmail.com
This may be a silly beginner question, but how do I access functions
after importing a custom class? When I compile I receive 'undefined
method' errors. Ideally I would like to clean up my code and make it more
reusable.
Robowiki and repository are down, so my options to learn are pretty limited.
myUtils.java (compiles)
package mo;
import robocode.*;
public class myUtils extends AdvancedRobot{
public void spin() {
setTurnRight(10000);
}
}
myBot.java (doesnt compile)
package mo;
import robocode.*;
import mo.myUtils;
import java.awt.Color;
public class myBot {
public void run() {
while(true) {
spin();
}
}
}
Compiling...
----------
1. ERROR in D:\MyFiles\Games\RoboCode\robots\mo\MyBot2.java (at line 11)
spin();
^^^^
The method spin() is undefined for the type MyBot2
----------
1 problem (1 error)
Compile Failed (-1)
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robocode+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mike Oliver
2015-07-17 05:55:31 UTC
Permalink
Hi Everyone,
Back again for questions regarding custom classes. I've broken out my bot
into multiple files and am having troubles understanding where I've gone
wrong. When I run my bot I get:

robocode.exception.RobotException: You cannot call the getHeadingRadians()
method before your run() method is called, or you are using a Robot object
that the game doesn't know about.
at robocode._RobotBase.uninitializedException(_RobotBase.java:88)
at
robocode._AdvancedRadiansRobot.getHeadingRadians(_AdvancedRadiansRobot.java:40)
at robocode.AdvancedRobot.getHeadingRadians(AdvancedRobot.java:1439)
at mo.data.EnemyData.update(EnemyData.java:25)
at mo.PewPew.onScannedRobot(PewPew.java:18)

I'm trying to figure out how to declare my bot object properly so that
methods like getHeadingRadians will work.
Any input or suggestions is really appreciated.
Thanks
Mike


Pew.Pew.java
package mo;
import mo.data.*;

import robocode.*;

public class PewPew extends AdvancedRobot
{
private EnemyData enemy = new EnemyData();
public void run() {
while(true) turnRadarRightRadians(Double.POSITIVE_INFINITY);
}
public void onScannedRobot(ScannedRobotEvent e) {
enemy.update(e);
}
public void onRobotDeath(RobotDeathEvent e) {
enemy.update(e);
}
}

EnemyData.Java
package mo.data;
import mo.utils.*;

import robocode.*;

import java.util.HashMap;
import java.util.LinkedHashMap;

public class EnemyData {

public static AdvancedRobot myRobot = new AdvancedRobot();
public static LinkedHashMap<String, HashMap<String, Object>> enemy = new
LinkedHashMap<String, HashMap<String, Object>>(5, 2, false);
protected double velocity;
protected double bearing;
protected double absBearing;
//onScannedRobot
public void update(ScannedRobotEvent e) {
String name = e.getName();
if (!enemy.containsKey(name))
enemy.put(name, new HashMap<String, Object>());

enemy.get(name).put("velocity", e.getVelocity());
enemy.get(name).put("absBearing", myRobot.getHeadingRadians() +
e.getBearingRadians());
enemy.get(name).put("bearing", e.getBearingRadians());
Utils.printMap(enemy); //print out debug info
}
//onRobotDeath
public void update(RobotDeathEvent e) {
enemy.remove(e.getName());
}
}

Utils.java
package mo.utils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


public class Utils {
//print out Map information
public static void printMap(LinkedHashMap<String, HashMap<String, Object>>
map) {
for (Map.Entry<String, HashMap<String, Object>> cursor : map.entrySet())
System.out.println(cursor.getKey() +"="+ cursor.getValue() +"\r");
System.out.println("\r");
}
}
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robocode+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Johannes Slotta
2015-07-17 09:06:20 UTC
Permalink
Hi, you should not set myRobot to a new AdvancedRobot instance (which the
game cannot recognise), but to your own one given by the game. Do this by
using a custom constructor for EnemyData. HTH Johannes
Post by Mike Oliver
Hi Everyone,
Back again for questions regarding custom classes. I've broken out my bot
into multiple files and am having troubles understanding where I've gone
robocode.exception.RobotException: You cannot call the getHeadingRadians()
method before your run() method is called, or you are using a Robot object
that the game doesn't know about.
at robocode._RobotBase.uninitializedException(_RobotBase.java:88)
at
robocode._AdvancedRadiansRobot.getHeadingRadians(_AdvancedRadiansRobot.java:40)
at robocode.AdvancedRobot.getHeadingRadians(AdvancedRobot.java:1439)
at mo.data.EnemyData.update(EnemyData.java:25)
at mo.PewPew.onScannedRobot(PewPew.java:18)
I'm trying to figure out how to declare my bot object properly so that
methods like getHeadingRadians will work.
Any input or suggestions is really appreciated.
Thanks
Mike
Pew.Pew.java
package mo;
import mo.data.*;
import robocode.*;
public class PewPew extends AdvancedRobot
{
private EnemyData enemy = new EnemyData();
public void run() {
while(true) turnRadarRightRadians(Double.POSITIVE_INFINITY);
}
public void onScannedRobot(ScannedRobotEvent e) {
enemy.update(e);
}
public void onRobotDeath(RobotDeathEvent e) {
enemy.update(e);
}
}
EnemyData.Java
package mo.data;
import mo.utils.*;
import robocode.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class EnemyData {
public static AdvancedRobot myRobot = new AdvancedRobot();
public static LinkedHashMap<String, HashMap<String, Object>> enemy = new
LinkedHashMap<String, HashMap<String, Object>>(5, 2, false);
protected double velocity;
protected double bearing;
protected double absBearing;
//onScannedRobot
public void update(ScannedRobotEvent e) {
String name = e.getName();
if (!enemy.containsKey(name))
enemy.put(name, new HashMap<String, Object>());
enemy.get(name).put("velocity", e.getVelocity());
enemy.get(name).put("absBearing", myRobot.getHeadingRadians() +
e.getBearingRadians());
enemy.get(name).put("bearing", e.getBearingRadians());
Utils.printMap(enemy); //print out debug info
}
//onRobotDeath
public void update(RobotDeathEvent e) {
enemy.remove(e.getName());
}
}
Utils.java
package mo.utils;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class Utils {
//print out Map information
public static void printMap(LinkedHashMap<String, HashMap<String, Object>>
map) {
for (Map.Entry<String, HashMap<String, Object>> cursor : map.entrySet())
System.out.println(cursor.getKey() +"="+ cursor.getValue() +"\r");
System.out.println("\r");
}
}
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robocode+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mike Oliver
2015-07-22 07:34:37 UTC
Permalink
Ok, I did more research into properly setting up my Java files and looked
into constructors as Johannes suggested but I'm still missing something. I
still cant seem to access my BotData information inside my EnemyData class.
I have a sneaky suspicion that its because I'm overloading my
constructors. Maybe that's the issue?

Also I apologize for what I can assume is very newbish Java questions,
opposed to actual Robocode stuff, its just easier to bounce ideas off you
guys since you have likely dealt with this before.

Thanks again for all your help. Eventually I will get this =D

PewPew.java
package mo;

import java.awt.Graphics2D;
import mo.Data.*;
import mo.Utils.*;
import robocode.*;

public class PewPew extends AdvancedRobot {

BotData myBot;
EnemyData enemy = new EnemyData();
RadarData radar = new RadarData();
PaintUtils paint = new PaintUtils();

public void run() {
myBot = new BotData(this);
while (true) {
//System.out.println("PewPew: " + myBot.getPos());
setTurnRadarRightRadians(radar.getRadarDir() * Double.POSITIVE_INFINITY);
scan();
}
}

public void onScannedRobot(ScannedRobotEvent e) {
//myBot.update();
enemy.update(e);
// radar.update(e);
}

public void onRobotDeath(RobotDeathEvent e) {
enemy.update(e);
}

public void onPaint(Graphics2D g) {
/*
if (enemy.getPos() != null) {
paint.drawPos(g, enemy.getPos());
}
*/
}
}

BotData.java
package mo.Data;

import robocode.*;
import java.awt.geom.Point2D;

public class BotData{

//VARIABLES
private AdvancedRobot myBot;
private Point2D.Double myPos;
private double headingRadians;
private double radarHeadingRadians;
private int getOthers;

// CONSTRUCTORS
public BotData() {
}

public BotData(AdvancedRobot robot) {
myBot = robot;
myPos = new Point2D.Double(myBot.getX(), myBot.getY());
headingRadians = myBot.getHeadingRadians();
radarHeadingRadians = myBot.getRadarHeadingRadians();
getOthers = myBot.getOthers();
}

// ACCESSORS
public Point2D.Double getPos() {
return this.myPos;
}

public double getHeadingRadians() {
return this.headingRadians;
}

public double getRadarHeadingRadians() {
return this.radarHeadingRadians;
}

public int getOthers() {
return this.getOthers;
}

// METHODS
/*
public void update() {
myPos = new Point2D.Double(myBot.getX(), myBot.getY());
headingRadians = myBot.getHeadingRadians();
radarHeadingRadians = myBot.getRadarHeadingRadians();
getOthers = myBot.getOthers();
}
*/
}

EnemyData.java
package mo.Data;

import robocode.*;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.LinkedHashMap;
import mo.Utils.*;

public class EnemyData {

// VARIABLES
BotData myBot = new BotData();
BotUtils utils = new BotUtils();

private String eName;
private double eAbsBearing;
private double eBearing;
private double eDistance;
private double eVelocity;
private Point2D.Double ePos;
private LinkedHashMap<String, HashMap<String, Object>> eMap = new
LinkedHashMap<String, HashMap<String, Object>>(5, 2, true);

// ACCESSORS
public String getName() {
return this.eName;
}

public double getAbsBearing() {
return this.eAbsBearing;
}

public double getBearing() {
return this.eBearing;
}

public double getVelocity() {
return this.eVelocity;
}

public Point2D.Double getPos() {
return this.ePos;
}

public LinkedHashMap<String, HashMap<String, Object>> getMap() {
return this.eMap;
}

// METHODS
public void update(ScannedRobotEvent e) {
/*
eName = e.getName();
eDistance = e.getDistance();
eVelocity = e.getVelocity();
eBearing = e.getBearingRadians();
eAbsBearing = myBot.getHeadingRadians() + e.getBearingRadians();
ePos = utils.getPos(myBot.getPos(), eAbsBearing, eDistance);

// Add enemy information to Map
eMap.put(eName, new HashMap<String, Object>());
eMap.get(eName).put("eVelocity", eVelocity);
eMap.get(eName).put("eAbsBearing", eAbsBearing);
eMap.get(eName).put("eBearing", eBearing);
eMap.get(eName).put("ePos", ePos);
*/
System.out.println("EnemyData: " + myBot.getPos());
}

public void update(RobotDeathEvent e) {
eMap.remove(e.getName());
}
}
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robocode+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nat Pavasant
2015-07-22 07:38:14 UTC
Permalink
In the same fashion you need to pass AdvancedRobot to your BotData class,
you also need to pass the BotData class created in the PewPew into
EnemyData class.

Whenever you use `new` to create object, the object is different from every
other one. If you want to same object, so you could share data, then pass
the object to the class.

- Nat
Post by Mike Oliver
Ok, I did more research into properly setting up my Java files and looked
into constructors as Johannes suggested but I'm still missing something. I
still cant seem to access my BotData information inside my EnemyData
class. I have a sneaky suspicion that its because I'm overloading my
constructors. Maybe that's the issue?
Also I apologize for what I can assume is very newbish Java questions,
opposed to actual Robocode stuff, its just easier to bounce ideas off you
guys since you have likely dealt with this before.
Thanks again for all your help. Eventually I will get this =D
PewPew.java
package mo;
import java.awt.Graphics2D;
import mo.Data.*;
import mo.Utils.*;
import robocode.*;
public class PewPew extends AdvancedRobot {
BotData myBot;
EnemyData enemy = new EnemyData();
RadarData radar = new RadarData();
PaintUtils paint = new PaintUtils();
public void run() {
myBot = new BotData(this);
while (true) {
//System.out.println("PewPew: " + myBot.getPos());
setTurnRadarRightRadians(radar.getRadarDir() * Double.POSITIVE_INFINITY);
scan();
}
}
public void onScannedRobot(ScannedRobotEvent e) {
//myBot.update();
enemy.update(e);
// radar.update(e);
}
public void onRobotDeath(RobotDeathEvent e) {
enemy.update(e);
}
public void onPaint(Graphics2D g) {
/*
if (enemy.getPos() != null) {
paint.drawPos(g, enemy.getPos());
}
*/
}
}
BotData.java
package mo.Data;
import robocode.*;
import java.awt.geom.Point2D;
public class BotData{
//VARIABLES
private AdvancedRobot myBot;
private Point2D.Double myPos;
private double headingRadians;
private double radarHeadingRadians;
private int getOthers;
// CONSTRUCTORS
public BotData() {
}
public BotData(AdvancedRobot robot) {
myBot = robot;
myPos = new Point2D.Double(myBot.getX(), myBot.getY());
headingRadians = myBot.getHeadingRadians();
radarHeadingRadians = myBot.getRadarHeadingRadians();
getOthers = myBot.getOthers();
}
// ACCESSORS
public Point2D.Double getPos() {
return this.myPos;
}
public double getHeadingRadians() {
return this.headingRadians;
}
public double getRadarHeadingRadians() {
return this.radarHeadingRadians;
}
public int getOthers() {
return this.getOthers;
}
// METHODS
/*
public void update() {
myPos = new Point2D.Double(myBot.getX(), myBot.getY());
headingRadians = myBot.getHeadingRadians();
radarHeadingRadians = myBot.getRadarHeadingRadians();
getOthers = myBot.getOthers();
}
*/
}
EnemyData.java
package mo.Data;
import robocode.*;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.LinkedHashMap;
import mo.Utils.*;
public class EnemyData {
// VARIABLES
BotData myBot = new BotData();
BotUtils utils = new BotUtils();
private String eName;
private double eAbsBearing;
private double eBearing;
private double eDistance;
private double eVelocity;
private Point2D.Double ePos;
private LinkedHashMap<String, HashMap<String, Object>> eMap = new
LinkedHashMap<String, HashMap<String, Object>>(5, 2, true);
// ACCESSORS
public String getName() {
return this.eName;
}
public double getAbsBearing() {
return this.eAbsBearing;
}
public double getBearing() {
return this.eBearing;
}
public double getVelocity() {
return this.eVelocity;
}
public Point2D.Double getPos() {
return this.ePos;
}
public LinkedHashMap<String, HashMap<String, Object>> getMap() {
return this.eMap;
}
// METHODS
public void update(ScannedRobotEvent e) {
/*
eName = e.getName();
eDistance = e.getDistance();
eVelocity = e.getVelocity();
eBearing = e.getBearingRadians();
eAbsBearing = myBot.getHeadingRadians() + e.getBearingRadians();
ePos = utils.getPos(myBot.getPos(), eAbsBearing, eDistance);
// Add enemy information to Map
eMap.put(eName, new HashMap<String, Object>());
eMap.get(eName).put("eVelocity", eVelocity);
eMap.get(eName).put("eAbsBearing", eAbsBearing);
eMap.get(eName).put("eBearing", eBearing);
eMap.get(eName).put("ePos", ePos);
*/
System.out.println("EnemyData: " + myBot.getPos());
}
public void update(RobotDeathEvent e) {
eMap.remove(e.getName());
}
}
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robocode+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...