我编写此代码是为了使用比课程预期更多的 Java 元素。但我很难让它适用于所有 4 个方向。getter 和 setter 嵌入到使 Karel 移动的方法中。如果我能得到任何帮助让这条鳕鱼工作,那就太好了。
import stanford.karel.*;
public class MidpointFindingKarel extends SuperKarel {
public void run(){
while(facingEast()){
moveEast();
}
while(facingNorth()){
moveNorth();
}
while(facingWest()){
moveWest(moveEast());
}
while(facingSouth()){
moveSouth(moveNorth());
}
}
private int moveEast(){
int width = 0;
while(frontIsClear()){
width++;
move();
}
turnLeft();
width /= 2;
return width;
}
private int moveNorth(){
int height = 0;
while(frontIsClear()){
height++;
move();
}
turnLeft();
height /= 2;
return height;
}
private void moveWest(int _width){
for(int _w = 0; _w < _width; _w++){
move();
}
turnLeft();
}
private void moveSouth(int _height){
for(int _h = 0; _h < _height; _h++){
move();
}
turnLeft();
}
}