英文:
Chess, preventing a piece from jumping over pieces when going diagonally
问题
希望你们一切都好。
我在过去的两天里一直在尝试编写一个国际象棋引擎,但我遇到了一个似乎无法解决的问题。我有下面的代码,它完美地工作,棋子可以在棋盘上的所有2D空间中对角线移动(请注意,棋盘是一个8x8的2D数组),但问题是我不知道如何阻止棋子在对角线移动时跳过其他棋子。
这是这个方法:
/**
* y = abs(x)
* y = -abs(x)
* 编写这两个函数将允许棋子在所有方向上对角线移动。
*@param pieceX: 棋子的当前X坐标
* @param pieceY : 棋子的当前Y坐标
* @param board: Board实例,用于获取所有棋子的位置
* @return 合法的移动
*/
protected int[][] diagonalMove(int pieceX, int pieceY, Board board)
{
ArrayList<Integer> legalXMovements = new ArrayList<>();
ArrayList<Integer> legalYMovements = new ArrayList<>();
// 8x8大小的笛卡尔平面。
for (int x = 7; x > -8; x--)
{
for (int y = 7; y > -8; y--)
{
// 函数1:y = abs(x) 且 y > 0: Math.abs(x) == Math.abs(y)
// 函数2:y = -abs(x) : y == -Math.abs(x)
if (Math.abs(x) == Math.abs(y) || y == -Math.abs(x))
{
// 防止越界。
if (pieceX + x >= 0 && pieceX + x < 8)
{
if (pieceY + y >= 0 && pieceY + y < 8)
{
// 确保棋子不吃掉己方的棋子
// 如果格子为空。
if (board.getTile(pieceX + x, pieceY + y).checkIsEmpty())
{
legalXMovements.add(pieceX + x);
legalYMovements.add(pieceY + y);
}
else
{
// 如果是敌人。
if (color != board.getTile(pieceX + x, pieceY + y).getPiece().getColor())
{
legalXMovements.add(pieceX + x);
legalYMovements.add(pieceY + y);
}
}
}
}
}
}
}
// 国际象棋,防止棋子在对角线移动时跳过其他棋子
int[][] finalLegalMovements = new int[legalXMovements.size()][2];
for (int x = 0; x < legalXMovements.size(); x++)
{
finalLegalMovements[x][0] = legalXMovements.get(x);
finalLegalMovements[x][1] = legalYMovements.get(x);
}
return finalLegalMovements;
}
感谢你的帮助!
英文:
hope you guys are doing fine.
I've been trying to code a chess engine in the last two days, i have problem i can't seem to solve, i have this code below that works perfecally fine, the piece goes diagonally in all the 2D space limited in the board (note that the board is an 8*8 2D array), the problem is i can't figure out how to prevent a piece from jumping over other pieces when going diagonally
here is the method:
/**
* y = abs(x)
* y = -abs(x)
* programming these 2 functions will let the piece go diagonally in all directions.
*@param pieceX: current X coordinates of the piece
* @param pieceY : current Y Coordinates of the piece
* @param board: Board instance to get the locations of all the pieces
* @return legal movements
*/
protected int[][] diagonalMove(int pieceX, int pieceY, Board board)
{
ArrayList<Integer> legalXMovements = new ArrayList<>();
ArrayList<Integer> legalYMovements = new ArrayList<>();
//cartesian plane with a size of 8*8.
for (int x = 7; x > -8; x--)
{
for (int y = 7; y > -8; y--)
{
//function 1: y = abs(x) and y > 0: Math.abs(x) == Math.abs(y)
//function 2: y = -abs(x) : y == -Math.abs(x)
if (Math.abs(x) == Math.abs(y) || y == -Math.abs(x))
{
//prevent OutOfBounds at any case.
if (pieceX + x >= 0 && pieceX + x < 8)
{
if (pieceY + y >= 0 && pieceY + y < 8)
{
//make sure that the piece doesn't eat his allies
//if Tile is empty.
if (board.getTile(pieceX + x, pieceY + y).checkIsEmpty())
{
legalXMovements.add(pieceX + x);
legalYMovements.add(pieceY + y);
}
else
{
//if enemy.
if (color != board.getTile(pieceX + x, pieceY + y).getPiece().getColor())
{
legalXMovements.add(pieceX + x);
legalYMovements.add(pieceY + y);
}
}
}
}
}
}
}
//Chess, preventing a piece from jumping over pieces when going diagonally
int[][] finalLegalMovements = new int[legalXMovements.size()][2];
for (int x = 0; x < legalXMovements.size(); x++)
{
finalLegalMovements[x][0] = legalXMovements.get(x);
finalLegalMovements[x][1] = legalYMovements.get(x);
}
return finalLegalMovements;
}
Thanks for helping!
答案1
得分: 1
这是我以前写的一小段代码,它运行得很好,但与你的架构不同。无论如何,它可能会帮助你提出自己的解决方案。
else if((piece == 'B') || (piece == 'b')){
//检查deltaX和deltaY是否相等
if(Math.abs(move.getFromX()-move.getToX()) == Math.abs(move.getFromY() - move.getToY())){
//检查方向
int directionX = 1;
if(move.getToX() < move.getFromX())
directionX = -1;
int directionY = 1;
if(move.getToY() < move.getFromY())
directionY = -1;
//检查是否所有位置都为空
int y = move.getFromY();
for(int x = move.getFromX() + directionX; x != move.getToX(); x += directionX){
y += directionY;
if(position.getPieceAt(x, y) != ' ')
//有棋子阻挡 -> 移动非法!
return false;
}
}
else{
return false;
}
}
请注意,我是在2009年写的这段代码,它不是很好的代码,结构可以更好地组织,并且存在风格问题。但数学部分是可靠的。
英文:
Here is a bit of code I once wrote, which works just fine, but uses a different architecture from yours. Anyway, it might help you come up with your own solution.
else if((piece == 'B') || (piece == 'b')){
//Check if deltaX and deltaY are equal
if(Math.abs(move.getFromX()-move.getToX()) == Math.abs(move.getFromY() - move.getToY())){
//check the directions
int directionX = 1;
if(move.getToX() < move.getFromX())
directionX = -1;
int directionY = 1;
if(move.getToY() < move.getFromY())
directionY = -1;
// Check if everything is free
int y = move.getFromY();
for(int x = move.getFromX() + directionX; x != move.getToX(); x += directionX){
y += directionY;
if(position.getPieceAt(x, y) != ' ')
//A piece is in the way -> move illegal!
return false;
}
}
else{
return false;
}
}
Note that I wrote this in 2009, it is not good code, it could be structured far better and has stylistic problems. But the maths are solid.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论