国际象棋编程(无人工智能)——动作验证

发布于 2024-12-01 11:36:34 字数 377 浏览 2 评论 0原文

我正在尝试编写自己的国际象棋引擎(没有人工智能)。 我知道有国际象棋游戏入门套件,我观看了它以获取入门灵感。

但我没有注意到的是经过验证的动作在哪里(这里是动作验证)对于我的非国王棋子阻止让自己过牌?

想象一下情况:
A5 - 对手车
A4 - 我的主教
A3 - 我的国王

,我现在不能移动我的主教,因为我要过牌。

或者您建议如何检查这种情况?

谢谢

I'm trying to program my own chess engine (no AI).
I know there is Chess Game Starter Kit and I watched it for start inspiration.

But what I didn't catch is where are validated moves (here is moves validation) for my non-king pieces preventing to get myself to check?

Imagine situation:
A5 - opponents rook
A4 - my bishop
A3 - my king

I can't move my bishop now since I would get to check.

Or how would you suggest to check this situation?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2024-12-08 11:36:34

对于给定的棋盘位置,大多数国际象棋引擎仅通过生成伪合法动作来启动。所谓伪合法,我的意思是即使发生以下情况,也会生成一个移动:

  • 让国王处于受控状态
  • 将国王移动到
  • 受攻击方格的受控城堡

这样做的原因是性能。由于 Beta 修剪,许多动作实际上不会被搜索,因此您可以通过避免全面检查动作有效性来节省时间。

对于搜索的每一步,您都需要检查它是否确实有效。这通常是通过将国王的颜色和方格(以及王的易位移动中国王旁边的方格)传递到 IsAttacked 方法中来完成的。如果该方法返回 true,则您知道该移动无效,因此不应将其包含在搜索中。

这是我自己的 C# 国际象棋引擎的 IsAttacked 方法。请记住,我的引擎是基于 magic bitboard 的,因此代码不会直接适用于您链接到的国际象棋入门套件。除非您熟悉魔法位板,否则翻译不会是微不足道的。

// IsAttacked is primarily used as a move legality test to see if a set of 
// one or more squares is under attack from the side to move.
// It returns true as soon as an attack is detected, otherwise returns false.
// It can be used for check detection, castling legality, or simply to 
// detect whether a specific square is attacked.
internal bool IsAttacked(Board board, UInt64 targetSquares, bool whiteAttacking)
{
    UInt64 slidingAttackers; Int32 targetSquare;
    UInt64 remainingTargetSquares = targetSquares;

    // Test for attacks by WHITE on any of the target squares.
    if (whiteAttacking) 
    {
        // For the remaining target squares...
        while (remainingTargetSquares != 0)
        {
            // Find the next square in the list.
            targetSquare = BitOperations.BitScanForward(remainingTargetSquares);

            // Is this square attacked by a pawn, knight, or king?
            if ((board.WhitePawns & Constants.BLACK_PAWN_ATTACKS[targetSquare]) != 0) return true;
            if ((board.WhiteKnights & Constants.KNIGHT_ATTACKS[targetSquare]) != 0) return true;
            if ((board.WhiteKing & Constants.KING_ATTACKS[targetSquare]) != 0) return true;

            // Is this square attacked by a queen or rook along a file or rank?
            slidingAttackers = board.WhiteQueens | board.WhiteRooks;
            if (slidingAttackers != 0)
            {
                if (this.RankMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                if (this.FileMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
            }

            // Is this square attacked by a queen or bishop along a diagonal?
            slidingAttackers = board.WhiteQueens | board.WhiteBishops;
            if (slidingAttackers != 0)
            {
                if (this.DiagonalA8H1Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                if (this.DiagonalA1H8Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
            }

            // This square isn't attacked - remove and move on to next target square.
            remainingTargetSquares ^= Constants.BITSET[targetSquare];
        }
    }

    // Test for attacks by BLACK on any of the target squares.
    else
    {
        // For the remaining target squares...
        while (remainingTargetSquares != 0)
        {
            // Find the next square in the list.
            targetSquare = BitOperations.BitScanForward(remainingTargetSquares);

            // Is this square attacked by a pawn, knight, or king?
            if ((board.BlackPawns & Constants.WHITE_PAWN_ATTACKS[targetSquare]) != 0) return true;
            if ((board.BlackKnights & Constants.KNIGHT_ATTACKS[targetSquare]) != 0) return true;
            if ((board.BlackKing & Constants.KING_ATTACKS[targetSquare]) != 0) return true;

            // Is this square attacked by a queen or rook along a file or rank?
            slidingAttackers = board.BlackQueens | board.BlackRooks;
            if (slidingAttackers != 0)
            {
                if (this.RankMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                if (this.FileMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
            }

            // Is this square attacked by a queen or bishop along a diagonal?
            slidingAttackers = board.BlackQueens | board.BlackBishops;
            if (slidingAttackers != 0)
            {
                if (this.DiagonalA8H1Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                if (this.DiagonalA1H8Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
            }

            // This square isn't attacked - remove and move on to next target square.
            remainingTargetSquares ^= Constants.BITSET[targetSquare];
        }
    }

    // None of the target squares are attacked.
    return false;
}

下面是为白方生成伪合法易位移动的代码片段:

// If White can still castle kingside...
if ((board.WhiteCastlingStatus & Board.EnumCastlingStatus.CanCastleOO) != 0)
{
    // And the White kingside castling squares (F1/G1) aren't occupied...
    if ((Constants.MASK_FG[Constants.WHITE_MOVE] & board.OccupiedSquares) == 0)
    {
        board.MoveBuffer[moveIndex++] = Constants.WHITE_CASTLING_OO;
    }
}

// If White can still castle queenside...
if ((board.WhiteCastlingStatus & Board.EnumCastlingStatus.CanCastleOOO) != 0)
{
    // And the White queenside castling squares (D1/C1/B1) aren't occupied...
    if ((Constants.MASK_BD[Constants.WHITE_MOVE] & board.OccupiedSquares) == 0)
    {
        board.MoveBuffer[moveIndex++] = Constants.WHITE_CASTLING_OOO;
    }
}

下面是检查伪合法易位移动是否实际上合法的代码:

// Checks whether the King is moving from or into check.
// Checks whether the King is moving across attacked squares.
internal bool IsCastlingMoveLegal(Board board, Move move)
{
    if (move.IsCastlingOO)
    {
        if (move.IsWhiteMove)
        {
            // Are any of the White kingside castling squares (E1/F1/G1) attacked?
            return !this.IsAttacked(board, Constants.MASK_EG[Constants.WHITE_MOVE], false);
        }
        else
        {
            // Are any of the Black kingside castling squares (E8/F8/G8) attacked?
            return !this.IsAttacked(board, Constants.MASK_EG[Constants.BLACK_MOVE], true);
        }
    }
    else if (move.IsCastlingOOO)
    {
        if (move.IsWhiteMove)
        {
            // Are any of the White queenside castling squares (E1/D1/C1) attacked?
            return !this.IsAttacked(board, Constants.MASK_CE[Constants.WHITE_MOVE], false);
        }
        else
        {
            // Are any of the Black queenside castling squares (E8/D8/C8) attacked?
            return !this.IsAttacked(board, Constants.MASK_CE[Constants.BLACK_MOVE], true);
        }
    }
    // Not a castling move!
    else
    {
        Debug.Assert(false, "Not a castling move!");
        return true;
    }
}

For a given board position, most chess engines start by generating pseudo-legal moves only. By pseudo-legal, I mean a move will be generated even if it:

  • Leaves the King in check
  • Moves the King into check
  • Castles across squares that are being attacked

The reason for this is performance. As many moves won't actually be searched due to beta pruning, you save time by avoiding the full check of move validity.

For every move that is searched, you need to check that it's really valid. This is normally done by passing the King's colour and square (and squares next to the King for a castling move) into an IsAttacked method. If that method returns true, you know the move isn't valid and therefore you shouldn't include it in your search.

This is the IsAttacked method from my own C# chess engine. Bear in mind that my engine is magic bitboard-based, so the code won't be directly applicable to the chess starter kit to which you linked. Unless you're familiar with magic bitboards, the translation won't be trivial.

// IsAttacked is primarily used as a move legality test to see if a set of 
// one or more squares is under attack from the side to move.
// It returns true as soon as an attack is detected, otherwise returns false.
// It can be used for check detection, castling legality, or simply to 
// detect whether a specific square is attacked.
internal bool IsAttacked(Board board, UInt64 targetSquares, bool whiteAttacking)
{
    UInt64 slidingAttackers; Int32 targetSquare;
    UInt64 remainingTargetSquares = targetSquares;

    // Test for attacks by WHITE on any of the target squares.
    if (whiteAttacking) 
    {
        // For the remaining target squares...
        while (remainingTargetSquares != 0)
        {
            // Find the next square in the list.
            targetSquare = BitOperations.BitScanForward(remainingTargetSquares);

            // Is this square attacked by a pawn, knight, or king?
            if ((board.WhitePawns & Constants.BLACK_PAWN_ATTACKS[targetSquare]) != 0) return true;
            if ((board.WhiteKnights & Constants.KNIGHT_ATTACKS[targetSquare]) != 0) return true;
            if ((board.WhiteKing & Constants.KING_ATTACKS[targetSquare]) != 0) return true;

            // Is this square attacked by a queen or rook along a file or rank?
            slidingAttackers = board.WhiteQueens | board.WhiteRooks;
            if (slidingAttackers != 0)
            {
                if (this.RankMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                if (this.FileMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
            }

            // Is this square attacked by a queen or bishop along a diagonal?
            slidingAttackers = board.WhiteQueens | board.WhiteBishops;
            if (slidingAttackers != 0)
            {
                if (this.DiagonalA8H1Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                if (this.DiagonalA1H8Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
            }

            // This square isn't attacked - remove and move on to next target square.
            remainingTargetSquares ^= Constants.BITSET[targetSquare];
        }
    }

    // Test for attacks by BLACK on any of the target squares.
    else
    {
        // For the remaining target squares...
        while (remainingTargetSquares != 0)
        {
            // Find the next square in the list.
            targetSquare = BitOperations.BitScanForward(remainingTargetSquares);

            // Is this square attacked by a pawn, knight, or king?
            if ((board.BlackPawns & Constants.WHITE_PAWN_ATTACKS[targetSquare]) != 0) return true;
            if ((board.BlackKnights & Constants.KNIGHT_ATTACKS[targetSquare]) != 0) return true;
            if ((board.BlackKing & Constants.KING_ATTACKS[targetSquare]) != 0) return true;

            // Is this square attacked by a queen or rook along a file or rank?
            slidingAttackers = board.BlackQueens | board.BlackRooks;
            if (slidingAttackers != 0)
            {
                if (this.RankMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                if (this.FileMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
            }

            // Is this square attacked by a queen or bishop along a diagonal?
            slidingAttackers = board.BlackQueens | board.BlackBishops;
            if (slidingAttackers != 0)
            {
                if (this.DiagonalA8H1Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                if (this.DiagonalA1H8Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
            }

            // This square isn't attacked - remove and move on to next target square.
            remainingTargetSquares ^= Constants.BITSET[targetSquare];
        }
    }

    // None of the target squares are attacked.
    return false;
}

Here's a fragment of code that generates pseudo-legal castling moves for White:

// If White can still castle kingside...
if ((board.WhiteCastlingStatus & Board.EnumCastlingStatus.CanCastleOO) != 0)
{
    // And the White kingside castling squares (F1/G1) aren't occupied...
    if ((Constants.MASK_FG[Constants.WHITE_MOVE] & board.OccupiedSquares) == 0)
    {
        board.MoveBuffer[moveIndex++] = Constants.WHITE_CASTLING_OO;
    }
}

// If White can still castle queenside...
if ((board.WhiteCastlingStatus & Board.EnumCastlingStatus.CanCastleOOO) != 0)
{
    // And the White queenside castling squares (D1/C1/B1) aren't occupied...
    if ((Constants.MASK_BD[Constants.WHITE_MOVE] & board.OccupiedSquares) == 0)
    {
        board.MoveBuffer[moveIndex++] = Constants.WHITE_CASTLING_OOO;
    }
}

And here's the code that checks whether a pseudo-legal castling move is actually legal:

// Checks whether the King is moving from or into check.
// Checks whether the King is moving across attacked squares.
internal bool IsCastlingMoveLegal(Board board, Move move)
{
    if (move.IsCastlingOO)
    {
        if (move.IsWhiteMove)
        {
            // Are any of the White kingside castling squares (E1/F1/G1) attacked?
            return !this.IsAttacked(board, Constants.MASK_EG[Constants.WHITE_MOVE], false);
        }
        else
        {
            // Are any of the Black kingside castling squares (E8/F8/G8) attacked?
            return !this.IsAttacked(board, Constants.MASK_EG[Constants.BLACK_MOVE], true);
        }
    }
    else if (move.IsCastlingOOO)
    {
        if (move.IsWhiteMove)
        {
            // Are any of the White queenside castling squares (E1/D1/C1) attacked?
            return !this.IsAttacked(board, Constants.MASK_CE[Constants.WHITE_MOVE], false);
        }
        else
        {
            // Are any of the Black queenside castling squares (E8/D8/C8) attacked?
            return !this.IsAttacked(board, Constants.MASK_CE[Constants.BLACK_MOVE], true);
        }
    }
    // Not a castling move!
    else
    {
        Debug.Assert(false, "Not a castling move!");
        return true;
    }
}
江心雾 2024-12-08 11:36:34

我的国际象棋程序有一个方法可以检查某个区域是否受到威胁。在计算国王的移动时,它会检查国王可能移动到的每个区域(如果该区域受到威胁)。

My chess program has a method that checks if a field is threatened. When calculating moves for the king it checks for every field the king might move to if that field is threatened.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文