Link to home
Start Free TrialLog in
Avatar of Ryan Bayne
Ryan BayneFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Transport Robot - Check Ahead - Move

OK 0 = Up, 1 = Right, 2 = Down and 3 = Left

I've done most of the code for my Robot to move in teleport. Atleast I think its most of it but I need help. I need pointing in the right direction and I'm hoping that an expert may think along the same lines as my tutor because I cant get my tutors help until Wednesday.

Heres some of the code I wont post all of it because I dont want it copied but I think this will be all you need.

import SE111aClasses.*;
import java.util.Random;
public class MyRobot extends Robot {

      GUI gui = new GUI();
   
      // method to move robby from current position to position (x,y) if (x,y) is empty
      // if the tile is full (i.e. contains an obstacle) robby should not move
      // message displayed to show if robby is moving to square or unable to move
      public void goTo(Room room, int x, int y)
      {
            int keepx=0; // variables to hold the robot's current position
        int keepy=0;
        int dir=0;

         keepx=get_xpos(); // use the robot methods to get x and y positions
         keepy=get_ypos();
         dir = get_dir();
            
        // setX(x);      // use the robot methods to set the position of the robot
         // setY(y-1);       // to (x,y-1), one y position lower to enable look ahead
         
     if (dir==0)
     {setX(x);
      setY(y-1);}
     
     else if (dir==1)
     {setX(x-1);
     setY(y);}
     
     else if (dir==2)
     {setX(x);
     setY(y+1);}
     
     else
     {setX(x+1);
      setY(y);}
     
     if (obstacle_ahead(room)) // if there is an obstacle ahead
            {
                  setX(keepx); // put robot back to position before goTo method
            setY(keepy);
            }
            
            else // put the robot in the correct, free y position
            
            {
                  setX(x);
                  setY(y);
            } // this method ONLY works for robby when he is facing up
      }

      public void teleport(Room room) //While obstacle ahead teleport to new location
      {
      int x=0;
      int y=0;
      
            do {
        Random rand = new Random();
        x = rand.nextInt(18) + 1;
        y = rand.nextInt(18) + 1;
       } while (isObstacleInWay(room,x,y));
    }
   
    public boolean isObstacleInWay(Room r, int x, int y)
    {
          int dir = get_dir();
          int keepx=0; // variables to hold the robot's current position
        int keepy=0;
          keepx=get_xpos();
          keepy=get_ypos();
          
          
          if ( (x != keepx) && (y !=keepy) )
          {
                return false;
          }
          
          else
          {
                return true;
          }
          
    }
   
      } // end class myRobot


OK
I'm meant to put an extra method in
    public boolean isObstacleInWay(Room r, int x, int y)
but I aint 100% sure why. Its boolean. I can only imagine it returns if or not there is an obsticle ahead of the robot and if so continue to generate random numbers with teleport. However the code already covers this area.

ANYONE GOT ANYIDEAS OR SEE FAULTS WITH THE CODE?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial