Results 1 to 4 of 4
  1. #1
    marcosol is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default 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.
    Java 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!
    Last edited by Norm; 03-10-2012 at 02:22 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: efficiency problem

    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.

  3. #3
    marcosol is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: efficiency problem

    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.

Similar Threads

  1. efficiency problem with homework
    By marcosol in forum New To Java
    Replies: 15
    Last Post: 09-19-2012, 07:56 PM
  2. Efficiency of Searching
    By fam2315 in forum New To Java
    Replies: 12
    Last Post: 08-03-2011, 09:44 AM
  3. Efficiency of code...
    By Inventor22 in forum New To Java
    Replies: 6
    Last Post: 09-26-2010, 11:24 AM
  4. URLConnection Efficiency
    By Lil_Aziz1 in forum New To Java
    Replies: 22
    Last Post: 08-19-2010, 07:27 PM
  5. method efficiency
    By TheWave in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •