I am making a program using the LRV(Least recently visited) Algorithm. Basically, I design the algorithm for a robot to traverse through a grid (which is a 2D char array). The robot whilst traversing the grid checks whether each cell is either EMPTY (defined by ‘O’), OCCUPIED ( defined by ‘S’ ) or BLOCKED (defined by ‘X’). The cells can only be occupied by an object known as Sensor (this has its own class). BLOCKED cells cannot be traversed on. Each time the robot must move, it receives a direction from the sensor. So in the beginning the robot would be placed on the grid and it would drop a sensor and get a direction from it, or get a direction from a pre-existing sensor.
Now that I’ve explained my program, my specific question is,
I have a class Sensor that has a getVisitingDirection method that returns INT.
I have a counter for each direction (North, South, East and West of type INT)
Here is the class.
package ITI1121A;
public class Sensor {
private int cColumns;
private int cRows;
private int North;
private int South;
private int West;
private int East;
public Sensor(int sX, int sY) {
cColumns = sX;
cRows = sY;
South= -1;
North = -1;
West = -1;
East = -1;
}
/* ADD YOUR CODE HERE */
public int getX ()
{return cColumns;}
public int getY ()
{return cRows;}
public int getVisitingDirection(GridMap g1)
boolean temp;
{
if(cRows==0){
//top row
if(cColumns==0){
temp=g1.isCellBlocked(cColumns+1,cRows);
if (temp=false){
return West++;
}
}
}
}
public void increaseCounter(int direction)
{}
}
Now where I am stuck is at getVisitingDirection, I’ve tried to make if statements to check the top left edge of the grid ( coordinates 0,0) and yeah that’s about it.
I want the method to give a direction to the robot and then increase the counter of that direction.
Having real difficulty even getting the concept here.
Any help will be highly appreciated!
Thanks
Varun
I’ve put a function in pseudo-code that should set you in the right path.
Do notice that you have to create the checkIfUpIsBlocked + methods.
On top of that seams pretty good.
You may want to change the int fields by enums as they are easier to read and less prone to human errors.
How to return directions with an int number?
You can use the binary logic and return a single int to represent multiple directions.