Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Peanut head {me} still does not get it. Bitboard puzzles

Author: KarinsDad

Date: 20:19:57 03/22/99

Go up one level in this thread


On March 22, 1999 at 22:35:03, Dann Corbit wrote:

>/*
>** Why not just do something like this?
>** How are bitboards faster?
>*/
>#include <assert.h>
>
>typedef struct tag_bitboard
>  {
>    char rows[8];
>  } bitboard;
>
>void
>SetSquare (bitboard * b, int row, int col)
>{
>#ifdef UNIT_TEST
>  assert (b);
>  assert (row >= 0 && row <= 7);
>  assert (col >= 0 && col <= 7);
>#endif
>  b->rows[row] |= (1 << col);
>}
>void
>FlipSquare (bitboard * b, int row, int col)
>{
>#ifdef UNIT_TEST
>  assert (b);
>  assert (row >= 0 && row <= 7);
>  assert (col >= 0 && col <= 7);
>#endif
>  b->rows[row] ^= (1 << col);
>}
>void
>ClearSquare (bitboard * b, int row, int col)
>{
>#ifdef UNIT_TEST
>  assert (b);
>  assert (row >= 0 && row <= 7);
>  assert (col >= 0 && col <= 7);
>#endif
>  b->rows[row] &= ~(1 << col);
>}
>int
>GetSquare (bitboard b, int row, int col)
>{
>#ifdef UNIT_TEST
>  assert (row >= 0 && row <= 7);
>  assert (col >= 0 && col <= 7);
>#endif
>  return (b.rows[row] >> (col) & 1);
>}
>
>#ifdef UNIT_TEST
>#include <stdio.h>
>void
>ShowBoard (bitboard b)
>{
>  int i;
>  int j;
>  for (i = 0; i < 8; i++)
>    {
>      for (j = 0; j < 8; j++)
>        if (GetSquare (b, i, j))
>          putchar ('*');
>        else
>          putchar ('.');
>      putchar ('\n');
>    }
>  putchar ('\n');
>}
>#endif
>
>#ifdef UNIT_TEST
>int
>main (void)
>{
>  bitboard bb = {0};
>  bitboard z = bb;
>  int i;
>
>  for (i = 0; i < 8; i++)
>    SetSquare (&bb, i, i);
>  ShowBoard (bb);
>
>  bb = z;
>  for (i = 0; i < 8; i++)
>    SetSquare (&bb, i, 0);
>  ShowBoard (bb);
>
>  bb = z;
>  for (i = 0; i < 8; i++)
>    SetSquare (&bb, 0, i);
>  ShowBoard (bb);
>  return 0;
>}
>#endif

Dann,

This looks like it will work for some things. However, how do you determine if a
file is open or not since your bitboard is row based?

I just check if the appropriate xor column bitboard == 0. To check for a
partially open file, I check if the white column bitboard == 0 and the black
column bitboard |= 0 (or vice versa if checking half open black files).

My move generation algorithm creates all legal rook and queen horizontal (and
vertical moves, and bishop and queen diagonal moves) moves with one small loop
and one table lookup and one AND and one XOR per direction per piece per
horizontal, vertical, or diagonal direction. If you have a queen, 2 rooks, and 2
bishops on the board for the moving side, this is 12 table lookups, 12 ANDs and
12 XORs (and a few additions to get to the correct row/column) and you have 12
bytes with the data for all legal queen, rook, and bishop moves in them (if a
piece is pinned, I do not do the search in the non-pinned directions). If any of
these bytes == 0, then there are no legal moves for that piece in either of two
directions.

This can also be used for your king moves if you determine which squares the
king cannot move to ahead of time (i.e. you put a fake same color bit into a
copy of the bitboard so that the algorithm does not find a legal move there, but
I have not done that yet). If all 4 king bytes == 0, then your king is trapped.

Since my bitboards are set up once and then only delta information is added to
them, it appears to be fairly quick. Is it faster? I do not know. I only know
that I use my bitboards for a lot of other things in additional to move
generation.

KarinsDad :)



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.