To number 1:
You can do that with a table lookup. Just create and array[256] indexed by
targetSq - fromSq + 128. Then you store the direction of travel there.
Code: Select all
i.e. assuming that for 0x88
a1 ... a8 : 0 1 2 3 4 5 6 7
h1 ... h8 : 112 113 114 115 116 117 118 119
a1 -h8 : targetSq (119) - fromSq (0) + 128 = 247
so table[247] = 17
h8 - a1: 0 - 119 + 128 = 9
table[9] = -17
etc.
With this, you get the following table:
// vector of movement for captures targetSQ - fromSQ + 128
const signed char attackstep[256] = {0, 0, 0, 0, 0, 0, 0, 0, // 0 - 7
0,-17, 0, 0, 0, 0, 0, 0,-16, 0, 0, 0, 0, 0, 0,-15, // 8 - 23
0, 0,-17, 0, 0, 0, 0, 0,-16, 0, 0, 0, 0, 0,-15, 0, // 24 - 39
0, 0, 0,-17, 0, 0, 0, 0,-16, 0, 0, 0, 0,-15, 0, 0, // 40
0, 0, 0, 0,-17, 0, 0, 0,-16, 0, 0, 0,-15, 0, 0, 0, // 56
0, 0, 0, 0, 0,-17, 0, 0,-16, 0, 0,-15, 0, 0, 0, 0, // 72
0, 0, 0, 0, 0, 0,-17,-33,-16,-31,-15, 0, 0, 0, 0, 0, // 88
0, 0, 0, 0, 0, 0,-18,-17,-16,-15,-14, 0, 0, 0, 0, 0, // 104
0 ,-1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1, 1, 1, // 120
0, 0, 0, 0, 0, 0, 14, 15, 16, 17, 18, 0, 0, 0, 0, 0, // 136
0, 0, 0, 0, 0, 0, 15, 31, 16, 33, 17, 0, 0, 0, 0, 0, // 152
0, 0, 0, 0, 0, 15, 0, 0, 16, 0, 0, 17, 0, 0, 0, 0, // 168
0, 0, 0, 0, 15, 0, 0, 0, 16, 0, 0, 0, 17, 0, 0, 0, // 184
0, 0, 0, 15, 0, 0, 0, 0, 16, 0, 0, 0, 0, 17, 0, 0, // 200
0, 0, 15, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 17, 0, // 216
0, 15, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 17, // 232 - 247
0, 0, 0, 0, 0, 0, 0, 0 }; // 248 - 255
Now you can the look up
int track = targetsq-fromsq+128;
direction_of_travel=attackstep[track];
You can also do an additional table attackmove[track] that contains the bits set of the pieces that can reach a targetsq from a fromsq by storeing bitcombinations in it:
Code: Select all
king = 100000 = 32
queen = 010000 = 16
rook = 001000 = 8
bishop = 000100 = 4
knight = 000010 = 2
whitepawn = 000001 = 1
blackpawn = 1000000 = 64
example:
--------
pieces that can capture to +1 = K/R/Q = 111000 = 32+16+8 = 56
const char attackmove[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0,20, 0, 0, 0, 0, 0, 0, // 0 - 15
24, 0, 0, 0, 0, 0, 0,20, 0, 0,20, 0, 0, 0, 0, 0, // 16 - 31
24, 0, 0, 0, 0, 0,20, 0, 0, 0, 0,20, 0, 0, 0, 0, // 32 - 47
24, 0, 0, 0, 0,20, 0, 0, 0, 0, 0, 0,20, 0, 0, 0, // 48 - 63
24, 0, 0, 0,20, 0, 0, 0, 0, 0, 0, 0, 0,20, 0, 0, // 64 - 79
24, 0, 0,20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,20, 2, // 80 - 95
24, 2,20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,116, // 96 - 111
56,116, 2, 0, 0, 0, 0, 0, 0,24,24,24,24,24,24,56, // 112 - 127
0,56,24,24,24,24,24,24, 0, 0, 0, 0, 0, 0, 2,53, // 128 - 143
56,53, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,20, 2, // 144 - 159
24, 2,20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,20, 0, 0, // 160 - 175
24, 0, 0,20, 0, 0, 0, 0, 0, 0, 0, 0,20, 0, 0, 0, // 176 - 191
24, 0, 0, 0,20, 0, 0, 0, 0, 0, 0,20, 0, 0, 0, 0, // 192 - 207
24, 0, 0, 0, 0,20, 0, 0, 0, 0,20, 0, 0, 0, 0, 0, // 208 - 223
24, 0, 0, 0, 0, 0,20, 0, 0,20, 0, 0, 0, 0, 0, 0, // 224 - 239
24, 0, 0, 0, 0, 0, 0,20, 0, 0, 0, 0, 0, 0, 0, 0 }; // 240 - 255
an then you can probe with
if(attackmove[track] & piece) == piece
thus, to simply see if piece on square fromSq can reach toSq I used the following function in 0x88:
Code: Select all
const char fig_table[13] = {32, 16, 8, 4, 2, 64, 0, 1, 2, 4, 8, 16, 32};
// sliding pieces
const char sliding_mask[13] = {0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0};
bool can_reach(const position* ppos, const int fromSq, const int targetSq)
{
int way = targetSq - fromSq + 128;
int piece = fig_table[ppos->board[fromSq]+6];
int step = attackstep[way];
if ((attackmove[way] & piece) == piece)
{
int iFrom = fromSq;
for (int i = 0; i < 8; i++)
{
iFrom += step;
if (iFrom == targetSq)
{
// Made it to the target
return true;
}
if (ppos->board[iFrom] != 0)
return false; // path is blocked
}
}
return false;
}