# efficiency problem

• 03-09-2012, 01:31 PM
marcosol
efficiency problem
I'm making a class of robots. The have a position(x,y), orientation(0=up,1=right,2=down,3=left) and an amount of energy.
The y-axis is pointing downwards and the x-axis is pointing to the right.
They must be able to move next to each other with as parameter another robot (not on the same position).
Important with this is that the moving and turning requires energy (500,100 respectivly) and the goal is that you use the minimal amount of energy. Here is the code of the methods that matter.
Code:

```public void moveNextTo(IRobot robot) {                 while((getY()==(robot.getY()+1)) || (getY()==(robot.getY()-1)))                                 && (getX()==(robot.getX()+1)) || (getX()==(robot.getY()-1)))                                 this.moveOneStepTo(robot);                                 robot.moveOneStepTo(this);                        } private void moveOneStepTo(IRobot robot){                 if((getX()==robot.getX())&&(getY()==robot.getY()))                         move();                 if(getX()<robot.getX()-1){                         turnToCorrectOrientation(1);                         move();                 }                 if(getX()>robot.getX()+1){                         turnToCorrectOrientation(3);                         move();                 }                 if(getY()<robot.getY()-1){                         turnToCorrectOrientation(0);                         move();                 }                 if(getY()>robot.getY()+1){                         turnToCorrectOrientation(2);                         move();                 } private void turnToCorrectOrientation(int orientation){                                 if(getOrientation()>orientation)                 {                         while (orientation !=getOrientaton())                                 turnCounterClockwise()                 }                 if(getOrientation()<orientation)                 {                         while (orientation !=getOrientaton())                         turnClockwise                 }         }```
The problem occurs, I believe, in the last method. Because if the robot is looking up and he should face left my solution requires 3 turnes. Same with the other way around. Any Idees?

Futhermore I had a question regarding assertions. The methods move and turn(counter)clockwise use an assertion to make sure the robot has enough energy. But if the assertion fails what happens next? Cause if the method is just cancelled from there i'm bound to get in stuck an internal loop.

Thanks!
• 03-10-2012, 02:23 AM
Norm
Re: efficiency problem
Quote:

if the robot is looking up and he should face left my solution requires 3 turnes.
Find and use the shortest way to turn vs always turning the same way.
• 03-10-2012, 12:09 PM
marcosol
Re: efficiency problem
I think this might do it.

private void turnToNearestOrientation(int orientation){
if(Math.abs((getOrietation()-orientation)==3)){
if(orientation==0){
turnCounterClockwise();
}
if(orientation==3){
turnClockwise();
}
}
if(getOrientation()>orientation){
while(getOrientation!=orientation)
{
turnCounterClockwise();
}
}
if(getOrientation()<orientation){
while(getOrientation!=orientation)
{
turnClockwise();
}
}
}
Can someone tell me what happens with my program if an assert statement fails?
Wether or not the method who calls the submethod(with the failed assertion) stops aswell.
• 03-10-2012, 01:41 PM
Norm
Re: efficiency problem
Quote:

happens with my program if an assert statement fails?
Can you execute the code and give it values that cause the assert to fail? Then you will see what it does.