idea: Robocode for Python

chris liechti cliechti at no.spam.gmx.net
Tue Sep 11 14:24:52 EDT 2001


porter at et.byu.edu (C. Porter Bassett) wrote in
news:84552519.0109110751.6abc9e27 at posting.google.com: 

>> Wouldn't jython fill the bill in this case?  
> 
> I had the same thought, and decided to try it.  Since I don't have any
> java nor jython experience, I didn't get very far.  I'll post my
> progress here in the hopes that somebody can help me get a litte
> further.
> 
> First of all, here's a very simple .java robot:
> 
>   package porter;
>   import robocode.*;
> 
>   public class CPB extends Robot
>   {
>     public void run()
> 
> 
>       while(true)
> 
> 
>         ahead(100);
>         turnGunRight(360);
>         back(100);
>         turnGunRight(360);
>       }
>     }
> 
>     public void onScannedRobot(ScannedRobotEvent e)
>     {
>       fire(1);
>     }
>   }
> 
> I have no idea what the package porter; line is for, so I ignored it.

"package" defines the in which package your code is. you would call your 
file "CPB.java" in the subdirectory "porter" (when you use the "package"
line)
"packages" are namespaces for java.

> I
> added the robocode.jar to my classpath, and tried to translate this
> java
> code to python.  Here's my python file:
> 
>   from robocode import *
>   class pyrobot(Robot):
>      def run(self):
>         while(1):
>            ahead(100)
>            turnGunRight(360)
>            back(100)
>            turnGunRight(360)
>      def onScannedRobot(self, event):
>         fire(1)
> 
> I ran that through jythonc, and it compiled, but I got a depracation
> warning.  I recompiled with -deprecation, and here's the output from
> jythonc:
> 
>   0932:PWORK://d/robocode/robots/porter$ jythonc pyrobot.py
>   processing pyrobot
> 
>   Required packages:
>     robocode.dialog
>     robocode.jdk13bugfix
>     robocode.editor
>     robocode*
> 
>   Creating adapters:
> 
>   Creating .java files:
>     pyrobot module
>       pyrobot extends robocode.Robot
> 
>   Compiling .java to .class...
>   Compiling with args: ['c://jdk1.3.1_01/bin/javac.exe',
> '-deprecation',
> '-classpath', 'C:\\jython-2.0\\jython.jar;.;C:\\Program
> Files\\proe2000i2\\modchk\\java\\xml\\xml4j.jar;c:\\jython-2.0\\jython.j
> ar;d 
>:\\robocode\\robocode.jar;.\\jpywork;;C:\\jython-2.0\\Tools\\jythonc;d:\\
>:rob 
>:ocode\\robots\\porter\\.;C:\\jython-2.0\\Lib;c:\\jython\\lib;C:\\jython-
>:2.0' 
> , '.\\jpywork\\pyrobot.java']
>   0  .\jpywork\pyrobot.java:100: warning:
> jfindattr(org.python.core.PyProxy,java.lang.String) in
> org.python.core.Py
> has been deprecated
>           PyObject inst = Py.jfindattr(this, "onScannedRobot");
>                             ^
>   .\jpywork\pyrobot.java:110: warning:
> jfindattr(org.python.core.PyProxy,java.lang.String) in
> org.python.core.Py
> has been deprecated
>           PyObject inst = Py.jfindattr(this, "run");
>                             ^
>   2 warnings
> 
> When I try to run a robocode battle with this robot, the robocode
> console
> gives me the following error:
> 
> 
>   java.lang.NoClassDefFoundError: porter/jpywork/pyrobot (wrong name:
> pyrobot)
>           at java.lang.ClassLoader.defineClass0(Native Method)
>           at java.lang.ClassLoader.defineClass(Unknown Source)
>           at java.lang.ClassLoader.defineClass(Unknown Source)
>           at
> robocode.JBotsClassLoader.loadRobotClass(JBotsClassLoader.java:55)
>           at robocode.Battle.initialize(Battle.java:350)
>           at robocode.Battle.run(Battle.java:42)
>           at java.lang.Thread.run(Unknown Source)
> 
> Because of my utter lack of java knowledge, I am completely stuck.  Is
> it
> even possible to do what I am trying to do?  If so, what should I try
> next?
> 

Looks like some naming problem. I don't know how this is handled with 
jython. As said above in java you usualy write one public class per file
and the file must have the name of the class + '.java'. The Java compiler
produeces then a file with the class name + '.class'. I think jython should
create a file named pyrobot.class in your case.

good look

chris <cliechti at gmx.net>




More information about the Python-list mailing list