对于类,我应该扩展bug actor来创建一个在网格上生成M的bug。这就是我到目前为止所知道的,但是bug并没有在指定的方向上运行。相反,它会形成一个正方形。对我做错了什么有什么帮助吗?
import info.gridworld.actor.Bug;
import info.gridworld.grid.Location;
public class MBug extends Bug{
private int lineLength;
private int steps;
private int line;
public MBug(int length)
{
setDirection(Location.NORTH);
steps = 0;
line = 1;
lineLength = length;
}
public void act(){
if (line <= 4 && steps < lineLength){
if (canMove()){
move();
steps++;
}
}else if (line == 2){
setDirection(Location.SOUTHEAST);
steps = 0;
line++;
}else if (line == 3){
setDirection(Location.NORTHEAST);
steps = 0;
line++;
}else if (line == 4){
setDirection(Location.SOUTH);
steps = 0;
line++;
}
}}
发布于 2014-02-01 06:18:19
太棒了!

这是代码。如果有什么不明白的,请告诉我。
import info.gridworld.actor.Bug;
import info.gridworld.grid.Location;
public class MBug extends Bug{
private int lineLength;
private int steps;
/* strokeNum is basically a code to tell the bug which stroke it is on.
* In this case, an 'M' has four strokes:
* up, diagonal down, diagonal up, and then down.
* This method can be used to create any letter,
* but round strokes (C's, R's, etc.) take so many individual strokes that it's almost impossible.
*/
private int strokeNum;
public MBug(int length){
lineLength=length;
steps = 0;
strokeNum=0;
}
public void act(){
if(strokeNum==0){
setDirection(Location.NORTH);
}else if(strokeNum==1){
setDirection(Location.SOUTHEAST);
//This is to shorten the length of this stroke.
steps++;
}else if(strokeNum==2){
setDirection(Location.NORTHEAST);
//This is to shorten the length of this stroke.
steps++;
}else if(strokeNum==3){
setDirection(Location.SOUTH);
}
if(canMove() && strokeNum<4){
move();
steps++;
if(steps>=lineLength){
steps=0;
strokeNum++;
}
}
}
}https://stackoverflow.com/questions/19466492
复制相似问题