class Ant {
// An ant is represented by the coordinates of its location,
// and the direction it is facing.
Integer x;
Integer y;
Direction direction;
// Represent direction as an enumeration
enum Direction { X, Y, NegX, NegY };
//Represent turn choice as an enumeration
enum Turn { Pos, Neg };
// Ant constructor
Ant(Integer x, Integer y, Direction direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
// Change this ant's direction
private void turn(Turn turn) {
}
// Mave this ant forward one step in the direction it is facing.
// Sadly, Java's "%" is called "mod" but it is remainder.
// That may explain the "+ width" and "+ height".
private void forward(Integer width, Integer height) {
switch (direction) {
case X : x = (x + 1 + width) % width; break;
case Y : y = (y + 1 + height) % height; break;
case NegX : x = (x - 1 + width) % width; break;
case NegY : y = (y - 1 + height) % height; break;
}
}
// This ant turns then steps forward.
void advance(State state, Integer width, Integer height) {
Ant State.On = new Ant(Direction.X,Y.Turn.Pos)
Ant State.Off = new Ant(Direction.NegX,NegY;Turn.Neg}
// Your code here:
// 1) Turn this ant based on state parameter
// 2) Step forward
}
}
void turn(转弯)此mutator方法对应于Assignment中的转弯功能 它的作用是通过将它转为正或负来改变蚂蚁的方向 方向。 turn函数的参数是Turn类型的值,即它是Turn.Pos或Turn.Neg。
void advance(状态,整数宽度,整数高度)advance advance mutator 方法根据网格的那个点的状态转动蚂蚁。然后它使蚂蚁向这个方向迈进了一步。如果状态是State.On,则蚂蚁转向Turn.Pos方向。如果状态是State.Off,蚂蚁转向Turn.Neg方向。您已经有方法转向和转发分别执行这两个操作,因此将根据这两个方法定义advance。
好的,我已经编辑了整个事情..现在有了所有完整的信息..我更喜欢解释,我会尝试自己编写完整的信息。感谢。