Question

I want to build a chessboard via bitboard system. Starting with 12 bitboards i want to display a table (chessboard), during loop/iteration a piece must be drawn.

How do i loop through all bitvalues? I was thinking of something like: for(i=0;i<64;i++) draw table / build array / draw empty square

These are my my values to start a game:

    function init_game($whitePlayer,$blackPlayer)
{
    $WhitePawns     = '0000000000000000000000000000000000000000000000001111111100000000';
    $WhiteKnights   = '0000000000000000000000000000000000000000000000000000000001000010';
    $WhiteBishops   = '0000000000000000000000000000000000000000000000000000000000100100';
    $WhiteRooks     = '0000000000000000000000000000000000000000000000000000000010000001';
    $WhiteQueens    = '0000000000000000000000000000000000000000000000000000000000010000';
    $WhiteKing      = '0000000000000000000000000000000000000000000000000000000000001000';

    $BlackPawns     = '0000000011111111000000000000000000000000000000000000000000000000';
    $BlackKnights   = '0100001000000000000000000000000000000000000000000000000001000010';
    $BlackBishops   = '0010010000000000000000000000000000000000000000000000000000100100';
    $BlackRooks     = '1000000100000000000000000000000000000000000000000000000000000000';
    $BlackQueens    = '0000100000000000000000000000000000000000000000000000000000000000';
    $BlackKing      = '0001000000000000000000000000000000000000000000000000000000000000';

    $WhitePieces = $WhitePawns|$WhiteKnights|$WhiteBishops|$WhiteRooks|$WhiteQueens|$WhiteKing;
    $BlackPieces = $BlackPawns|$BlackKnights|$BlackBishops|$BlackRooks|$BlackQueens|$BlackKing;
}

Some people asked me: why bitboard appoach? Answer: About bitboard

A bitboard, often used for boardgames such as chess, checkers and othello, is a specialization of the bitset data structure, where each bit represents a game position or state, designed for optimization of speed and/or memory or disk use in mass calculations. Bits in the same bitboard relate to each other in the rules of the game often forming a game position when taken together. Other bitboards are commonly used as masks to transform or answer queries about positions. The "game" may be any game-like system where information is tightly packed in a structured form with "rules" affecting how the individual units or pieces relate.

Was it helpful?

Solution

First you have to check if your PHP version supports 64bit integers, otherwise you will have strange results.

Just run:

echo PHP_INT_MAX;

and if result is 9223372036854775807 then it should work.

You're using strings and I suppose that when you'll do $string | $string in form like you're doing it above then it will be cast as integer with base 10, so the result won't be what you want. Since PHP 5.4 you can use 0b000 notation, for lower PHP version you'll need to keep it in hexadecimal or base 10 format. If you're storing values in DB or somewhere like that and you'll receive value as string or you just want to keep it in format presented above, then you have to use intVal($value, 2) first to cast it properly.

To iterate over the value you can use just for loop (as you suggested):

$value = intVal($WhitePieces,2);
for ($i = 0 ; $i < 64 ; ++$i) {
    if ((pow(2,$i) & $value)) {
        // draw piece
    }
}

OTHER TIPS

You do not have bitvalues, you do have strings. And strings should be difficult to or.

How do you loop? Use an array and foreach.

How do you use 64bit values? Use PHP 5.4 and the binary number format: 0b00001111 => 16 - alternatively express the integer value as hex or decimal, which should be completely ok for a game setup routine that will not change because the rules are known for centuries.

Remember that you have to use a 64Bit system to execute your code, otherwise PHP will be unable to support 64Bit integers, and either treat them as float values, or shorten them to 32Bit values, depending on what you actually do.

Because of all this, I'd suggest NOT to use bit fields for the solution. They seem like a great idea to program more assembler-like, but you are not writing assembler, and will probably pay for this approach with non-optimal performance compared to anything else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top