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

发布于 2024-12-29 20:12:54 字数 1231 浏览 0 评论 0原文

我正在使用 LRV(最近最少访问)算法制作一个程序。基本上,我设计了机器人遍历网格(这是一个二维字符数组)的算法。机器人在遍历网格时会检查每个单元格是否为EMPTY(由“O”定义)、OCCUPIED(由“S”定义)或BLOCKED代码>(由“X”定义)。这些单元格只能被称为传感器的对象占据(它有自己的类)。无法遍历 BLOCKED 单元格。每次机器人必须移动时,它都会收到来自传感器的方向。因此,一开始,机器人将被放置在网格上,它会放下一个传感器并从中获取方向,或者从预先存在的传感器中获取方向。

现在我已经解释了我的程序,我的具体问题是, 我有一个 Sensor 类,它有一个返回 INT 的 getVisitingDirection 方法。 我每个方向都有一个计数器(北、南、东、西,类型为 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)
{}

}

现在我陷入困境的是 getVisitingDirection,我尝试使用 if 语句来检查网格的左上角(坐标 0,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 '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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2025-01-05 20:12:54

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

// 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;
}

请注意,您必须创建 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

I've put a function in pseudo-code that should set you in the right path.

// 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;
}

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.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文