2

我正在尝试打印出我正在使用位板实现的国际象棋游戏。我无法想象我将如何显示 8x8 网格以及用于播放的棋子。

这是我正在关注的教程的链接(尽管不完整)。

    @white_pawn    = 0b0000000000000000000000000000000000000000000000001111111100000000
    @white_rook    = 0b0000000000000000000000000000000000000000000000000000000010000001
    @white_knight  = 0b0000000000000000000000000000000000000000000000000000000001000010
    @white_bishop  = 0b0000000000000000000000000000000000000000000000000000000000100100
    @white_king    = 0b0000000000000000000000000000000000000000000000000000000000010000
    @white_queen   = 0b0000000000000000000000000000000000000000000000000000000000001000

    @black_pawn    = 0b0000000011111111000000000000000000000000000000000000000000000000
    @black_rook    = 0b1000000100000000000000000000000000000000000000000000000000000000
    @black_knight  = 0b0100001000000000000000000000000000000000000000000000000000000000
    @black_bishop  = 0b0010010000000000000000000000000000000000000000000000000000000000
    @black_king    = 0b0001000000000000000000000000000000000000000000000000000000000000
    @black_queen   = 0b0000100000000000000000000000000000000000000000000000000000000000

    @all_pieces = @all_white | @all_black

我根据默认国际象棋设置位置设置了上述二进制文件,其中 MSB 是左上角,LSB 是右下角。

随着游戏的进行,上面的二进制文字会随着时间而改变(目前它们处于默认的国际象棋位置)。

在此处输入图像描述

然后,游戏管理器将再次考虑打印板上方的所有更新的二进制文字,如上所示。

根据上面的信息,迭代文字并打印必要的空方块/播放件的好方法是什么?

4

1 回答 1

2

对于每种类型的棋子(即white_pawn, white_rook, black_pawn, black_rook, ...),迭代二进制数中的每个位。如果位为,则( , ) 处1绘制相应的棋子图像;在哪里xy

x =  bit_index % board_width         # relative position to nearest-down chess board row
y = (bit_index / board_width).floor  # nearest-down chess board row
于 2017-08-08T16:35:37.937 回答