迷你pacman代码java

时间:2014-04-03 03:05:16

标签: java pacman greenfoot

我用greenfoot编写了一个迷你pacman代码。我的问题是,一旦所有的叶子被收集,克拉拉就会停在第一棵树的前面,然而,我继续得到一个错误,说她不能移动树。以下是我的代码:

/**
 * MyClara is a subclass of Clara. Therefore, it inherits all methods of Clara: <p>
 * 
 * PERMITTED COMMANDS
 * Actions:     move(), turnLeft(), turnRight(), putLeaf(), removeLeaf(), stop()
 * Sensors:     onLeaf(), treeFront() 
 * JAVA:        if, else, !, &&, ||, for, while
 */
public class MyClara extends Clara 
{ 

    public void run()
    {

        if (!treeFront())
        {
            eatLeaf();
            moveThroughMaze();

        }

    }

    void stopInfrontOfFirstTree()
    {
        if (treeFront())

            stop();
    }

    void eatLeaf()
    {
        while(onLeaf())
        {
            removeLeaf();
            move();
        }

    }

    void turnAround()
    {
        turnLeft();
        turnLeft();
        move();
        turnLeft();
        turnLeft();        

    }

    void checkLeft()
    {
        turnLeft();
        move();
        if (onLeaf())
            eatLeaf();
        else
            turnAround();

    }

    void checkRight()
    {
        turnRight();
        move();
        if (onLeaf())
            eatLeaf();
        else
            turnAround();

    }

    void moveThroughMaze()
    {
        eatLeaf();
        turnAround();
        checkLeft();
        turnAround();
        checkLeft();
        turnAround();
        checkRight();
        turnAround();
        checkRight();
        turnAround();
        checkRight();
        turnAround();
        checkLeft();
        turnAround();
        checkLeft();
        turnAround();
        checkLeft();

    }
}

0 个答案:

没有答案