Jump to content

  • Log In with Google      Sign In   
  • Create Account

Checking for offboard squares with bitboards (vs padded array)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 patishi   Members   -  Reputation: 212

Like
0Likes
Like

Posted 07 December 2013 - 01:02 PM

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


Edited by patishi, 07 December 2013 - 01:47 PM.


Sponsor:

#2 ferrous   Members   -  Reputation: 2007

Like
0Likes
Like

Posted 08 December 2013 - 07:00 PM

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.



#3 Álvaro   Crossbones+   -  Reputation: 13317

Like
1Likes
Like

Posted 08 December 2013 - 07:19 PM

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.



#4 patishi   Members   -  Reputation: 212

Like
0Likes
Like

Posted 08 December 2013 - 10:51 PM

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

#5 Álvaro   Crossbones+   -  Reputation: 13317

Like
0Likes
Like

Posted 09 December 2013 - 03:19 AM

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.

#6 patishi   Members   -  Reputation: 212

Like
0Likes
Like

Posted 09 December 2013 - 04:23 AM

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?

#7 Álvaro   Crossbones+   -  Reputation: 13317

Like
0Likes
Like

Posted 09 December 2013 - 05:07 AM

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.

#8 patishi   Members   -  Reputation: 212

Like
0Likes
Like

Posted 09 December 2013 - 05:55 AM

Thank you very much my friend ! Very appreciated :)

#9 Logic Crazy   Members   -  Reputation: 104

Like
0Likes
Like

Posted 17 January 2014 - 11:18 AM

     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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS