c# - 如何在 VB.NET 中实现 Bitboard?

标签 c# .net vb.net

我的问题是:

  • 是否可以实现 bitboard在 vb.net 中?
  • 在 VB 中做位板的任何教程/引用资料?

  • C# 答案是可以接受的,因为它并不难翻译。

    最佳答案

    要在 VB(或 C#)中实现位板,请使用 System.UInt64 .这可以容纳 64 位,国际象棋棋盘的每个方格 1 位。这种值类型适用于许多快速的按位运算。我不建议使用另一张海报推荐的 BitArray,因为它太慢了。任何体面的国际象棋引擎的基本要求之一是速度。

    要回答您的第二个问题,这是 good bitboard tutorial .

    以下是我自己的 C# 国际象棋引擎中的一些示例。正如您从代码中看到的那样,使用位板可能需要一段时间才能理解,但它们通常非常快,尤其是对于位置评估。

    示例 1 - 位板定义:

    internal UInt64 WhiteKing;
    internal UInt64 WhiteQueens;
    internal UInt64 WhiteRooks;
    internal UInt64 WhiteBishops;
    internal UInt64 WhiteKnights;
    internal UInt64 WhitePawns;
    internal UInt64 WhitePieces;
    

    示例 2 - 位板初始化:
    // Initialise piece bitboards using square contents.
    private void InitPieceBitboards()
    {
        this.WhiteKing = 0; 
        this.WhiteQueens = 0; 
        this.WhiteRooks = 0; 
        this.WhiteBishops = 0; 
        this.WhiteKnights = 0; 
        this.WhitePawns = 0;
    
        for (Int16 i = 0; i < 64; i++)
        {
            if (this.Squares[i] == Constants.WHITE_KING)
            {
                this.WhiteKing = this.WhiteKing | Constants.BITSET[i];
            }
            if (this.Squares[i] == Constants.WHITE_QUEEN)
            {
                this.WhiteQueens = this.WhiteQueens | Constants.BITSET[i];
            } 
            if (this.Squares[i] == Constants.WHITE_ROOK) 
            {
                this.WhiteRooks = this.WhiteRooks | Constants.BITSET[i];
            }
            if (this.Squares[i] == Constants.WHITE_BISHOP) 
            {
                this.WhiteBishops = this.WhiteBishops | Constants.BITSET[i];
            }
            if (this.Squares[i] == Constants.WHITE_KNIGHT) 
            {
                this.WhiteKnights = this.WhiteKnights | Constants.BITSET[i];
            }
            if (this.Squares[i] == Constants.WHITE_PAWN) 
            {
                this.WhitePawns = this.WhitePawns | Constants.BITSET[i];
            }
    
            this.WhitePieces = this.WhiteKing | this.WhiteQueens | 
                               this.WhiteRooks | this.WhiteBishops | 
                               this.WhiteKnights | this.WhitePawns;
            this.BlackPieces = this.BlackKing | this.BlackQueens | 
                               this.BlackRooks | this.BlackBishops | 
                               this.BlackKnights | this.BlackPawns;
            this.SquaresOccupied = this.WhitePieces | this.BlackPieces;
        }
    }
    

    示例 3 - 移动生成:
    // We can't capture one of our own pieces.
    eligibleSquares = ~this.WhitePieces;
    
    // Generate moves for white knights.
    remainingKnights = this.WhiteKnights;
    
    // Generate the moves for each knight...
    while (remainingKnights != 0)
    {
        squareFrom = BitOps.BitScanForward(remainingKnights);
        generatedMoves = Constants.ATTACKS_KNIGHT[squareFrom] & eligibleSquares;
        while (generatedMoves != 0)
        {
            squareTo = BitOps.BitScanForward(generatedMoves);
            moveList.Add(new Move(squareFrom, squareTo, Constants.WHITE_KNIGHT, 
                                  this.Squares[squareTo], Constants.EMPTY));
            generatedMoves ^= Constants.BITSET[squareTo];
        }
        // Finished with this knight - move on to the next one.
        remainingKnights ^= Constants.BITSET[squareFrom];
    }    
    

    示例 4 - 计算 Material 分数:
    // Material score from scratch, in centipawns from White's perspective.
    internal static Int32 ScoreMaterial(Board position)
    {
        return BitOps.BitCountWegner(position.WhitePawns)   * Constants.VALUE_PAWN +
               BitOps.BitCountWegner(position.WhiteKnights) * Constants.VALUE_KNIGHT +
               BitOps.BitCountWegner(position.WhiteBishops) * Constants.VALUE_BISHOP +
               BitOps.BitCountWegner(position.WhiteRooks)   * Constants.VALUE_ROOK   +
               BitOps.BitCountWegner(position.WhiteQueens)  * Constants.VALUE_QUEEN  -
               BitOps.BitCountWegner(position.BlackPawns)   * Constants.VALUE_PAWN   -
               BitOps.BitCountWegner(position.BlackKnights) * Constants.VALUE_KNIGHT -
               BitOps.BitCountWegner(position.BlackBishops) * Constants.VALUE_BISHOP -
               BitOps.BitCountWegner(position.BlackRooks)   * Constants.VALUE_ROOK   -
               BitOps.BitCountWegner(position.BlackQueens)  * Constants.VALUE_QUEEN;
    }
    

    示例 5 - 计算工件移动性:
    // Calculate mobility score for white knights.
    remainingPieces = position.WhiteKnights;
    while (remainingPieces != 0)
    {
        squareFrom = BitOps.BitScanForward(remainingPieces);
        mobilityKnight += BitOps.BitCountWegner(Constants.ATTACKS_KNIGHT[squareFrom]
                                                & unoccupiedSquares);
        remainingPieces ^= Constants.BITSET[squareFrom];
     }
    

    关于c# - 如何在 VB.NET 中实现 Bitboard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9635214/

    相关文章:

    c# - 用 RichTextBox 替换 WinForms TextBox

    c# - Windows Phone 8.1 内存问题

    c# - 为什么我需要在 C#/.Net 代码中使用这些讨厌的注释?

    VB.NET 进程 BeginOutputReadLine 不工作

    javascript - 从 SQL 查询获取值到文本框

    vb.net - 重置文本框的背景颜色

    c# - ASP.NET WEB API 启动类

    c# - 如何在 ASP.NET MVC 5 中将 IAuthenticationManager 与 Ninject 绑定(bind)?

    c# - 如何获取字符串中的xml节点值

    c# - .NET 中的自动问答 (FAQ)