3 # Make bitfields for piece moves.
6 This file is part of Arrocco, which is Copyright 2007 Thomas Plick
7 (tomplick 'at' gmail.com).
9 Arrocco is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 Arrocco is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 r1
, c1
= divmod(sq
, 8)
25 return [i
for i
in range(64) for (r2
, c2
) in [divmod(i
, 8)]
26 if max(abs(r1
-r2
), abs(c1
-c2
)) == 1]
28 def bitsForKnight(sq
):
29 r1
, c1
= divmod(sq
, 8)
30 return [i
for i
in range(64) for (r2
, c2
) in [divmod(i
, 8)]
31 if abs(r1
-r2
) * abs(c1
-c2
) == 2]
33 def bitfieldForBits(bits
):
34 return sum(2**i
for i
in bits
if i
>= 0 and i
< 64)
36 def bitsForPawnMove(sq
, direc
, firstRow
):
38 if sq
// 8 == firstRow
:
39 L
.insert(0, sq
+ direc
* 16)
42 def bitsForPawnCapture(sq
, direc
):
45 if c
> 0: L
.append((r
+direc
) * 8 + (c
- 1))
46 if c
< 7: L
.append((r
+direc
) * 8 + (c
+ 1))
49 def bitsForHorizontalRookDests(sq
, occupancy
):
51 occupiedColumns
= [c
for c
in range(8) if occupancy
& (1 << c
)]
52 for c2
in range(c1
+ 1, 8):
54 if c2
in occupiedColumns
:
56 for c2
in range(c1
- 1, -1, -1):
58 if c2
in occupiedColumns
:
61 def bitsForVerticalRookDests(sq
, occupancy
):
63 occupiedRows
= [r
for r
in range(8) if occupancy
& (1 << r
)]
64 for r2
in range(r1
+ 1, 8):
66 if r2
in occupiedRows
:
68 for r2
in range(r1
- 1, -1, -1):
70 if r2
in occupiedRows
: