我一直致力于使用 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();
}
}