java - 使用 Gridworld 的跳棋游戏 (Java)

标签 java gridworld

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

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();
}
}

最佳答案

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

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

  • 相同的逻辑适用于后续的 else if 语句。这次,您没有检查 jump1 的有效性。

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

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

[添加]两件小事。

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

  • 每次在 if 语句中获取网格时,是否不能避免执行 new Location 操作?您在使用这些网格时没有检查它们的有效性。您确定它们有效吗?您可以在方法的开头创建它们,检查它们的有效性,然后在 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;
}

关于java - 使用 Gridworld 的跳棋游戏 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859671/

相关文章:

java - 如何抛出并捕获 IllegalArgumentException?

java - Gridworld 案例研究 (java) : Mac Errors

java - 将字符网格添加到 jPanel

Java:如何解决读写器问题?

java - BoxLayout 的对齐问题

java - 如何将 javaFX 14 应用程序导出为 IntelliJ 中的可执行文件?

java - 创建 ArrayList 时应为 '(' 或 '['

java - 我的扫描仪/终端窗口在重复一次后停止运行

java - 帮助 EJB 创建无状态 session Bean

java - JNI - 创建另一个 C++ 类的实例