Java 国际象棋产生可能的移动国王安全

Java 国际象棋产生可能的移动国王安全,java,chess,Java,Chess,目前,我在尝试拼凑国际象棋,但我似乎找不到一种简单的方法来重用我必须解决的现有代码 问题在于检查可能的移动是否安全。 我的想法是通过每一个相反的部分,检查他们是否可以移动到国王可以移动到的广场之一。 但这并不奏效。 尝试了几种方法来教典当或国王了解彼此,但都没有成功。 除了那两个在一起工作之外,其他的都在工作不!电路板使用扩展jButton的8x8方形阵列 如何让国王知道如何在他安全的情况下移动棋子? 目前的问题是,国王根本不安全,试图解决它似乎是在广场上迭代。但是,你又如何通过拦网来保护国王不

目前,我在尝试拼凑国际象棋,但我似乎找不到一种简单的方法来重用我必须解决的现有代码

问题在于检查可能的移动是否安全。 我的想法是通过每一个相反的部分,检查他们是否可以移动到国王可以移动到的广场之一。 但这并不奏效。 尝试了几种方法来教典当或国王了解彼此,但都没有成功。 除了那两个在一起工作之外,其他的都在工作不!电路板使用扩展jButton的8x8方形阵列

如何让国王知道如何在他安全的情况下移动棋子? 目前的问题是,国王根本不安全,试图解决它似乎是在广场上迭代。但是,你又如何通过拦网来保护国王不受棋子的伤害呢

很像那辆车。我希望它能够看到国王的作品过去或寻找可能的威胁广场,国王可以进入

尽管当兵此时并不在意,并阻止国王在当兵面前移动,但根据颜色,不能移动到前左/前右

有没有办法解决这个问题

这是解决办法

解决这个问题的方法是将国王从他当前的位置移走,检查他周围的方格,并在每个方格中寻找与假币匹配的拼图类型。 因此,如果它发现了一个匹配的棋子类型的对手,就可以将它所站的方块从可能的移动中移除

国王现在足够聪明,可以闪避并攻击一个棋子,如果它攻击后没有其他棋子能够攻击它

国王:
@覆盖
公共集合generatePossibleMoves(){
possibleMoves.clear();
列表移动=新建ArrayList();
int[][]偏移量={
{1, 0},
{0, 1},
{-1, 0},
{0, -1},
{1, 1},
{-1, 1},
{-1, -1},
{1, -1}
};
对于(int[]o:偏移量){
Square=super.getSquare().nexter(o[0],o[1]);
if(square!=null&(square.getPiece()==null | | isoponent(square.getPiece())){
移动。添加(方形);
}
}
可能移动。添加所有(移动);
if(getSquare().isSelected()){
件[]件={
PieceType.PAWN.create(getPieceColor()),
创建(getPieceColor()),
PieceType.BISHOP.create(getPieceColor()),
创建(getPieceColor()),
创建(getPieceColor()),
创建(getPieceColor())};
奥德金=这个;
getSquare().removePiece();
用于(方形移动:移动){
if(kingMove.isEmpty()){
用于(件:件){
piece.putPieceOnSquareFirstTime(kingMove);
工件。生成许可移动();
for(方形敌人:parte.getPossibleMoves()){
如果(!friend.isEmpty()&&friend.getPiece().isoponent(piece)&&friend.getPiece().getTypeNumber()==piece.getTypeNumber()){
敌人。挫折地(颜色。蓝色);
可能移动。移除(kingMove);
打破
}
}
}
kingMove.removePiece();
}else if(isoponent(kingMove.getPiece())){
工件oldPiece=kingMove.getPiece();
用于(件:件){
kingMove.removePiece();
piece.putPieceOnSquareFirstTime(kingMove);
工件。生成许可移动();
for(Square-square1:piece.getPossibleMoves()){
如果(!square1.isEmpty()&&square1.getPiece().isoponent(piece)&&square1.getPiece().getTypeNumber()==piece.getTypeNumber()){
可能移动。移除(kingMove);
打破
}
}
}
kingMove.removePiece();
oldPiece.putPieceOnSquareFirstTime(kingMove);
}
}
putPieceOnSquareFirstTime(getSquare());
}
返回可能的动作;
}
主教
@覆盖
公共集合generatePossibleMoves(){
int row=super.getSquare().row;
int column=super.getSquare().column;
possibleMoves.clear();
//向下正对角线上所有可能的移动
对于(int j=列+1,i=行+1;j-1&&i-1&&i>-1;j--,i--){
Square=super.getSquare().getBoardSquare(i,j);
if(square.getPiece()==null){
可能移动。添加(方形);
}else if(等分量(square.getPiece()){
可能移动。添加(方形);
打破
}否则{
打破
}
}
//向下负对角线中所有可能的移动
对于(int j=column+1,i=row-1;j-1;j++,i--){
Square=super.getSq
@Override
public Collection<Square> generatePossibleMoves() {
    possibleMoves.clear();
    List<Square> moves = new ArrayList<>();
    int[][] offsets = {
        {1, 0},
        {0, 1},
        {-1, 0},
        {0, -1},
        {1, 1},
        {-1, 1},
        {-1, -1},
        {1, -1}
    };
    for (int[] o : offsets) {
        Square square = super.getSquare().neighbour(o[0], o[1]);
        if (square != null && (square.getPiece() == null || isOpponent(square.getPiece()))) {
            moves.add(square);
        }
    }
    possibleMoves.addAll(moves);
    if (getSquare().isSelected()) {
        Piece[] pieces = {
            PieceType.PAWN.create(getPieceColor()),
            PieceType.ROOK.create(getPieceColor()),
            PieceType.BISHOP.create(getPieceColor()),
            PieceType.KNIGHT.create(getPieceColor()),
            PieceType.QUEEN.create(getPieceColor()),
            PieceType.KING.create(getPieceColor())};
        Piece oldKing = this;
        getSquare().removePiece();
        for (Square kingMove : moves) {
            if (kingMove.isEmpty()) {
                for (Piece piece : pieces) {
                    piece.putPieceOnSquareFirstTime(kingMove);
                    piece.generatePossibleMoves();
                    for (Square enemy : piece.getPossibleMoves()) {
                        if (!enemy.isEmpty() && enemy.getPiece().isOpponent(piece) && enemy.getPiece().getTypeNumber() == piece.getTypeNumber()) {
                            enemy.setBackground(Color.BLUE);
                            possibleMoves.remove(kingMove);
                            break;
                        }
                    }
                }
                kingMove.removePiece();
            } else if (isOpponent(kingMove.getPiece())) {
                Piece oldPiece = kingMove.getPiece();
                for (Piece piece : pieces) {
                    kingMove.removePiece();
                    piece.putPieceOnSquareFirstTime(kingMove);
                    piece.generatePossibleMoves();
                    for (Square square1 : piece.getPossibleMoves()) {
                        if (!square1.isEmpty() && square1.getPiece().isOpponent(piece) && square1.getPiece().getTypeNumber() == piece.getTypeNumber()) {
                            possibleMoves.remove(kingMove);
                            break;
                        }
                    }
                }
                kingMove.removePiece();
                oldPiece.putPieceOnSquareFirstTime(kingMove);
            }
        }
        oldKing.putPieceOnSquareFirstTime(getSquare());
    }
    return possibleMoves;
}
@Override
public Collection<Square> generatePossibleMoves() {
    int row = super.getSquare().ROW;
    int column = super.getSquare().COLUMN;
    possibleMoves.clear();
    //all possible moves in the down positive diagonal
    for (int j = column + 1, i = row + 1; j < Board.SIZE && i < Board.SIZE; j++, i++) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the up positive diagonal
    for (int j = column - 1, i = row + 1; j > -1 && i < Board.SIZE; j--, i++) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the up negative diagonal
    for (int j = column - 1, i = row - 1; j > -1 && i > -1; j--, i--) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the down negative diagonal
    for (int j = column + 1, i = row - 1; j < Board.SIZE && i > -1; j++, i--) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    return possibleMoves;
}
@Override
public Collection<Square> generatePossibleMoves() {
    possibleMoves.clear();
    int[][] offsets = {
        {-2, 1},
        {-1, 2},
        {1, 2},
        {2, 1},
        {2, -1},
        {1, -2},
        {-1, -2},
        {-2, -1}
    };
    for (int[] o : offsets) {
        Square square = super.getSquare().neighbour(o[0], o[1]);
        if (square != null && (square.getPiece() == null || isOpponent(square.getPiece()))) {
            possibleMoves.add(square);
        }
    }
    return possibleMoves;
}
@Override
public Collection<Square> generatePossibleMoves() {
    int row = super.getSquare().ROW;
    int column = super.getSquare().COLUMN;
    possibleMoves.clear();
    //all possible moves in the up
    for (int i = row + 1; i < Board.SIZE; i++) {
        Square square = super.getSquare().getBoardSquare(i, column);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the down
    for (int i = row - 1; i > -1; i--) {
        Square square = super.getSquare().getBoardSquare(i, column);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves to the right
    for (int i = column + 1; i < Board.SIZE; i++) {
        Square square = super.getSquare().getBoardSquare(row, i);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves to the left
    for (int i = column - 1; i > -1; i--) {
        Square square = super.getSquare().getBoardSquare(row, i);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    return possibleMoves;
}
@Override
public Collection<Square> generatePossibleMoves() {
    possibleMoves.clear();
    Piece[] pieces = {
        PieceType.ROOK.create(getPieceColor()),
        PieceType.BISHOP.create(getPieceColor())
    };
    for (Piece piece : pieces) {
        piece.setSquare(getSquare());
        possibleMoves.addAll(piece.generatePossibleMoves());
    }
    return possibleMoves;
}
@Override
public Collection<Square> generatePossibleMoves() {
    possibleMoves.clear();
    boolean color = super.isWhite();
    int dx = color ? -1 : 1;

    Square ahead = super.getSquare().neighbour(dx, 0);
    if (ahead.getPiece() == null) {
        possibleMoves.add(ahead);
        if (super.getSquare().ROW == 6 && color) {
            Square aheadsecond = super.getSquare().neighbour(dx - 1, 0);
            if (aheadsecond.getPiece() == null) {
                possibleMoves.add(aheadsecond);
            }
        } else if (super.getSquare().ROW == 1 && !color) {
            Square aheadsecond = super.getSquare().neighbour(dx + 1, 0);
            if (aheadsecond.getPiece() == null) {
                possibleMoves.add(aheadsecond);
            }
        }
    }
    Square aheadLeft = super.getSquare().neighbour(dx, -1);
    if (aheadLeft != null && aheadLeft.getPiece() != null && isOpponent(aheadLeft.getPiece())) {
        possibleMoves.add(aheadLeft);
    }
    Square aheadRight = super.getSquare().neighbour(dx, 1);
    if (aheadRight != null && aheadRight.getPiece() != null && isOpponent(aheadRight.getPiece())) {
        possibleMoves.add(aheadRight);
    }
    return possibleMoves;
}