makefile
[arrocco.git] / bits.py
blob47a86a5ac9ae5973614d358b58f77f74521abab0
1 # bits.py
2 # 1 Mar 2007
3 # Make bitfields for piece moves.
5 """
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/>.
21 """
23 def bitsForKing(sq):
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):
37 L = [sq + direc * 8]
38 if sq // 8 == firstRow:
39 L.insert(0, sq + direc * 16)
40 return L
42 def bitsForPawnCapture(sq, direc):
43 r, c = divmod(sq, 8)
44 L = []
45 if c > 0: L.append((r+direc) * 8 + (c - 1))
46 if c < 7: L.append((r+direc) * 8 + (c + 1))
47 return L
49 def bitsForHorizontalRookDests(sq, occupancy):
50 r, c1 = divmod(sq, 8)
51 occupiedColumns = [c for c in range(8) if occupancy & (1 << c)]
52 for c2 in range(c1 + 1, 8):
53 yield 8 * r + c2
54 if c2 in occupiedColumns:
55 break
56 for c2 in range(c1 - 1, -1, -1):
57 yield 8 * r + c2
58 if c2 in occupiedColumns:
59 break
61 def bitsForVerticalRookDests(sq, occupancy):
62 r1, c = divmod(sq, 8)
63 occupiedRows = [r for r in range(8) if occupancy & (1 << r)]
64 for r2 in range(r1 + 1, 8):
65 yield 8 * r2 + c
66 if r2 in occupiedRows:
67 break
68 for r2 in range(r1 - 1, -1, -1):
69 yield 8 * r2 + c
70 if r2 in occupiedRows:
71 break