我正在用 C# 编写一个带有魔法位板的国际象棋引擎,现在速度很慢。从初始位置计算 perft 6(119,060,324 个位置)需要 2 分钟,而其他引擎可以在 1-3 秒内完成。我目前正在使用这种方法来查找位板上所有 1 的索引:
public static readonly int[] index64 = {
0, 47, 1, 56, 48, 27, 2, 60,
57, 49, 41, 37, 28, 16, 3, 61,
54, 58, 35, 52, 50, 42, 21, 44,
38, 32, 29, 23, 17, 11, 4, 62,
46, 55, 26, 59, 40, 36, 15, 53,
34, 51, 20, 43, 31, 22, 10, 45,
25, 39, 14, 33, 19, 30, 9, 24,
13, 18, 8, 12, 7, 6, 5, 63
};
public static List<int> bitScan(ulong bitboard) {
var indices = new List<int>(30);
const ulong deBruijn64 = 0x03f79d71b4cb0a89UL;
while (bitboard != 0) {
indices.Add(index64[((bitboard ^ (bitboard - 1)) * deBruijn64) >> 58]);
bitboard &= bitboard - 1;
}
return indices;
}
这是被调用最多的方法,我想加快它。有没有更快的方法来做到这一点?我想返回一个数组而不是一个列表,但是我不知道如何因为位数是未知的(而且我不想要空元素,因为我有很多 foreach 循环)。
任何建议表示赞赏。