改变一个点的方向

时间:2013-04-29 07:52:54

标签: java direction

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

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.

7 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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;
  }
}

答案 2 :(得分: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(NORTHWESTEASTSOUTH)并将int d更改为{{1 }}。我真的建议使用Direction d。只是为了安全。

答案 3 :(得分: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;
    }

因此,您要检查p是否为南方而不是d

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

答案 4 :(得分:0)

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

2)你应该按顺时针顺序重新排列方向的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);
}

答案 5 :(得分:0)

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

因为如果d == NORTH,则d将成为WEST

在下一个if中,d确实是d==WEST,并且将成为SOUTH

在一天结束时,d将再次NORTH

答案 6 :(得分: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;
    }

}