Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a bit value?

Tags:

php

bitboard

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.

like image 367
Terradon Avatar asked Oct 23 '22 00:10

Terradon


1 Answers

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
    }
}
like image 166
lupatus Avatar answered Nov 01 '22 11:11

lupatus