【问题标题】:is there a better way to write this code in Python ? (transforming UCI-move into bitboard, chess)有没有更好的方法来用 Python 编写这段代码? (将 UCI-move 转化为 bitboard、国际象棋)
【发布时间】:2020-11-28 01:11:51
【问题描述】:

我想将 UCI-move 转换为 bitboard。

例如 a2a3 -> 32768, 8388608

我需要将 [7,6,...,0] 分配给 [a,b,...,h] 以便对于每个字母我都有分配的数字 (n) 来计算 2^n

然后我可以左移 uci[1] 或 uci[3] *8 中的值,具体取决于 start- 或 endfield。

这是我的方法,看起来不太好和多余。

def ucitoBit(uci):
    if uci[0] == 'a':
        mask1 = 2 ** 7
    if uci[0] == 'b':
        mask1 = 2 ** 6
    if uci[0] == 'c':
        mask1 = 2 ** 5
    if uci[0] == 'd':
        mask1 = 2 ** 4
    if uci[0] == 'e':
        mask1 = 2 ** 3
    if uci[0] == 'f':
        mask1 = 2 ** 2
    if uci[0] == 'g':
        mask1 = 2 ** 1
    if uci[0] == 'h':
        mask1 = 2 ** 0
    mask1 = mask1 << 8 * (int(uci[1]) - 1)

    if uci[2] == 'a':
        mask2 = 2 ** 7
    if uci[2] == 'b':
        mask2 = 2 ** 6
    if uci[2] == 'c':
        mask2 = 2 ** 5
    if uci[2] == 'd':
        mask2 = 2 ** 4
    if uci[2] == 'e':
        mask2 = 2 ** 3
    if uci[2] == 'f':
        mask2 = 2 ** 2
    if uci[2] == 'g':
        mask2 = 2 ** 1
    if uci[2] == 'h':
        mask2 = 2 ** 0
    mask2 = mask2 << 8 * (int(uci[3]) - 1)
    bitstring = [np.uint64(mask1), np.uint64(mask2)]
    return bitstring

【问题讨论】:

标签: chess code-cleanup bitboard


【解决方案1】:

如何定义两个包含行和列索引的数组并像这样使用它们:

    rows = ["1", "2", "3", "4", "5", "6", "7", "8"]
    cols = ["a", "b", "c", "d", "e", "f", "g", "h"]

def parse_move(move):
    from_col, from_row, to_col, to_row = list(move)
    from_sq = 2**((7 - cols.index(from_col)) + 8*rows.index(from_row))
    to_sq = 2**((7 - cols.index(to_col)) + 8*rows.index(to_row))
    return [from_sq, to_sq]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-08
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多