0

我一直致力于使用 Gridworld 在 Eclipse 中创建一个 Checkers 游戏。到目前为止,我只修改了红色部分。我的目标是能够 move() 件并让他们选择是跳跃还是跨步。显然,跳跃(即当棋子移动到相反颜色的棋子上,然后从网格中移除该棋子时)优先于跨步。我的问题是,每当我尝试移动前两列或后两列中的部分时,我都会收到 Illegal Out of Bounds 错误。谁能帮我解决这个问题?谢谢,麻烦您了。

import java.awt.Color;
import info.gridworld.actor.Actor;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Critter;
import info.gridworld.actor.Flower;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

public class RedPieces extends Bug {
boolean jumped;

public RedPieces() {
    setColor(Color.red);
    setDirection(180);
    jumped = false;
}

public boolean canMove() {
    Grid<Actor> gr = getGrid();
    if (gr == null)
        return false;
    Location loc1 = getLocation();
    if (getGrid().get(new Location(loc1.getRow() + 1, loc1.getCol() - 1)) == null || 
        getGrid().get(new Location(loc1.getRow() + 1, loc1.getCol() - 1)).getColor() == Color.black || 
        getGrid().get(new Location(loc1.getRow() + 1, loc1.getCol() + 1)) == null || 
        getGrid().get(new Location(loc1.getRow() + 1, loc1.getCol() + 1)).getColor() == Color.black) {
        return true;
    }
    return false;
}

public boolean jump2(Location loc){
    Grid<Actor> gr = getGrid();
    if (gr == null)
        return false;
    Location jump1 = new Location(loc.getRow() + 2, loc.getCol() - 2);
    Location jump2 = new Location(loc.getRow() + 2, loc.getCol() + 2);

    if( (gr.isValid(jump1)) && 
        (gr.get(jump1) == null) && 
        (gr.get(jump2) == null) && 
        (gr.get(new Location(loc.getRow() + 1, loc.getCol() -1)) instanceof BlackPieces) && 
        ((gr.get(new Location(loc.getRow() + 1, loc.getCol() + 1)) == null) || 
         (gr.get(new Location(loc.getRow() + 1, loc.getCol() + 1))) instanceof RedPieces))
    {
        gr.get(new Location(loc.getRow() + 1, loc.getCol() -1)).removeSelfFromGrid();
        moveTo(jump1);
        return true;
    }
    else if( (gr.isValid(jump2)) && 
             (gr.get(jump2) == null) && 
             (gr.get(jump1) == null) && 
             (gr.get(new Location(loc.getRow() + 1, loc.getCol() +1)) instanceof BlackPieces) && 
             ((gr.get(new Location(loc.getRow() +1, loc.getCol() - 1)) == null) || 
              (gr.get(new Location(loc.getRow() + 1, loc.getCol() -1)) instanceof RedPieces)))
    {
        gr.get(new Location(loc.getRow() + 1, loc.getCol() +1)).removeSelfFromGrid();
        moveTo(jump2);
        return true;
    }
    else if((gr.isValid(jump1) && gr.get(jump1) == null) && 
            (gr.isValid(jump2) && gr.get(jump2) != null))
    {
        if(gr.get(new Location(loc.getRow() + 1, loc.getCol() -1)) instanceof BlackPieces)
        {
            gr.get(new Location(loc.getRow() + 1, loc.getCol() -1)).removeSelfFromGrid();
            moveTo(jump1);
            return true;
        }
    }
    else if((gr.isValid(jump2) && gr.get(jump2) == null) && 
            (gr.isValid(jump1) && gr.get(jump1) != null))
    {
        if(gr.get(new Location(loc.getRow() + 1, loc.getCol() +1)) instanceof BlackPieces)
        {
            gr.get(new Location(loc.getRow() + 1, loc.getCol() +1)).removeSelfFromGrid();
            moveTo(jump2);
            return true;
        }
    }

    return false;
}

public void move() {
    Grid<Actor> gr = getGrid();
    if (gr == null)
        return;

    Location loc = getLocation();
    Location next1 = new Location(loc.getRow() + 1, loc.getCol() - 1);
    Location next2 = new Location(loc.getRow() + 1, loc.getCol() + 1);
    if (jump2(loc) == false) {
        if (gr.isValid(next2) && gr.get(next2) == null && 
            gr.isValid(next1) && gr.get(next1) != null)
        {
            moveTo(next2);
        }
        else if (gr.isValid(next1) && gr.get(next1) == null && 
                 gr.isValid(next1) && gr.get(next2) != null)
        {
            moveTo(next1);
        }
        else if (gr.isValid(next1) && gr.get(next1) == null && 
                 gr.isValid(next2) && gr.get(next2) == null)
        {
            moveTo(randomLoc(next1, next2));
        }
        else
            return;
    }
}

public static Location randomLoc(Location loc1, Location loc2) {
    double num = Math.random();
    if (num < 0.5)
        return loc1;
    else
        return loc2;
}

public void act() {
    if(canMove()) move();
}
}
4

1 回答 1

1

必须重新格式化您的代码以获得更好的可读性。通过这样做,我发现了一些潜在的错误。

  • 在该jump2()方法中,第二if条语句不检查 的有效性jump2。这可能是该语句中gr.get(jump2 == null)条件异常的原因。if

  • 相同的逻辑适用于后续else if语句。这一次,您不检查jump1.

  • 在第一个move()方法中else if,您只检查了两次的有效性,next1这似乎是一个错误。请在您问题的重新格式化代码中找到它。

总的来说,我认为您需要if尽可能简单(可读)修改条件。重新格式化也会有所帮助。

[添加] 两件小事。

  • 在该canMove()方法中,您gr仅使用 at 第一个if语句。您在第二个语句中使用getGrid()而不是。grif

  • 你能不能避免每次你在报表new Location中得到网格时都这样做?if您正在使用这些网格而不检查它们的有效性。你确定它们是有效的吗?您可以在方法的开头创建它们,检查它们的有效性,并在if语句中使用它们。它也可能有助于if语句的可读性。

[添加更多]jump2()方法简化示例

以下代码忽略列的另一侧(无效、空或红色部分)。我的假设是,当一侧有效跳过黑色棋子时,另一侧的条件无关紧要。

public boolean jump2(Location loc){
    Grid<Actor> gr = getGrid();
    if (gr == null)
        return false;

    Location jump1 = new Location(loc.getRow() + 2, loc.getCol() - 2);
    Location jump2 = new Location(loc.getRow() + 2, loc.getCol() + 2);
    Location adjacent1 = new Location(loc.getRow() + 1, loc.getCol() - 1);
    Location adjacent2 = new Location(loc.getRow() + 1, loc.getCol() + 1);

    // try one column
    if( gr.isValid(jump1))
    {
        if( (gr.get(jump1) == null) && 
            (gr.get(adjacent1) instanceof BlackPieces))
        {
            gr.get(adjacent1).removeSelfFromGrid();
            moveTo(jump1);
            return true;
        }
    }

    // try the other column
    if( gr.isValid(jump2))
    {
        if( (gr.get(jump2) == null) && 
             (gr.get(adjacent2) instanceof BlackPieces))
        {
            gr.get(adjacent2).removeSelfFromGrid();
            moveTo(jump2);
            return true;
        }
    }

    return false;
}
于 2014-05-25T23:21:26.207 回答