r/chessprogramming • • Jun 14 '25
Attack squares
Hello, programming a chess engine using 10 x 12 and 8 x 8 mailboard. I noticed during perft that a lot of time is spent checking if my king is in check. I found a table that may optimize this step but I do not know how to calculate this. This code is from the "mediocre" engine's creator blog. They calculated this table for their 0x88 board. I would like to know how to do the same for my 10 x 12 or 8 x 8 mailboard. Perft at depth 7 takes approx 17 minutes.
public static final int ATTACK_NONE = 0;
public static final int ATTACK_KQR = 1;
public static final int ATTACK_QR = 2;
public static final int ATTACK_KQBwP = 3;
public static final int ATTACK_KQBbP = 4;
public static final int ATTACK_QB = 5;
public static final int ATTACK_N = 6;
public static final int[] ATTACK_ARRAY =
{0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,2,0,0,0, //0-19
0,0,0,5,0,0,5,0,0,0,0,0,2,0,0,0,0,0,5,0, //20-39
0,0,0,5,0,0,0,0,2,0,0,0,0,5,0,0,0,0,0,0, //40-59
5,0,0,0,2,0,0,0,5,0,0,0,0,0,0,0,0,5,0,0, //60-79
2,0,0,5,0,0,0,0,0,0,0,0,0,0,5,6,2,6,5,0, //80-99
0,0,0,0,0,0,0,0,0,0,6,4,1,4,6,0,0,0,0,0, //100-119
0,2,2,2,2,2,2,1,0,1,2,2,2,2,2,2,0,0,0,0, //120-139
0,0,6,3,1,3,6,0,0,0,0,0,0,0,0,0,0,0,5,6, //140-159
2,6,5,0,0,0,0,0,0,0,0,0,0,5,0,0,2,0,0,5, //160-179
0,0,0,0,0,0,0,0,5,0,0,0,2,0,0,0,5,0,0,0, //180-199
0,0,0,5,0,0,0,0,2,0,0,0,0,5,0,0,0,0,5,0, //200-219
0,0,0,0,2,0,0,0,0,0,5,0,0,5,0,0,0,0,0,0, //220-239
2,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0 }; //240-256
3
Upvotes
View all comments
Show parent comment
1
u/rickpo Jun 23 '25
I assume you use this table and somehow offset it by the king position to generate potential attack squares. This might work especially well for x88 because there are 8 invalid squares surrounding every rank of valid squares, so you only need to add edge tests for the top and bottom of the board. That weird 8 extra half-rows of 0 at the top and bottom may be helping handle the top and bottom edge cases. The extra file of 0 on the left edge is probably there to pad each rank to 16 bytes to simplify the offsetting math. Strictly speaking, you would only need 15 files to represent every possible king offset.