0

对于课程,我应该扩展错误参与者以制作一个在网格上生成 M 的错误。这是我到目前为止所拥有的,但错误并没有朝指定的方向转动。相反,它是一个正方形。对我做错了什么有帮助吗?

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

}

4

1 回答 1

0

达达!

错误

这是代码。如果您有任何不明白的地方,请告诉我。

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++;
            }
        }
    }
}
于 2014-01-31T22:18:18.897 回答