我写了一个迷你 pacman 代码,使用 greenfoot。我的问题是,一旦收集完所有的叶子,Clara 就停在第一棵树前,但是,我继续收到错误消息,说她无法移动树。下面是我的代码:
/**
* 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();
}
}