5

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.

Terradon
  • 883
  • 2
  • 13
  • 33
  • What exactly is your issue? You said you thought about a solution, did you try it? – netcoder Nov 26 '12 at 21:16
  • Is it mandatory to have a chessboard this way? Why not just represent the chessboard as chess pieces with coordinates assigned to them? – Desmond Hume Nov 26 '12 at 21:19
  • In present script we do use arrays, like $aBoard[row][col] But, for example, move validation is much easier/faster/with less code, to achieve. It is not only about displaying a chessgame, but also for online playing (usually 100-150 players simultaniously). later on, we want an online chessengine in PHP, which can communicate with other chess software. (long way to go). What we have tried: reading a lot about how bitboards are implemented in chesssftware. Made a 1st list of pseudo-code. read php.net about bitwise operators, but just can't find a way to loop over the bitvalue. – Terradon Nov 26 '12 at 21:25
  • 3
    Bah, this cries for arrays and OOP. – Madara's Ghost Nov 26 '12 at 21:29
  • It doesn't, because of the bitwise operators i don't have to check every piece or every direction for every piece, i just use bitwise operators for desicions, faster/easier to handel, much less coderules. Thanks for all your answers and discussions, but please, let me try to find out by myself, if this is the way to go. With all respect, my question is not about the best approach, but how to loop over the bitvalue:) – Terradon Nov 26 '12 at 21:33
  • 1
    I don't know how you're planning on parsing this, but I'm guessing that your king/queen positioning is going to be incorrect. – Nick Vaccaro Nov 26 '12 at 21:51
  • Thanks Norla, i would have found out, as soon as i am capable to loop over my 64bit value:) My plan is to draw the board by looping over all the bits of the bitboard, creating an array like $aBoard[row][col], with this array, i can build up the table/chessboard – Terradon Nov 26 '12 at 21:56
  • I just read the right answer on my question, but it isn't here anymore? Answer removed?? – Terradon Nov 26 '12 at 22:09

2 Answers2

2

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
    }
}
lupatus
  • 4,208
  • 17
  • 19
  • Thanks! Tested PHP_INT_MAX = OK. php version = 5.3.10, so i will upgrade this first. – Terradon Nov 26 '12 at 22:54
  • with 5.3 bit operations also work properly, just you cannot use 0b000 notation. – lupatus Nov 26 '12 at 23:05
  • I know this is an old post, but anyone else reading this, use ```bindec($value)```instead of ```intVal($value, 2)```, it's much quicker. – iforwms Feb 06 '16 at 08:01
  • your 'much quicker' is almost not measurable and bindec does not guarantee returning integer, it can return float (probably over int max, but its not specified in documantation) and then on binary operation you'll get some unexpected results. – lupatus Feb 06 '16 at 12:43
1

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.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • It is just a try out for us. In MySQL we want to use a BIT-field to store the bitboards. Looping tested with:for ($i=0; $i<64; $i++) echo '
    '.$WhitePieces{$i}; Seems to work. (this answer was removed?? it still is in my inbox!) for move validation we thought about the bitwise operators in PHP. This toughts are still experimental and we want to compare te results with the current script we use for online chessgames.
    – Terradon Nov 26 '12 at 22:24