Transport Robot - Check Ahead - Move

Posted on 2006-05-15
Last Modified: 2016-05-20
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
         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)
     else if (dir==1)
     else if (dir==2)
     if (obstacle_ahead(room)) // if there is an obstacle ahead
                  setX(keepx); // put robot back to position before goTo method
            else // put the robot in the correct, free y position
            } // 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;
          if ( (x != keepx) && (y !=keepy) )
                return false;
                return true;
      } // end class myRobot

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.

Question by:Ryan Bayne
    1 Comment
    LVL 92

    Accepted Solution

    isObstacleInWay() needs to check the contents of the room at x,y

    goTo() needs to pass x,y coords to isObstacleInWay()

    By the descripotion of goTo() it does not need to move x,y in any direction as it is being passed the cooords to move to.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Convert ArrayList of custom objects to JSON 4 44
    scoresClump  challenge 31 87
    sumHeights2  challenge 7 51
    countHi challenge 25 62
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now