Good examples for magic or rotated boards

Code, algorithms, languages, construction...
Post Reply
jashwant
Posts: 4
Joined: Mon Oct 25, 2010 5:44 pm
Real Name: Jashwant

Good examples for magic or rotated boards

Post by jashwant » Tue Oct 26, 2010 4:39 am

Hi,all.
I am a newbie..II've gone through http://chessprogramming.wikispaces.com and http://www.cis.uab.edu/hyatt/pubs.html and many other programming tutorials. They all cover most of the theoretical part but none of them have good pictorial examples. I am perhaps not a good learner or programmer but I want to know about this.... I have developed a small chess program between two player with 8*8 board but soon realised this would not be efficient for AI. I am switching to bitboards. I've got basic idea of bitboards (pawn,king,knight moves) but rotating bitboards or magic boards are too high for me :( . Can someone provide me a programming logic to build moves for bishop/rook (from rotated or magic boards ) ??? It will be very helpful if someone can teach magic boards as rotating boards are still seems as I can learn them :)

RobertP
Posts: 8
Joined: Tue Jun 22, 2010 8:36 am
Real Name: Robert Purves
Location: New Zealand

Re: Good examples for magic or rotated boards

Post by RobertP » Tue Oct 26, 2010 8:47 am

The easiest bitboard slider-attacks code to write is the standard or classical method:
http://chessprogramming.wikispaces.com/ ... l+Approach

Here's an implementation of RookAttacks().

Code: Select all

extern BitBoard gRayN[64], gRayE[64], gRayS[64], gRayW[64];

BitBoard RookAttacks( int fromSquare, BitBoard occupied ) 
{ 
	int        blockingSquare; 
	BitBoard   attacks, partAttacks, blockers; 
	
	attacks = gRayN[fromSquare]; 
	blockers = attacks & occupied; 
	if ( blockers ) 
	{ 
		blockingSquare = GetLowestBit( blockers ); 
		attacks ^= gRayN[blockingSquare]; // mask off beyond blocking square
	} 
	
	partAttacks = gRayE[fromSquare]; 
	blockers = partAttacks & occupied; 
	if ( blockers ) 
	{ 
		blockingSquare = GetLowestBit( blockers ); 
		partAttacks ^= gRayE[blockingSquare]; 
	} 
	attacks |= partAttacks; 
	
	partAttacks = gRayS[fromSquare]; 
	blockers = partAttacks & occupied; 
	if ( blockers ) 
	{ 
		blockingSquare = GetHighestBit( blockers ); 
		partAttacks ^= gRayS[blockingSquare]; 
	} 
	attacks |= partAttacks; 
	
	partAttacks = gRayW[fromSquare]; 
	blockers = partAttacks & occupied; 
	if ( blockers ) 
	{ 
		blockingSquare = GetHighestBit( blockers ); 
		partAttacks ^= gRayW[blockingSquare]; 
	} 

	return attacks | partAttacks; 
}  
BishopAttacks() is similar, with the obvious substitutions (gRayNW[], gRayNE[], gRaySE[], gRaySW[]).
These functions are quite easy to understand, which is important while you get used to bitboard programming. They will be much faster than your existing slider-attack code, which no doubt has loops.
At some later time you may want to try magic, as a minor optimization.

Robert P.

jashwant
Posts: 4
Joined: Mon Oct 25, 2010 5:44 pm
Real Name: Jashwant

Re: Good examples for magic or rotated boards

Post by jashwant » Wed Oct 27, 2010 5:27 pm

Nice approach..At this time, I dont want any optimizations. First I want to complete my initial work.Then, I'll go for optimizations.

Thanks a lot for this approach.Made my way...and many thanks to your implementation :)

BB+
Posts: 1484
Joined: Thu Jun 10, 2010 4:26 am

Re: Good examples for magic or rotated boards

Post by BB+ » Wed Nov 24, 2010 8:18 pm

There was a comparative analysis of bitboard methods by Harald Lüßen a few years back. http://www.talkchess.com/forum/viewtopi ... 11&t=16002
As you can see, there are a number of different methods. For each method, there is a later post that gives the actual code. He notes that your mileage way vary, as all comparison were with his Elephant engine.

At the same time, code recycling happens a lot here. For instance, Bob chooses to adapt Pradu's code in Crafty, and VR does the same in Rybka 4 (both of these seem to be unacknowledged in the respective distributions, though Bob has mentioned his usage on various occasions -- Pradu simply says: "If you use this code in a product, an acknowledgment in the product documentation would be appreciated but is not required"), with the exact same magic multipliers, order of squares in the arrays, etc.

hyatt
Posts: 1242
Joined: Thu Jun 10, 2010 2:13 am
Real Name: Bob Hyatt (Robert M. Hyatt)
Location: University of Alabama at Birmingham
Contact:

Re: Good examples for magic or rotated boards

Post by hyatt » Thu Dec 02, 2010 6:48 am

I'm glad you posted that. I am not sure why there was no reference to Pradu's work in Crafty's changelog in the comments. Probably because I had mentioned his name every time I mentioned my conversion to magic bitboards. In any case, I fixed this as I have always tried to cite anyone that provided code I used inside Crafty...

Pradu visited UAB for our first ACCA event at UAB (He was at Ga Tech I think) so I had the opportunity to discuss the magic stuff at length with him. And enjoyed the visit...

zwegner
Posts: 57
Joined: Thu Jun 10, 2010 5:38 am

Re: Good examples for magic or rotated boards

Post by zwegner » Thu Dec 02, 2010 9:56 pm

BB+ wrote:There was a comparative analysis of bitboard methods by Harald Lüßen a few years back. http://www.talkchess.com/forum/viewtopi ... 11&t=16002
As you can see, there are a number of different methods. For each method, there is a later post that gives the actual code. He notes that your mileage way vary, as all comparison were with his Elephant engine.

At the same time, code recycling happens a lot here. For instance, Bob chooses to adapt Pradu's code in Crafty, and VR does the same in Rybka 4 (both of these seem to be unacknowledged in the respective distributions, though Bob has mentioned his usage on various occasions -- Pradu simply says: "If you use this code in a product, an acknowledgment in the product documentation would be appreciated but is not required"), with the exact same magic multipliers, order of squares in the arrays, etc.
Still no response to this?

I'd think the news that Vas is using open source code in Rybka 4 without acknowledgement would cause a bit of commotion. :)

Jeremy Bernstein
Site Admin
Posts: 1226
Joined: Wed Jun 09, 2010 7:49 am
Real Name: Jeremy Bernstein
Location: Berlin, Germany
Contact:

Re: Good examples for magic or rotated boards

Post by Jeremy Bernstein » Fri Dec 03, 2010 12:23 am

zwegner wrote:
BB+ wrote:There was a comparative analysis of bitboard methods by Harald Lüßen a few years back. http://www.talkchess.com/forum/viewtopi ... 11&t=16002
As you can see, there are a number of different methods. For each method, there is a later post that gives the actual code. He notes that your mileage way vary, as all comparison were with his Elephant engine.

At the same time, code recycling happens a lot here. For instance, Bob chooses to adapt Pradu's code in Crafty, and VR does the same in Rybka 4 (both of these seem to be unacknowledged in the respective distributions, though Bob has mentioned his usage on various occasions -- Pradu simply says: "If you use this code in a product, an acknowledgment in the product documentation would be appreciated but is not required"), with the exact same magic multipliers, order of squares in the arrays, etc.
Still no response to this?

I'd think the news that Vas is using open source code in Rybka 4 without acknowledgement would cause a bit of commotion. :)
"If you use this code in a product, an acknowledgment in the product documentation would be appreciated but is not required". Vas isn't breaking any license agreements, if he's using this code. Might be a little rude, but that's bidnis.

Jeremy

BB+
Posts: 1484
Joined: Thu Jun 10, 2010 4:26 am

Re: Good examples for magic or rotated boards

Post by BB+ » Fri Dec 03, 2010 2:45 am

I'd think the news that Vas is using open source code in Rybka 4 without acknowledgement would cause a bit of commotion. :)
Well, Bob is technically doing the same, as I don't find a mention of Pradu (and/or Buzz) in the Crafty distribution. Pradu is also quite clear that it is permissible. It also seems (to me) that one really can't use Pradu's code "as a library" as it were [for instance, compile it separately and link in via an API, more like Nalimov] --- you have to hack it up to fit your needs. This said, it can't be that difficult to write independent magic multiplier code once the idea is known (and Tord has provided code to generate the mults if you need help with that); perhaps using Pradu's numbers could be nice at first if debugging, but I don't even think they are the "best known" values any more [though that can't really matter too much].

Post Reply