Gridworld を使用して、Eclipse でチェッカー ゲームを作成する作業を行っています。これまでのところ、私は赤い部分だけを修正しています。私の目標は、ピースを move() して、ジャンプするかステップするかを選択できるようにすることです。明らかに、ジャンプ (つまり、ピースが反対の色のピースの上を移動し、グリッドからそのピースを削除する場合) はステップよりも優先されます。私の問題は、最初の 2 列または最後の 2 列にあるピースを移動しようとするたびに、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();
}
}