9

I am writing a C++ chess engine and I am looking for magic numbers for Little-Endian Rank-File Mapping bitboards to generate moves for sliding pieces.

The rival chess website gives magic numbers but not for the same board mapping.

The chess programming wiki gives some of the best magic numbers so far but is not exhaustive.

At the end I am looking for four things:

  • occupancy mask for each square
  • magic number for each square
  • magic shifts for each square
  • moves database array for each square

So I can use the following code to find the moves of the Rook on C3 (for instance):

bbBlockers = bbAllPieces & occupancyMaskRook[C3]

databaseIndex = (int)((bbBlockers * magicNumberRook[C3]) >> rookMagicShifts[C3])

bbMoveSquares = magicMovesRook[C3][databaseIndex] & ~bbFriendlyPieces
Brian Towers
  • 96,800
  • 11
  • 239
  • 397
Romain
  • 667
  • 8
  • 15

1 Answers1

9

This is a very well known problem in chess programming. You should consider use the numbers generated by Pradyumna Kannan. Dr Kannan had kindly produced open-source the magic numbers. It is being used by Crafty and a few other chess engines including my own.

You can read more by google "Crafty magic number".

I've prepared a zipped file for you here. This is the same files being used in the SmallChess chess engine app. Please read the documentation. Basically, you'll need to call Rmagic(square, occupancy) for rooks and Bmagic(square, occupancy) for bishops. You can XOR those into a queen mask.

SmallChess
  • 22,476
  • 2
  • 45
  • 82
  • Thanks it works well. Can I freely use/modify this code in my chess engine? – Romain May 08 '15 at 19:26
  • There is no licence restriction. Use as you pleased. It wasn't me who did it, it was a contribution by Dr Kannan. Please accept my answer if it helps! – SmallChess May 09 '15 at 00:44
  • Hi, can you upload the file MagicMoves.zip to GoogleDrive? I can't download it at smallchess (I got some error). Thanks. – 123iamking Mar 23 '20 at 04:59
  • @123iamking The link is still working. I am not going to remove it. – SmallChess Mar 23 '20 at 04:59
  • @SmallChess - I don't ask about removing it, can you provide a mirror with Google Drive? Thanks. – 123iamking Mar 23 '20 at 09:12
  • @SmallChess Have you written the code in the zip file? –  Oct 31 '20 at 09:02
  • @AryanParekh It wasn't me who wrote it. – SmallChess Oct 31 '20 at 13:40
  • @SmallChess I need some help, regarding the magic bitboards, can you help me? –  Oct 31 '20 at 16:28
  • @AryanParekh Please post a new question. We can't provide individual level help here. – SmallChess Oct 31 '20 at 17:50
  • @SmallChess I understand! Its something that would be better suited in a chat room rather than a new question :) –  Oct 31 '20 at 18:18
  • @AryanParekh stackoverflow is a free service for all of us. I am not paid for offering expert level advice. I can’t help you one to one. Please start a new question so everybody can help. – SmallChess Oct 31 '20 at 19:50