首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Gridworld的棋子游戏(Java)

使用Gridworld的棋子游戏(Java)
EN

Stack Overflow用户
提问于 2014-05-25 20:30:12
回答 1查看 823关注 0票数 0

我一直在使用Gridworld在Eclipse中创建一个Checker游戏。到目前为止,我只是在修改红片。我的目标是能够移动()块,让他们选择是跳还是跳。显然,跳跃(即当该块移动到相反颜色的一块,然后将其从网格中移除)优先于步进。我的问题是,每当我试图移动前两列或最后两列中的部分时,我就会得到一个非法的越界错误。有人能帮我解决这个问题吗?请谢谢你。

代码语言:javascript
复制
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();
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-25 23:21:26

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

  • jump2()方法中,第二个if语句不检查jump2的有效性。这可能是在gr.get(jump2 == null)条件下if语句中出现异常的原因。
  • 同样的逻辑也适用于后续的else if语句。这一次,您没有检查jump1的有效性。
  • move()方法(第一个else if )中,您只检查next1的有效性两次,这似乎是一个错误。请在您的问题中重新格式化的代码中找到它。

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

增加了两件小事。

  • canMove()方法中,只在第一个if语句处使用gr。在第二个getGrid()语句中使用的是gr而不是gr
  • 您能避免每次在new Location语句中获取网格时都执行if吗?你在使用这些网格时没有检查它们的有效性。你确定它们是有效的吗?您可以在方法的开头创建它们,检查它们的有效性,并在if语句中使用它们。它也可能有助于if语句的可读性。

添加了更多的jump2()方法简化示例

下面的代码将忽略列的另一端(无效、空或红段)。这是基于我的假设,当一方有效跳过一块黑色物体时,其他方面的情况并不重要。

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23859671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档