0

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 '-'), OCCUPIED ( defined by 'O' ) 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 my GridMap class

public class GridMap {
  private int height;
  private int width;
  private char[][] grid;


  public GridMap(int x, int y) { 
    height=x;
    width=y;  
    grid = new char [x][y];
  }
  public int getHeight(){
    return height;
  }
  public int getWidth(){
    return width;
  }
  public String toString(){
    String s1 = 
    return s1;
  }
  public char getElementAt(int x, int y){
  }
  public void setElementAt(int x, int y){
  }
  public boolean isCellBlocked(int x, int y){
  }
  public double getCoverageIndex(){
    return COVERAGE_INDEX;
  }
}

What I want to know is how can I represent my 2D char array as a string of -, O's and X's. I tried to be as detailed as possible, if anyone has any questions I'd be willing to answer asap. Any help will be highly appreciated. Thanks

Varun

4 Answers 4

4

Is this what you mean?

public String toString()
{
    StringBuilder builder = new StringBuilder();
    for(int i = 0; i < getHeight(); i++)
    {
        for(int j = 0; j < getWidth(); j++)
        {
            builder.append(grid[i][j]);
        }
    }    
    return builder.toString();
}
Sign up to request clarification or add additional context in comments.

Comments

0
public String toString() {
  StringBuilder sb = new StringBuilder();
  for (int i=0; i< this.grid.length; i++) {
    for (int j=0; j < this.grid[i].length; j++) {
      sb.append(grid[i][j]);
    }
    sb.append("\n");
  }
  return sb.toString();
}

Comments

0

The natural representation would matrix wise:

"-----X----O--X\n" + // i=0, j=0,...width-1
"---X----------\n" + // i=1, j=0,...width-1
...
"------XX---O--\n"   // i=height-1

Unfortunately you do not use (i, j) but (x, y). The other answers are okay.

2 Comments

Ok I don't understand what you said, however the string representation of the matrix you showed me looks similar to a result I need.
The other answers will give you such an result (@yurib). However their (i, j) corresponds with your (y, x) - other order.
0

Do something like this

public String toString()
{
    String result = "";
    for(int i = 0; x < grid.length; i++)
    {
        for(int y = 0; y < grid[x].length; y++)
        {
            //you may need to convert each char to string here first
            result += grid[x][y];
        }
    }
    return result;
}

4 Comments

I see this, however I've got to make them either -, O or X's.
when you add chars to the array, don't you add them as X's and O's? For example, grid[0][0] = 'X';
@theDazzler's solution is okay too, += adds the char to the resulting string. Allow me to +1 him; at least he uses your x,y convention, the String += is not so good and \n could be added too.
Better to use StringBuilder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.