0

我在做一个吃豆人游戏。以下代码用于鬼魂移动,它可以正常工作。但我必须包括另一张支票。问题是我总是破坏逻辑。

当前代码:

public void moveGhost(Tiles target) {
    if(specialIntersections()){
        direction = direction; //keeps going in the same direction
    }
    else{
        int oppDir;
        if(direction == UP){
            oppDir = DOWN;
    }
    else if(direction == DOWN){
        oppDir = UP;
    }
    else if(direction == LEFT){
        oppDir = RIGHT;
    }
    else{
        oppDir=LEFT;
    }

    double minDist = 10000.0;
    Tiles potentialNext;

    for(int i=0; i<4; i++){
        if(i!=oppDir){
            potentialNext = maze.nextTile(getCurrentPos(), i);
            if(!(potentialNext.wall()) && check(potentialNext)){
                if(calculateDistance(target, potentialNext) < minDist){
                   minDist = calculateDistance(target, potentialNext);
                   futureDirection = i;
                }
            }
        }
    }
}


changeDirection();
timer++;
increment();

x += xinc;
y += yinc;
tunnel();

}

我需要包括的另一项检查:

  //if the door is a wall (closed) the object cannot go through it
  if(DoorIsWall()){
if(!(potentialNext.wall()) && !(potentialNext.door())  && check(potentialNext)){ 
4

1 回答 1

2

当我的条件开始变得不守规矩时,我通常会编写一个新方法:

if (isTileValid(potentialNext)) {
    // do stuff
}


private boolean isTileValid(TileObject someTile) {
    if (someTile.wall()) {
        return false;
    }
    if (someTile.door()) {
        return false;
    }
    if (! check(someTile)) {
        return false;
    }
    return true;
}
于 2012-11-28T20:51:06.570 回答