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

时间:2014-06-10 02:51:44

标签: java chess

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

问题在于检查可能的移动是否安全。 我的想法是通过每个相反的部分,并检查他们是否可以移动到国王可以移动到的其中一个方格。 但这并不奏效。 尝试了几种方法来教导Pawn或King关于彼此,但没有成功。 得到了所有其他的工作,但那两个一起工作呀! Board正在使用扩展jButton的8x8 Square of Square。

我如何让国王知道如何在安全的情况下四处走动? 目前的问题是国王根本不安全,试图解决它似乎是正方形的迭代器。但那你又怎么会通过封锁来防御国王。

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

虽然此刻的棋子并不关心并阻挡国王在棋子前面移动而不是根据颜色向左前方/前方移动。

关于如何解决它的任何想法?

这是解决方案。

解决了这个问题,将国王从他当前的位置移开并检查了他的正方形,并在每个方格中寻找匹配的PieceType。 从而删除假件&如果它找到匹配的PieceType对手,它可能会移动的正方形。

国王现在很聪明,如果在攻击后没有其他部分可以攻击它,就可以躲闪攻击。

景:

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

张大

移动与Rook和Bishop完全一样,为什么不重用?

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

1 个答案:

答案 0 :(得分:3)

为国王制定合法行动,最简单的方法是:

  1. 通过8个可能的方块迭代,国王可以移动到。

  2. 在每个广场上,假装国王是其中一个,并检查它是否攻击敌人的一块。例如,我们可以沿着对角线分支,看看我们是否遇到过敌人的主教。如果我们这样做,那么转移到那个方块是不合法的。

  3. 将动作收集到上方检查未遇到任何敌人棋子的方格。

  4. 这在8x8阵列系统中可能更加冗长,需要大量类似的代码,但它比循环遍历所有敌人的部分效率更高。当你开始搜索时,国际象棋的表现很重要!当您使用基于bitboard的方法时,这种方法更简单,其中检查片段交叉点等更容易。