# Minimize the line count

Robo Home | Changes | Preferences | AllPages

Showing revision 4
Difference (from revision 4 to revision 4) (minor diff, author diff)
(The revisions are identical or unavailable.)
Minimizing the line count (For Sonnet-Bots

I thought I would add some tips for those people trying to write SonnetBots for the Robocode Little League. This will allow your robot to get pretty big without to many semicolons. Since I like text that lines up I'm going to use the pre tag here.

```   Rule #1: Learn to comment well. I say this because It's going to be your comments that tell you
where everything is in the code and what everything is going to do. Since you are going to end
up having a lot of brackets I find it particularly useful to indent each new set of semi-colons unless
its a simple easy to read condition.

--- before ---
((x>0) && (x<width) && (y>0) && (y<height)) ? "yes" : "no"

--- after ---
(   (   x        // (double) - x coordinate
>   0        // Test if right of west wall (x=0)
)
&&  (   x        // (double) - x coordinate
<   width    // Test if left of east wall (x=width)
)
&&  (   y        // (double) - y coordinate
>   0        // Test if north of south wall (y=0)
)
&&  (   y        // (double) - y coordinate
<   height   // Test if couth of north wall (y=height)
)
)
?   "yes"        // The point is in the battlefield
:   "no"         // The point is not in the battle field

While this might make your code look more cryptic it make it far easier to understand the mechanics
of a condition that has many nested brackets. The same method can be applied to functions. It
helps when ALL THE BRACKETS AND OPERATOR ARE ALIGNED ON THE SAME INDENTATION DEPTH.
It also allows you to comment beside each argument so you know what they do.

Rule #2: Get comfortable with casting because you're going to be doing a lot of it.
Every variable you can imagine can be implemented using an object. Java supplies numerous
wrapper classes for the common primitives. int becomes Integer, double becomes Double.
If you use these wrapper classes ALL YOUR VARIABLES CAN BE WRITTEN ON ONE LINE.

--- before ---
int       i, j;
double    x, y;
HashTable h = new HashTable();

--- after ---
Object
i = new Integer(),
j = new Integer(),
x = new Double(),
y = new Double(),
h = new HashTable();

Now in order to access their actual values you will need to cast them into their wrapper values
and use the methods they contain.

--- before ---
if (i < j)
return i;
else  return j;

--- after ---
if (   ((Integer) i).intValue()
<   ((Integer) j).intValue()
)
{  return ((Integer) i).intValue();
} else
{  return ((Integer) j).intValue();
}

The above condition could have eliminated 2 semicolons by using the ? condition

--- better ---
return (   ((Integer) i).intValue() < ((Integer) j).intValue() )   // condition
?   ((Integer) i).intValue()                                // true return value
:   ((Integer) j).intValue();                               // false return value

Rule #3: Think of your program as one big condition. EVERY STATEMENT CAN BE WRITTEN AS A CONDITION THAT HAS
A BOOLEAN RETURN VALUE. This will allow you to eliminate almost ALL of your semi-colons right here.

--- before ---
A = B;

--- after ---
if ((A = B) != null)
{ // The assignment succeeded in this case.
} else
{ // The assignment failed in this case because the object was null
}

Rule #5: EACH FUNCTION YOU WRITE COSTS AT LEAST ONE SEMICOLON. Each time you write a function with a return
statement it costs one semi-colon. But you can use a function that has a return condition for free by
testing and assignment with it's return value in a condition. Writing a void function can be free because
you avoid the return statement, but you will need that semi-colon when you call it. Since NO FUNCTION
IS A NECESSITY, you can cut down on your line count by simply not writing them.

Unfortunatly the key methods in robocode that do what you want them to do are "void" methods
that do require a semi-colon. Since you have to use them, only call them once in the main method.
Determine what their arguments are before hand.

a) Amount to turn the radar.     (Double)
b) Amount to turn the gun.       (Double)
c) Amount to turn the robot.     (Double)
d) Amount to move back and forth (Double)
e) Fired power we wish to shoot. (Double)

Because the run method has the lowest priority it will be the last function executed. In the
end this is all we need to know so lets try to ensure we only leave one line for each required
method. The last one can be put into a condition if you use the alternate setFireBullet()

--- code ---
// I used 11 for the or symbol because it causes a line break in html. (grr..)
while( setFireBullet( power ) != null 11 true)