Solved

# Transport Robot - Check Ahead - Move

Posted on 2006-05-15
259 Views
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);}

{
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?
0
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.
0

## Featured Post

### Suggested Solutions

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…