Calculating TO

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

grant
Posts: 67
Joined: Mon Aug 06, 2007 4:42 pm
Location: London, England

Calculating TO

Post by grant »

Hi

If my rook can check the king, then by calculation, the TO square is

(oppKing >> 3) + (from & 7) and/or
(from >> 3) + (oppKing & 7)

If my bishop can check the king, is there a formula for calculating the TO squares?

Grant
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Calculating TO

Post by Sven »

What exactly do you mean by "TO square": the square from which the rook can give check to the king?

And what is your board representation (it does not quite look like "0x88")?

Sven
grant
Posts: 67
Joined: Mon Aug 06, 2007 4:42 pm
Location: London, England

Re: Calculating TO

Post by grant »

Sven

The TO square means the square/s that the rook moves to to check the king.
My board representation is A8=0, B8=1 ... H1=63, and I have a bitboard engine.

I just thought of trying a quick calculation to get my TO square in my generate checks function, which also does not require magics.

Grant
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Calculating TO

Post by Sven »

In case of bitboards, why don't you do an intersection (using precalculated tables) of all_bishop_targets[from] & all_bishop_targets[opp_king] that results in up to two squares, provided the bishop is not on the same diagonal as the opp_king already?

Same for rook: all_rook_targets[from] & all_rook_targets[opp_king], although I don't know whether the formula you gave wouldn't be slightly faster in the rook case.

For queen the same but there you may get up to four squares.

Sven
User avatar
Zach Wegner
Posts: 1922
Joined: Thu Mar 09, 2006 12:51 am
Location: Earth

Re: Calculating TO

Post by Zach Wegner »

Sven Schüle wrote:For queen the same but there you may get up to four squares.
That's actually 12.
Edmund
Posts: 670
Joined: Mon Dec 03, 2007 3:01 pm
Location: Barcelona, Spain

Re: Calculating TO

Post by Edmund »

grant wrote:Sven

The TO square means the square/s that the rook moves to to check the king.
My board representation is A8=0, B8=1 ... H1=63, and I have a bitboard engine.

I just thought of trying a quick calculation to get my TO square in my generate checks function, which also does not require magics.

Grant
assuming the definition:
#define getDIA(sq) (7 - ((sq)&7) + ((sq)>>3))
#define getADIA(sq) (((sq)&7) + ((sq)>>3))

now getting the intersection of these two values is the problem:
one possible solution would be:
adia+3.5*(adia+dia-7)

but sofar I didn't come up with a solution to find out whether the two are intersecting at all
Edmund
Posts: 670
Joined: Mon Dec 03, 2007 3:01 pm
Location: Barcelona, Spain

Re: Calculating TO

Post by Edmund »

Edmund wrote:but sofar I didn't come up with a solution to find out whether the two are intersecting at all
to test whether they are intersecting:
(dia + adia - 7) = even
and
(dia + adia - 7) / 2 <= dia


maybe you can find an easier way Grant :)
grant
Posts: 67
Joined: Mon Aug 06, 2007 4:42 pm
Location: London, England

Re: Calculating TO

Post by grant »

Sven

That is exactly what I was trying to avoid.

For rooks giving check, I simply lookup the bitboard containing all the squares the rook has to pass over to capture the king (if you see what I mean), and AND it with ALLOccupied. If the result is zero, a non-capture check is possible and the TO square can be calculated (as above).

Same with bishops but at the moment I have to use intersecting bitboards to get my TO squares.

Grant
Edmund
Posts: 670
Joined: Mon Dec 03, 2007 3:01 pm
Location: Barcelona, Spain

Re: Calculating TO

Post by Edmund »

so in short:
TO = (9*(sqA>>3 + sqA&7)+7*(sqB>>3 - sqB&7))/2

but be aware that TO has to be checked for 0 <= TO <= 63
and sqA and sqB must be on equal colored squares ((sqA+sqB) & 1) == 0
grant
Posts: 67
Joined: Mon Aug 06, 2007 4:42 pm
Location: London, England

Re: Calculating TO

Post by grant »

Thanks Edmund

The rook calculation above should have read

(sqA & 56) + (sqB & 7) and/or
((sqB & 56) + (sqA & 7)

Oops.

Grant