Robocode Java Compiler issues

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
RockoTDF
Posts: 582
Joined: Thu Oct 18, 2007 6:08 am UTC
Location: Tucson, AZ, US
Contact:

Robocode Java Compiler issues

Postby RockoTDF » Sun Nov 25, 2007 9:37 pm UTC

I'm in an intro level CS course, and we have been given the assignment to program our own robots in Java in the Robocode http://www.robocode.sourceforge.net environment. It uses the javac compiler.

I am in the process of improving my robot, and keep getting some compiler errors that seem to make no sense. I KNOW that parts of this code, especially the ones that are getting errors, work. They are taken from the sample bots or have worked in my robot already.

I have checked and made sure I don't have any weird variables, and I think my { } 's are set properly. I would really like to know what is causing these errors. Thanks for your help.

And please, none of the "if you cant do this you will suck" comments....we were kind of thrown to the wolves with this.

Code: Select all


 
import robocode.*;

public class HAL10000 extends AdvancedRobot
 

{
   /**
    * FirstRobot's run method - this is the method that contains the important algorithm
    */
   public void run()
       {
      
      // Movement algorithm
      
      turnGunRight(90);
      while(true)
         {
      scan();
      ahead(200);
      scan();
      turnGunRight(90);
      turnRight(140);
      scan();
      ahead(200);
      turnRight(140);
      ahead(200);
      turnGunRight(360);
         }
   }
   
   public void onScannedRobot()       {   
               
            fire(1);
      
            run();
                  }   
                  
      
   public void onHitRobot(HitRobotEvent) //error here, line 41
            {
       if (event.getBearing() > -90 && event.getBearing() <= 90) {
           back(100);
       } else
            {
           ahead(100);
       }
            }

   public void onHitWall(HitWallEvent event){
          turnRight(180);
         reset();
         }


   
   public void reset()
         {
         turnLeft(getHeading());
         ahead(getBattleFieldHeight()/2 - getY());
      
      if(getX() > getBattleFieldWidth()/2))
         {
         turnLeft(90);
         ahead(getX() - getBattleFieldWidth()/2);
         turnRight(90);
         };
      else
         {
         turnRight(90);
         ahead(getBattleFieldWidth()/2 - getX());
         turnLeft(90);
            };
         }
      }      //this is line 76
   } }
} //line 78
//line 79



Here are the errors

/Users/brianroller/Desktop/download/robocode/robots/HAL10000.java:41: <identifier> expected
public void onHitRobot(HitRobotEvent)
^
/Users/brianroller/Desktop/download/robocode/robots/HAL10000.java:76: ')' expected
}
^
/Users/brianroller/Desktop/download/robocode/robots/HAL10000.java:77: 'class' or 'interface' expected
} }
^
/Users/brianroller/Desktop/download/robocode/robots/HAL10000.java:79: 'class' or 'interface' expected
^
4 errors
Compile Failed (1)
Just because it is not physics doesn't mean it is not science.
http://www.iomalfunction.blogspot.com <---- A collection of humorous one liners and science jokes.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Robocode Java Compiler issues

Postby Rysto » Sun Nov 25, 2007 10:21 pm UTC

Good God, fix your indentation! Anyway, here are the problems that I see:

/Users/brianroller/Desktop/download/robocode/robots/HAL10000.java:41: <identifier> expected
public void onHitRobot(HitRobotEvent)
^
public void onHitRobot(HitRobotEvent event)

Your reset method is an absolute disaster. Here's what it should look like:

public void reset()
{
turnLeft(getHeading());
ahead(getBattleFieldHeight()/2 - getY());

if(getX() > getBattleFieldWidth()/2))
{
turnLeft(90);
ahead(getX() - getBattleFieldWidth()/2);
turnRight(90);
}
else
{
turnRight(90);
ahead(getBattleFieldWidth()/2 - getX());
turnLeft(90);
} //this is line 76
}
} //line 78

You should get an editor that does syntax highlighting and bracket matching -- it makes coding much easier.

User avatar
RockoTDF
Posts: 582
Joined: Thu Oct 18, 2007 6:08 am UTC
Location: Tucson, AZ, US
Contact:

Re: Robocode Java Compiler issues

Postby RockoTDF » Sun Nov 25, 2007 11:13 pm UTC

Rysto wrote:Good God, fix your indentation!

You should get an editor that does syntax highlighting and bracket matching -- it makes coding much easier.


The reset section was what we were given to work with, and it was working fine until this afternoon. Like I said before, we were basically just handed this assignment without a great deal of explanation as to the finer points of java. I'm making the robot more complicated so that it can compete against other students for extra credit.

As for a better editor, can you recommend one for OS X or Linux? If not I will just google until I find a good one.

Still getting errors. Solved a lot of them, but here it is as of now:

Code: Select all

import robocode.*;

public class HAL10000 extends AdvancedRobot
 

{
   /**
    * FirstRobot's run method - this is the method that contains the important algorithm
    */
public void run()
{
// Movement algorithm
turnGunRight(90);
while(true)
{
scan();
ahead(200);
scan();
turnGunRight(90);
turnRight(140);
scan();
ahead(200);
turnRight(140);
ahead(200);
turnGunRight(360);
}
}

public void onScannedRobot()
{   
fire(1);
run();
}   
                  
public void onHitRobot(HitRobotEvent event)
{
if (event.getBearing() > -90 && event.getBearing() <= 90)
{
back(100);
}
else
{
ahead(100);
}
}
public void onHitWall(HitWallEvent event)
{
turnRight(180);
reset();
}


public void reset()
{
turnLeft(getHeading());
ahead(getBattleFieldHeight()/2 - getY());
}
if(getX() > getBattleFieldWidth()/2))
{
turnLeft(90);
ahead(getX() - getBattleFieldWidth()/2); //line 59
turnRight(90);
}
else
{
turnRight(90);
ahead(getBattleFieldWidth()/2 - getX());
turnLeft(90);
}
} //line 71


Compiling...
/Users/brianroller/Desktop/download/robocode/robots/HAL10000.java:59: illegal start of type
if(getX() > getBattleFieldWidth()/2))
^
/Users/brianroller/Desktop/download/robocode/robots/HAL10000.java:71: <identifier> expected
}
^
2 errors
Compile Failed (1)

This is especially frustrating as line 59 has been working all along until now.
Just because it is not physics doesn't mean it is not science.
http://www.iomalfunction.blogspot.com <---- A collection of humorous one liners and science jokes.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Robocode Java Compiler issues

Postby Rysto » Sun Nov 25, 2007 11:58 pm UTC

Under KDE I use the Kate editor.

You problem is here:

public void reset()
{
turnLeft(getHeading());
ahead(getBattleFieldHeight()/2 - getY());
} // <-----

By putting a closing brace there, you're saying that the reset() method finishes there. That's not what you want, so get rid of the closing brace.

User avatar
RockoTDF
Posts: 582
Joined: Thu Oct 18, 2007 6:08 am UTC
Location: Tucson, AZ, US
Contact:

Re: Robocode Java Compiler issues

Postby RockoTDF » Mon Nov 26, 2007 12:09 am UTC

Thanks, but I am still getting the "illegal start of expression error" on that same line.
Just because it is not physics doesn't mean it is not science.
http://www.iomalfunction.blogspot.com <---- A collection of humorous one liners and science jokes.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Robocode Java Compiler issues

Postby Rysto » Mon Nov 26, 2007 12:17 am UTC

You are missing a closing brace at the end of the file. I'm not sure if that's causing the compile error that you're seeing.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Robocode Java Compiler issues

Postby headprogrammingczar » Mon Nov 26, 2007 12:44 am UTC

RockoTDF wrote:

Code: Select all


 
import robocode.*;

public class HAL10000 extends AdvancedRobot
 

{
   /**
    * FirstRobot's run method - this is the method that contains the important algorithm
    */
   public void run()
       {
      
      // Movement algorithm
      
      turnGunRight(90);
      while(true)
         {
      scan();
      ahead(200);
      scan();
      turnGunRight(90);
      turnRight(140);
      scan();
      ahead(200);
      turnRight(140);
      ahead(200);
      turnGunRight(360);
         }
   }
   
   public void onScannedRobot()       {   
               
            fire(1);
      
            run();
                  }   
                  
      
   public void onHitRobot(HitRobotEvent [b]event[/b]) //<-----error [s]here[/s] [b]fixed[/b], line 41
            {
       if (event.getBearing() > -90 && event.getBearing() <= 90) {
           back(100);
       } else
            {
           ahead(100);
       }
            }

   public void onHitWall(HitWallEvent event){
          turnRight(180);
         reset();
         }


   
   public void reset()
         {
         turnLeft(getHeading());
         ahead(getBattleFieldHeight()/2 - getY());
      
      if(getX() > getBattleFieldWidth()/2))
         {
         turnLeft(90);
         ahead(getX() - getBattleFieldWidth()/2);
         turnRight(90);
         };
      else
         {
         turnRight(90);
         ahead(getBattleFieldWidth()/2 - getX());
         turnLeft(90);
            };
         }
      }      //this is line 76
   } }
} //line 78
//line 79



This is the one error I found, but I could be missing some because the scan() method brought back some pretty nasty nightmares. You forgot to name the variable event in your method parameter, but used it in your method body, without it existing yet. An obvious error in any good IDE, but Robocode is worse than notepad for editing, and worse than DOS for compiling. Your AI is spot on though, from what I tested. :wink:
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

User avatar
RockoTDF
Posts: 582
Joined: Thu Oct 18, 2007 6:08 am UTC
Location: Tucson, AZ, US
Contact:

Re: Robocode Java Compiler issues

Postby RockoTDF » Mon Nov 26, 2007 3:25 am UTC

Thanks a lot!

I know one of the central tenets of learning to program is figuring things out yourself, but I am completely lost as to this error:

Compiling...
/Users/brianroller/Desktop/download/robocode/robots/HAL10000.java:58: illegal start of type
if(getX() > getBattleFieldWidth()/2))
1 error
Compile Failed (1)

All other code is as above after your previous fix. This is really starting to get on my last nerve, none of this was happening before!
Just because it is not physics doesn't mean it is not science.
http://www.iomalfunction.blogspot.com <---- A collection of humorous one liners and science jokes.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Robocode Java Compiler issues

Postby Rysto » Mon Nov 26, 2007 3:32 am UTC

So the end of your file looks like this:

Code: Select all

    public void reset()
    {
        turnLeft(getHeading());
        ahead(getBattleFieldHeight()/2 - getY());

        if(getX() > getBattleFieldWidth()/2))
        {
            turnLeft(90);
            ahead(getX() - getBattleFieldWidth()/2);
            turnRight(90);
        }
        else
        {
            turnRight(90);
            ahead(getBattleFieldWidth()/2 - getX());
            turnLeft(90);
        }        //this is line 76
    }
}

?

User avatar
RockoTDF
Posts: 582
Joined: Thu Oct 18, 2007 6:08 am UTC
Location: Tucson, AZ, US
Contact:

Re: Robocode Java Compiler issues

Postby RockoTDF » Mon Nov 26, 2007 4:11 am UTC

actually it is this:

Code: Select all

public void reset()
         {
         turnLeft(getHeading());
         ahead(getBattleFieldHeight()/2 - getY());
     
      if(getX() > getBattleFieldWidth()/2))
         {
         turnLeft(90);
         ahead(getX() - getBattleFieldWidth()/2);
         turnRight(90);
         };
      else
         {
         turnRight(90);
         ahead(getBattleFieldWidth()/2 - getX());
         turnLeft(90);
            };
         }   
          
}
Just because it is not physics doesn't mean it is not science.
http://www.iomalfunction.blogspot.com <---- A collection of humorous one liners and science jokes.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Robocode Java Compiler issues

Postby Rysto » Mon Nov 26, 2007 4:17 am UTC

Ok, several things:

First, you should never have a semi-colon directly after a brace in Java. Second, each closing brace must match a previous opening brace. Your reset method should look like the last one that I posted. Stop adding braces and semicolons at random.

User avatar
RockoTDF
Posts: 582
Joined: Thu Oct 18, 2007 6:08 am UTC
Location: Tucson, AZ, US
Contact:

Re: Robocode Java Compiler issues

Postby RockoTDF » Mon Nov 26, 2007 4:42 am UTC

Rysto wrote:Ok, several things:

First, you should never have a semi-colon directly after a brace in Java. Second, each closing brace must match a previous opening brace. Your reset method should look like the last one that I posted. Stop adding braces and semicolons at random.


Hmmm.....the second thing was a mistake on my part, but the first had never been pointed out to me.

I compiled it in the command line instead and it put the ^ underneath a ")" that was in some of the code I was given. The ^s have been off in this POS robocode program the whole time! No wonder nothing was working right!

Thank you all very much!
Just because it is not physics doesn't mean it is not science.
http://www.iomalfunction.blogspot.com <---- A collection of humorous one liners and science jokes.

Karrion
Posts: 92
Joined: Fri Jun 22, 2007 12:14 am UTC
Location: Melbourne, AU

Re: Robocode Java Compiler issues

Postby Karrion » Mon Nov 26, 2007 5:24 am UTC

Rysto wrote:First, you should never have a semi-colon directly after a brace in Java.


That's not entirely true... array initialisers and anonymous class instantiation can both involve a semicolon directly after a brace.

User avatar
Pathway
Leon Sumbitches...?
Posts: 647
Joined: Sun Oct 15, 2006 5:59 pm UTC

Re: Robocode Java Compiler issues

Postby Pathway » Mon Nov 26, 2007 8:24 am UTC

Code is easier to debug if you indent everything inside a brace. Do this recursively for nested braces. Take a look at this code from Wikipedia:

Code: Select all

// OddEven.java
import javax.swing.JOptionPane;
 
public class OddEven {
    private int input;
 
    public OddEven() {
    }
 
    public void showDialog() {
        input = Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number"));
    }
 
    public void calculate() {
        if (input % 2 == 0)
            System.out.println("Even");
        else
            System.out.println("Odd");
    }
 
    public static void main(String[] args) {
        OddEven number = new OddEven();
        number.showDialog();
        number.calculate();
    }
}


Notice how within all the method definitions, every line is indented from the method header? That is a good thing. It provides a quick visual shortcut to see what goes where, helping to organize your code. People who look at your code will like this. For example, the people who grade it.

It's also easier to see mistakes like this.

For that matter, lines that execute consecutively shouldn't be indented differently. It obscures the fact that they're in the same boat, conceptually speaking.

Code: Select all

   public void onHitWall(HitWallEvent event){
          turnRight(180);
         reset();
         }
SargeZT wrote:Oh dear no, I love penguins. They're my favorite animal ever besides cows.

The reason I would kill penguins would be, no one ever, ever fucking kills penguins.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests