2

I'm trying to print out a chess game which I am implementing using bitboards. I'm having trouble visualizing how would I go about display the 8x8 grid with the pieces for play.

Here's the link to the tutorial I am following (albeit incomplete).

    @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

I have the above binary set up as per a default chess set position whereby the MSB is the top left corner and the LSB is the bottom right corner.

As the game progresses, the binary literals above will change over time (currently they are at the default chess placements).

enter image description here

The game manager will then consider all the updated binary literals above the print the board again, as show above.

What would be a good way to iterate through the literals and print the necessary empty squares/playing pieces as per the information above?

Carrein
  • 3,231
  • 7
  • 36
  • 77

1 Answers1

2

For each type of chess piece (i.e. white_pawn, white_rook, black_pawn, black_rook, ...), iterate over each bit in the binary number. If the bit is 1, then draw the appropriate chess piece image, at (x, y); where

x =  bit_index % board_width         # relative position to nearest-down chess board row
y = (bit_index / board_width).floor  # nearest-down chess board row
clabe45
  • 2,354
  • 16
  • 27