【问题标题】:Chess, preventing a piece from jumping over pieces when going diagonally国际象棋,防止棋子在对角线时跳过棋子
【发布时间】:2020-08-07 15:47:48
【问题描述】:

希望你们一切都好。

过去两天我一直在尝试编写一个国际象棋引擎,我遇到了我似乎无法解决的问题,我在下面的这段代码运行良好,该棋子在所有有限的 2D 空间中对角线在棋盘中(注意棋盘是一个 8*8 的二维数组),问题是我不知道如何防止一个棋子在对角线时跳过其他棋子

方法如下:

    /**
 * 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;
}

感谢您的帮助!

输出:

【问题讨论】:

  • 您必须检查路径中的每个图块是否为空。就这么简单。
  • 感谢您的回复!,我知道我需要检查每个图块,但是,问题是如何在上面的代码中实现解决方案?再次感谢
  • 构建一个从起始空间到目标空间的循环,忽略这两个,并检查所有空间是否为空。但是我会以不同的方式对整个事情进行编码(事实上,我以前也这样做过)。

标签: java chess


【解决方案1】:

这是我曾经写过的一段代码,它工作得很好,但使用的架构与你的不同。无论如何,它可能会帮助您想出自己的解决方案。

    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;
        }
    }

请注意,这是我在 2009 年编写的,它不是很好的代码,它的结构可能要好得多,并且存在文体问题。但数学很可靠。

【讨论】:

  • 感谢您的宝贵时间!,是的,您下面的代码非常有用(并且比我的 xD 少得多),如果必须,我正在考虑重构此方法,您的代码给了我一些想法,再次感谢!
  • 好吧,我基本上放弃了我实施国际象棋规则的前五次尝试,因为它们比这更混乱,而且我的初稿肯定非常血腥 - 但它确实帮助我学到了很多东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 2022-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多