Bitboard for chess engine help

Sort:
theturk1234

Hi,

I have a question that has been driving me crazy: I am making a chess engine in C++ and I have done a fair amount of research on bitboards in particular. However, the one thing that they fail to mention is how to declare a bitboard; I kow all about ANDing and ORing them, but they never mention how to actually declare one. Should I use a class, Enumeration, vector, namespace, or what? Your comments would be greatly appreciated.

Tatzelwurm

The underlying type should be uint64_t, if that's what you mean.

For starting out, I recommend 0x88 board representation, which is much simpler.

You are aware of of http://chessprogramming.wikispaces.com/ ?

theturk1234

Sorry, I do not know what a uint64_t is; I have seen it before, but is it a data type? HOw do you declare one? Actually, I am determined to use bitboards. Yes, I have been all over wikispaces and as far as I could tell, they don't say how to declare a bitboard. Excuse me if I am overlookig something obvious. If I am reading you right, I should just declare a bitboard like this?: uint64_t Bitboard

{

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

}

 for an empty board?

 

Thanks

behemoth9

Exactly what do you mean about uint64_t?

behemoth9

A 64-bit integer is the most common way to declare a bitboard.

theturk1234

Yes, but how do you declare a 64-bit integer?

behemoth9

You declare, on some systems, _int64

theturk1234

It does not seem to work on my system. I am using MinGW.

Tatzelwurm

uint64_t is an unsigned integral type with a 64 bit representation, so it's exactly what you're looking for. It is defined in <cstdint>.

theturk1234

So I should #include it?

theturk1234

And should I just declare a bitboard like this:?

#include <cstdint>

int main()

{

uint64_t Bitboard {

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

}

// And if I want to display the uint64_t I just cout ?

return 0;

}

 

Is this a valid and functional bitboard?

 

If so, thanks; if not, could you give me some advice?

rtr1129

unsigned long long bitboard;

bitboard = 0x0000000000000000;

rtr1129

Or you can use a typedef:

typedef unsigned long long u64;

u64 bitboard = 0x0000000000000000;

BSKg

Unsigned long long int myBitboard = 0;

myBitboard |= (1 >> 32); //this sets the 33rd bit to 1

myBitboard &= (0 >> 63); // sets 64th bit to 0

// the lines above creates an integer (number) variable which on the hardware is represented by 64 bits, each being either 1 or 0. The idea of bitboards is to have each bit represent a square on the board, or to store multiple smaller numbers inside a single int

 

unsigned long long int whitePawnsBB = 0;

For(int i=0; i<8; i++) {

whitePawnsBB |= 1 >> (7 + i);

}

theturk1234

You guys are so helpful. THANK YOU!!!!

BSKg
[COMMENT DELETED]
theturk1234

I do not know hexadecimal form so well--does what you said mean that every time I update (after each turn, for example) the bitboards I have to tell the compiler in hex form? Or can I do it visually in 8x8 array of xeroes like  showed before? If so, how?

Thanks to all

BSKg

I don't think the array way you showed works, bitboards is kindof a hack more than a feature of c++, that's why it's awkward and why it's efficient. If you want to update a bitboard you must use bitwise and/or like I showed you. What kind of update did you have in mind? Moving a piece for example?

theturk1234 wrote:

I do not know hexadecimal form so well--does what you said mean that every time I update (after each turn, for example) the bitboards I have to tell the compiler in hex form? Or can I do it visually in 8x8 array of xeroes like  showed before? If so, how?

Thanks to all

theturk1234

Yes, exactly as you said--moving a piece for example. What I meant is: the hex form is so diffucult for humans to see-- the computer has no trouble converting it into an 8x8 array. I do have a nice utility specially for building bitboards that lets you manually change the bits on a board laid out in a 8x8 array, and when you are done, it converts the 8x8 array into a hex number. I could do this, but it would take awhile. On wikispaces there are nice 8x8 arrays that you can visually sink your teeth into, but does what you said mean that I have to use the hex form instead of something like this:

00000000

00000000

00000000

00000000

00000000

00000000

00000000

00000000

 

This is an empty board. If I want to add the white pieces I thought I could do it like this:

00000000

00000000

00000000

00000000

00000000

00000000

11111111

11111111

 

Can  I do this? If so, how?

This is sort of my main question--the reason I want to use bitboards is that it is very easy to AND and OR them and it is easy to lay out the 8x8 array of numbers. Do I really have to tell the compiler what I want in hex?

It is so easy the other way.

 

As always, thanks

rtr1129

You either need to use hex or make a function that will give you the bit for a given square.

Here is a bitboard helper that will give you the hex values.

http://cinnamonchess.altervista.org/bitboard_calculator/Calc.html