我正在编程国际象棋ai。我想将钻头从king_span移到左侧时遇到问题。当我将钻头移至45个位置时,它工作正常。如果我想将它们移动超过45个位置,则输出的位板是相同的,就好像它仅移动了45个位置。为什么不进一步移动它们,如何解决该问题?为此,我是否需要第二个king_span?
感谢您的帮助。
king_span = int("0000000000000000000000000000000000000000000001110000010100000111", 2)
def print_bitboard(bitboard):
board = '{:064b}'.format(bitboard)
for i in range(8):
print(board[8 * i + 0] + " " + board[8 * i + 1] + " " + board[8 * i + 2] + " " + board[8 * i + 3] + " " + board[
8 * i + 4] + " " + board[8 * i + 5] + " " + board[8 * i + 6] + " " + board[8 * i + 7])
print_bitboard(king_span << 45)
输出:
1 1 1 0 0 0 0 0
1 0 1 0 0 0 0 0
1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
->位没有进一步移动。下一个位板应如下所示:
1 1 0 0 0 0 0 0--> this bit gets deleted to zero
0 1 0 0 0 0 0 0--> this bit gets deleted to zero too
1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
答案 0 :(得分:0)
您的初始数字为1110000010100000111
-表示您的数字中有19个有效位。
将位向左移动45个二进制位时,您得到的数字以45 + 19 = 64
个有效数字表示:
1110000010100000111000000000000000000000000000000000000000000000
如果移动45步以上,则最终将超过64位。假设您移动了61步;您的最终结果将有80位数字:
new_number = king_span << 61
print('{:b}'.format(new_number))
将打印
11100000101000001110000000000000000000000000000000000000000000000000000000000000
由于您的for
循环仅打印8行,每行8位,这意味着您只在打印64位。你有更多;在此示例中,您有80位数字。其余数字只是不打印。