如何从传感器类中的方法获取机器人的方向?

Mja*_*ll2 5 java grid robot sensor

我正在使用LRV(最近访问过的最少)算法制作一个程序.基本上,我设计了一个机器人遍历网格(这是一个2D字符数组)的算法.机器人在穿过网格时检查每个单元是EMPTY(由'O' OCCUPIED定义),(由'S' BLOCKED定义)还是(由'X'定义).单元格只能被称为Sensor的对象占用(这有自己的类).BLOCKED细胞无法遍历.每次机器人必须移动时,它接收来自传感器的方向.因此,在开始时,机器人将被放置在网格上并且它将丢弃传感器并从其获得方向,或者从预先存在的传感器获得方向.

现在我已经解释了我的程序,我的具体问题是,我有一个类Sensor,它有一个getVisitingDirection返回INT 的方法.我有一个每个方向的计数器(INT类型的北,南,东和西)这是班级.

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)
{}

}
Run Code Online (Sandbox Code Playgroud)

现在我被困在getVisitingDirection,我试图使if语句检查网格的左上边缘(坐标0,0),是的,这就是它.我希望该方法为机器人指定方向,然后增加该方向的计数器.甚至在这里获得概念真的很困难.任何帮助将非常感谢!谢谢Varun

Fra*_*kie 3

我已经在伪代码中放置了一个函数,它应该可以让您走上正确的道路。

// lets assume binary code where 0000 represents top, right, bottom, left 
// (0011 would mean can go bottom or left)
public int getVisitingDirection()
{
    String tmpBinary = "b"; // to initialize the field

    // check if can go up
    tmpBinary += (cCollums>0&&checkIfUpIsBlocked()) "1" : "0";
    // TODO check if can go right (compare with size + 1)
    // check if can go bottom (compare with size +1 )
    // check if can go left (check if > 0)

    // when this runs tmpBinary will be in the form of a binary representation
    // this will be passed to the robot that can then chooses where to go
    // 1111 would mean that all squares are clear to go
    // 1101 would mean top, right and left
    // etc...

}

private boolean checkIfUpIsBlocked()
{
    // TODO must check if the cell is blocked
    return true;
}
Run Code Online (Sandbox Code Playgroud)

请注意,您必须创建 checkIfUpIsBlocked + 方法。

上面的接缝非常好。
您可能希望通过枚举更改 int 字段,因为它们更易于阅读并且不易出现人为错误。

如何用 int 数字返回方向?
您可以使用二进制逻辑并返回单个 int 来表示多个方向。

0000 (int 0)  => no possible direction
0001 (int 1)  => left direction
0010 (int 2)  => bottom direction
0011 (int 3)  => left and bottom
0100 (int 4)  => right direction
(...)
1111 (int 15) => all directions possible
Run Code Online (Sandbox Code Playgroud)