idea: Robocode for Python

Martin Franklin martin.franklin at westgeo.com
Thu Sep 13 12:41:40 CEST 2001


C. Porter Bassett wrote:

>> > 
>> > 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");
>> > 
>> 
>> Deprecated methods should not be used for new designs as these might
>> disapear in a future version of Java. On the other hand this means that
>> these methods are there and perfectly working, its only a warning.
> 
> OK, how am I using depreciated methods?  I wasn't doing anything
> special - just defining a class with two methods.  Here's my code
> again:
> 
> from robocode import *
> class pyrobot(Robot):
>    def run(self):
>       while(1):
>          self.ahead(100)
>          self.turnGunRight(360)
>          self.back(100)
>          self.turnGunRight(360)
>    def onScannedRobot(self, event):
>       self.fire(1)
> 

I guess you don't need to wory about the  depreciated methods (yet!) lets 
try getting it working first...

To elimimate robocode from the equation create a robocode.py file like so:-



################### code ##################


class Robot:        
    def ahead(self, value):
        print 'Ahead', value
        
    def turnGunRight(self, value):
        print 'turnGunRight', value
        
    def back(self, value):
        print 'Back', value
        
    def fire(self, value):
        print 'Fire', value

################### end code ##################
        
next your code:

################### code ##################
from robocode import *
class pyrobot(Robot):
   def run(self):
      for n in range(5):
         self.ahead(100)
         self.turnGunRight(360)
         self.back(100)
         self.turnGunRight(360)
   def onScannedRobot(self, event):
      self.fire(1)




if __name__=='__main__':
    myrobot=pyrobot()
    myrobot.run()

################### end code ##################


Ok so i've made a small change (no while 1 loop!) and i've added the magic 
if __name__ thing so I can execute it like so:-


Standard Cpython:-

[bpse at m-franklin Jscripts]$ python pyrobot.py
Ahead 100
turnGunRight 360
Back 100
turnGunRight 360
Ahead 100
turnGunRight 360
Back 100
turnGunRight 360
[bpse at m-franklin Jscripts]$

Now jython :-

[bpse at m-franklin Jscripts]$ jython pyrobot.py
Ahead 100
turnGunRight 360
Back 100
turnGunRight 360
Ahead 100
turnGunRight 360
Back 100
turnGunRight 360
[bpse at m-franklin Jscripts]$


Now compile it to java:-

[bpse at m-franklin Jscripts]$ ls -la
total 28
drwxrwxr-x    3 bpse     bpse         4096 Sep 13 11:38 .
drwxrwxrwx   32 root     root         4096 Sep 12 16:40 ..
drwxrwxr-x    2 bpse     bpse         4096 Sep 13 11:38 jpywork
-rw-rw-r--    1 bpse     bpse          325 Sep 13 11:37 pyrobot.py
-rw-rw-r--    1 bpse     bpse          298 Sep 13 11:33 robocode.py
-rw-rw-r--    1 bpse     bpse          787 Sep 13 11:33 robocode.pyc
-rw-rw-r--    1 bpse     bpse         2887 Sep 13 11:34 robocode$py.class
[bpse at m-franklin Jscripts]$ jythonc *.py
processing pyrobot
processing robocode
 
Required packages:
 
Creating adapters:
 
Creating .java files:
  robocode module
  pyrobot module
 
Compiling .java to .class...
Compiling with args: ['/usr/java/jdk1.3/bin/javac', '-classpath', 
'/usr/python/Jython/jython.jar:/usr/python/Jython/jython.jar:./:./jpywork::/usr/python/Jython/Tools/jythonc:/usr/python/Jscripts/.:/usr/python/Jython/Lib', 
'./jpywork/robocode.java', './jpywork/pyrobot.java']
0
[bpse at m-franklin Jscripts]$



now execute it with java:-

[bpse at m-franklin Jscripts]$ cd jpywork/
[bpse at m-franklin jpywork]$ export CLASSPATH=/usr/python/Jython/jython.jar:./
[bpse at m-franklin jpywork]$ java pyrobot
Ahead 100
turnGunRight 360
Back 100
turnGunRight 360
Ahead 100
turnGunRight 360
Back 100
turnGunRight 360
[bpse at m-franklin jpywork]$


I guess what i'm trying to say is if you get this working there nothing 
wrong with the jython/python side of things and I am at a loss as to what 
is happening (I don't know what robocode is :-( sorry)















More information about the Python-list mailing list