私はJavaでチェスゲームを作っていて、移動中のピースのパスをブロックしているピースがないことを確認するためにテストしています。ピースは(srcx、srcY)から(dstX、dstY)に移動します。このコードを単純化するにはどうすればよいですか? (チェスゲームの妨害テスト)
私はルークのために障害物があるかどうかをチェックする。このコードは書いている:任意のアイデアは...
if(dstY == srcY) {
// No change on Y axis, so moving east or west
if(dstX > srcX) {
// Moving east
// Test every cell the piece will pass over
for(int x = srcX+1; x < dstX; x++) {
// Is the cell set?
if(isPiece(x, srcY)) {
return true;
}
}
} else {
// Must be moving west
// Test every cell the piece will pass over
for(int x = srcX-1; x > dstX; x--) {
// Is the cell set?
if(isPiece(x, srcY)) {
return true;
}
}
}
} else if(dstX == srcX) {
// No change on X axis, so moving north or south
if(dstY > srcY) {
// Moving north
// Test every cell the piece will pass over
for(int y = srcY+1; y < dstY; y++) {
// Is the cell set?
if(isPiece(srcX, y)) {
return true;
}
}
} else {
// Must be moving south
// Test every cell the piece will pass over
for(int y = srcY-1; y > dstY; y--) {
// Is the cell set?
if(isPiece(srcX, y)) {
return true;
}
}
}
}
を、それは少し大きいですし、私はそれが簡体ことができると確信していますか?
ps、これは唯一の妨害試験です。私はすでに他のすべてを検証しました。
ナイトとキャスティングの特別なケースはありませんか?それともすでに他の場所で扱っていますか? –