0

我要创建一个模仿机器人功能的代码。转动和移动等等。我觉得好像我以所有错误的方式接近这个......当我写这篇文章时,我以为我理解了它的要点,如果构造函数中的方向是这个,那么如果它转向新的方向就是这个。我对此进行了测试,当然我最终得到了一些真正不正确的结果。我绝对确定我实际上并没有为我的对象使用任何这些函数。我可以获得有关如何使用此类代码的提示吗?

import java.awt.Point;
public class Robot
{
private int x;
private int y;
private int d;
private int p;
public static final int NORTH = 0;
public static final int SOUTH = 1;
public static final int EAST = 2;
public static final int WEST = 3;

/**
 * Constructor for objects of class Robot
 * @param theX the x coordinate
 * @param theY the y coordinate
 * @param theDirection the direction the robot is facing
 */
public Robot(int theX, int theY, int theDirection)
{
    x = theX;
    y = theY;
    d = theDirection;
}

public void turnLeft()
{
    if(d == NORTH) {
        d = WEST;
    }
    if(d == WEST) {
        d = SOUTH;
    }
    if(d == SOUTH) {
        d = EAST;
    }
    if(d == EAST) {
        d = NORTH;
    }
}



public String getDirection()
{
    if(d == NORTH) {
        return "N";
    }
    if(d == SOUTH) {
        return "S";
    }
    if(d == WEST) {
        return "W";
    }
    if(d == EAST) {
        return "E";
    }   
    return "";
}
}

测试

Robot rob = new Robot(20, 20, Robot.SOUTH);
rob.turnLeft;
System.out.println(rob.getDirection);

当我认为它实际上应该返回 E 时,它返回 S。

4

6 回答 6

1

枚举

public enum Direction {
    private String name;
    private String indicator;

    public Direction(String name, String indicator) {
        this.name = name;
        this.indicator= indicator;
    }

    // getters

    NORTH("North", "N"),
    EAST("East", "E"),
    SOUTH("South", "S"),
    WEST("West", "W");
}

接下来,您可以轻松地做到这一点:

turnLeft() {
   switch (d) {
        case Direction.NORTH: return Direction.WEST;
        case Direction.WEST:  return Direction.SOUTH;
        case Direction.SOUTH: return Direction.EAST;
        case Direction.EAST:  return Direction.NORTH;
    }
}

getDirection() {
    return d.getIndicator();
}

这样你就可以去掉四个静态int ( NORTH,,,, ) WEST,变成. 我真的建议为此使用。只是为了类型安全。EASTSOUTHint dDirection denum

于 2013-04-29T08:12:19.247 回答
1

你的turnLeft方法不太对。
这是使用的代码if

public void turnLeft() {
  if (d == NORTH) {
      d = WEST;
  } else if (d == WEST) {
      d = SOUTH;
  } else if (d == SOUTH) {
      d = EAST;
  } else if (d == EAST) {
      d = NORTH;
  }
}

这是使用的代码switch..case

public void turnLeft() {

  switch (d) {
    case NORTH: d = WEST; break;
    case WEST: d = SOUTH; break;
    case SOUTH: d = EAST; break;
    case EAST: d = NORTH; break;
  }
}
于 2013-04-29T08:06:18.853 回答
1

如果你需要其他的。当您向左转时,您为 d 分配了一个新值,该值与以下 if 语句的条件相匹配。

于 2013-04-29T08:04:05.987 回答
0

1)你真的应该清理你的 p 和 d 东西;)我建议称 d 为“方向”

2nd)您应该按顺时针顺序重新排列方向的 int 常量。所以你可以将你的turnLeft()方法减少到一行代码。你可以这样订购它们:

public static final int NORTH = 0;
public static final int EAST = 1;
public static final int SOUTH = 2;
public static final int WEST = 3;

3)进行测试,你应该使用 JUnit:

@Test
public void turnLeft() {
    Robot rob = new Robot(20, 20, Robot.SOUTH);
    rob.turnLeft;
    assertEquals("E", rob.getDirection);
    rob.turnLeft;
    assertEquals("N", rob.getDirection);
    rob.turnLeft;
    assertEquals("W", rob.getDirection);
    rob.turnLeft;
    assertEquals("S", rob.getDirection);
}
于 2013-04-29T08:06:38.497 回答
0

p您使用的var 是什么?您当前的方向存储在d

...
d = theDirection;
...

你应该试试这个:

public void turnLeft()
{
    if(d == NORTH) {
        d = WEST;
    }
    if(d == WEST) {
        d = SOUTH;
    }
    if(d == SOUTH) {
        d = EAST;
    }
    if(d == EAST) {
        d = NORTH;
    }
}

编辑:只是为了澄清。

你写了类似的东西

if(p == SOUTH) {
        d = EAST;
    }

因此,您正在检查pis SOUTH 和 not d

你没有p在任何地方使用,所以它永远不会是南方,这就是为什么你的turnLeft()方法绝对什么都不做!

于 2013-04-29T08:02:04.150 回答
0

万一有人需要,这里有一个解决方案,可以转向任何一种方式:

public enum Direction {
    UP,
    LEFT,
    DOWN,
    RIGHT;

    Direction turnLeft() {
        int turnLeft = 1;
        return getDirectionByOrdinalMovingClockwise(turnLeft);
    }

    Direction turnRight() {
        int turnRight = -1;
        return getDirectionByOrdinalMovingClockwise(turnRight);
    }

    Direction turnAround() {
        int turnAround = 2;
        return getDirectionByOrdinalMovingClockwise(turnAround);
    }

    private Direction getDirectionByOrdinalMovingClockwise(int moveBy) {
        var thisDirectionOrdinal = ordinal();
        int newDirectionOrdinal = (thisDirectionOrdinal + moveBy) % enumElementsCount();
        if (newDirectionOrdinal < 0) {
            newDirectionOrdinal = enumElementsCount() + newDirectionOrdinal;
        }
        return values()[newDirectionOrdinal];
    }

    private int enumElementsCount() {
        return values().length;
    }

}
于 2018-04-05T14:12:38.650 回答