Hello Ma Chao,
ok, you are talking of an attack getter function.
What you described for k1 and k2 are the blockers
along a line (and in your case a diagonal).
Because i was very improper yesterday, alone
with the formular 2*k2 - k1 (sorry for that),
which was meant of course as 2*onebit(k2) - onebit(k1),
i will take the time now to describe an algorithm that
may handle your task. ( Describing not with every minor detail,
but enough to understand the ideas at first glance).
so, before i continue some terminologies.
1a:
===
bitboard : 64-bit number
most significant bit : msb (i will use M , your k2)
least significant bit: lsb (i will use L , your k1)
square : i will use S , your k )
1b:
===
The reason why i use "S" and not "piece" is, that you
can compute attacks from any squares you like, doesnt
matter if it is occupied by a piece or not.
msb,lsb are the obstruction squares including real blockers (pieces)
or artificial obstructions.
1c:
===
it is easier to present the 64bit number as bitboard,
to follow what is happening, like...
Code: Select all
00000000 56...63 (63 == h8)
00000000 48...55
00000000 40...47
00000000 32...39
00000000 24...31
00000000 16...23
00000000 8...15
00000000 0...7 ( 0 == a1)
2a:
===
let us say you have a diagaonal occupied like...
Code: Select all
00000001 56...63 (63 == h8)
00000010 48...55
00000000 40...47
00000000 32...39
000S0000 24...31
00000000 16...23
01000000 8...15
10000000 0...7 ( 0 == a1)
00000001 56...63 (63 == h8)
000000M0 48...55
00000000 40...47
00000000 32...39
000S0000 24...31
00000000 16...23
0L000000 8...15
10000000 0...7 ( 0 == a1)
To get M,L by bitscan you first have to get rid
of the outer bits (in this case a1,h8)
How to get L:
--------------
Code: Select all
hiBits: AllBitsSet << x;
xxxxxxxx 56...63 (63 == h8)
xxxxxxxx 48...55
xxxxxxxx 40...47
xxxxxxxx 32...39
000Sxxxx 24...31
00000000 16...23
0L000000 8...15
10000000 0...7 ( 0 == a1)
if you clear the bits marked as x and S, there
wont be a bit anymore set above L, so L becomes
a msb which you can simply compute with reverse
bitscan (bsr)
How to get M:
--------------
Code: Select all
loBits: onebit(x) - 1;
00000001 56...63 (63 == h8)
000000M0 48...55
00000000 40...47
00000000 32...39
000Sxxxx 24...31
xxxxxxxx 16...23
xxxxxxxx 8...15
xxxxxxxx 0...7 ( 0 == a1)
if you clear the bits marked as x and S, there
wont be a bit anymore set below M, so M becomes
a lsb which you can simply compute with forward
bitscan (bsf)
Hint:
-----
Of course you can compute loBits,hiBits on the
fly with the given formulars, but you can also
precompute these masks for each square.
Second, make sure you also exclude S (the square itself)
to get L,M with the described method.
3:
====
Now we have L,M (k1,k2). All we need to do now is to
connect k2 and k1 along a line.
(diagonal,rank,file, doesnt matter
)
we have...
Code: Select all
00000000 56...63 (63 == h8)
000000M0 48...55
00000000 40...47
00000000 32...39
00000000 24...31
00000000 16...23
0L000000 8...15
00000000 0...7 ( 0 == a1)
note: S is excluded to connect M,L
onebit(M) - onebit(L) does a connection (not including M)
2 * onebit(M) - onebit(L) does a connection ( including M)
00000000 56...63 (63 == h8)
111111M0 48...55
11111111 40...47
11111111 32...39
11111111 24...31
11111111 16...23
1L000000 8...15
00000000 0...7 ( 0 == a1)
Mask the result with your line (diagonal in your case),
exclude the square itself and it is done.
Code: Select all
00000000 56...63 (63 == h8)
00000010 48...55
00000100 40...47
00001000 32...39
00000000 24...31
00100000 16...23
01000000 8...15
00000000 0...7 ( 0 == a1)
4:
===
So, here just an impression of the complexity
how it can look like finally (using some minor details ... )
Code: Select all
inline ui64_t attackLine(linemask_t *lmsk,ui64_t occ)
{
ui64_t lo = onebit(bsr64((lmsk->lineLo & occ)|1));
ui64_t hi = lmsk->lineHi & occ;
return(lmsk->lineEx & (2*(hi&-hi)-lo));
}
5:
===
The algorithm i described is named Obstruction Difference.
There are some minor details left out, but i think it is enough
to understand the idea.
if you need code you can pm me.
Hope this time i could help
Best,
Michael