Checking for offboard squares with bitboards (vs padded array)

Started by
7 comments, last by Logic Crazy 10 years, 2 months ago

I am currently starting to work on a chess engine.

With padded array it is easy to check for offboard squares, but what if I want to use bitboards to represent the board, how should I make this validation? should I keep a padded array (along with the bitboards) for those checks or there are maybe another more effiecient / bitboard oriented solutions ?

Thx

Advertisement

Ah, to be honest, I had to look up what you wanted:

http://www.cis.uab.edu/hyatt/boardrep.html

Seems to answer a lot of questions related to board layouts.

But honestly, I think at this point in computing, it's over optimizing to even worry about whether to pad your chessboard with offboard squares or not. A simple function or macro that is something like IsValid(x,y) will abstract it away, and you can just use whatever is most straightforward and most expedient for you at the moment. Unless of course you want to rival Deep Blue or whatever the reigning king is and want to get super fast chess computation.

I have written chess engines before, and I don't think it's over-optimizing. Keeping your code board-representation-agnostic using macros is not very realistic, because many of the algorithms you will use (Static Exchange Evaluation, repetition detection, what terms to use in the evaluation function...) and other data descriptions (moves, transposition-table entries...) will be heavily affected by the choice of representation. If you want to change your board representation after you have written an engine, you will be better off starting from scratch.

Bitboards are great, but they require a shift in how you think about a lot of things. In particular, being outside the board is not a very meaningful concept. For the most part, you don't handle squares one at a time when using bitboards: The basic data structure is a subset of the squares, and operations like moving North move all of them. Some of the bits that were set before moving North will seem to have disappeared, and that's about all there is to it.

 typedef unsigned long long u64;
  
  u64 North(u64 x) {
    return x << 8;
  }

If you have a specific example of code where you feel you need to check if a square is outside the board, post some details about what you are doing and I'll try to help you express it with bitboards.

Thx for the responses.
Alvaro, I read your comment but i'm having a little hard time to understand it completely.

Let's say the white queen is on A8,now it can't move north anymore cause there is no more board left. I need to make some kind of validation..
If I try translate ir to bitboard terms, A8 is the 57th bit, and if I do <<8 I will get strange results.
what am missing here??

Please bare with me , like you said..thinking OUTSIDE of the board requires a change in how you look at things.

Thx

Let's say the white queen is on A8,now it can't move north anymore cause there is no more board left. I need to make some kind of validation..
If I try translate ir to bitboard terms, A8 is the 57th bit, and if I do <<8 I will get strange results.
what am missing here??


No strange results: You'll get 0, which means the queen is nowhere on the board.

More importantly, why are you moving North from A8? Are you trying to generate moves? The way you would normally generate moves using bitboards doesn't require this operation at all.
I was planning to loop through each piece in every direction and check for available moves until i hit the edge of the board.. but i guess that i can simply get the available squares by means of bit operations on the relevant bitboards?
Move generation with bitboards is normally not done that way. Let's concentrate on sliding pieces (all the others are much easier). You pick one piece at a time, you compute a bitboard of all the places where it could go and then generate moves to go to each of those places. Why would you want to do it this way? Because the majority of the time in the search tree is spent in the quiescence search, where you mostly need to generate only captures. If you have a bitboard of the places where the piece could go, you can bitwise-AND it with the enemy pieces and get your captures, without having wasted any time on non-captures.

How do you compute the places where the piece might go? There are several ways to do it. The one I have implemented is called magic bitboards.
Thank you very much my friend ! Very appreciated :)

You do not need to have an array in addition to bitboards (although you might if you have difficulties understanding/debugging bitboards). I would recommend the following YouTube step-by-step tutorials which explain both why and how to efficiently deal with bitboards using current industry standards:

Bitboards (The Concept of) - Advanced Java Chess Engine Tutorial 1

Generating Bitboards - Advanced Java Chess Engine Tutorial 2

Chess960 and Favorite Links - Advanced Java Chess Engine Tutorial 3

Bitwise Operations - Advanced Java Chess Engine Tutorial 4

Pawn Moves & Optimization (Part 1) - Advanced Java Chess Engine Tutorial 5

Pawn Moves & Optimization (Part 2) - Advanced Java Chess Engine Tutorial 6

En Passants - Advanced Java Chess Engine Tutorial 7

Sliding Pieces (Part 1) - Advanced Java Chess Engine Tutorial 8

Sliding Pieces (Part 2) - Advanced Java Chess Engine Tutorial 9

Sliding Pieces (Part 3) - Advanced Java Chess Engine Tutorial 10

Knight Moves - Advanced Java Chess Engine Tutorial 11

King Movement & Safety - Advanced Java Chess Engine Tutorial 12

This topic is closed to new replies.

Advertisement