我试图打印出我正在使用位板实现的国际象棋游戏。我无法直观地看到如何显示8x8网格以及播放的部分。
这里是我追随的tutorial的链接(虽然不完整)。
@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是右下角。
随着游戏的进行,上面的二进制文字会随着时间的推移而改变(目前它们处于默认的国际象棋位置)。
游戏管理员将再次考虑打印板上方的所有更新二进制文字,如上所示。
根据上述信息,迭代文字并打印必要的空方块/游戏块的好方法是什么?
答案 0 :(得分:1)
对于每种类型的棋子(即white_pawn
,white_rook
,black_pawn
,black_rook
,...),迭代二进制数中的每一位。如果该位为1
,则绘制适当的棋子图像 at (x
,y
);其中
x = bit_index % board_width # relative position to nearest-down chess board row
y = (bit_index / board_width).floor # nearest-down chess board row