1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// This file is part of the shakmaty library.
// Copyright (C) 2017-2019 Niklas Fiekas <niklas.fiekas@backscattering.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::fmt;
use std::fmt::Write;
use std::iter::FromIterator;

use crate::attacks;
use crate::bitboard::Bitboard;
use crate::square::{File, Rank, Square};
use crate::types::{Color, Piece, Role};
use crate::material::{Material, MaterialSide};

/// [`Piece`] positions on a board.
///
/// # Examples
///
/// ```
/// # use shakmaty::{Square, Board};
/// # use shakmaty::Color::Black;
/// let board = Board::new();
/// // r n b q k b n r
/// // p p p p p p p p
/// // . . . . . . . .
/// // . . . . . . . .
/// // . . . . . . . .
/// // . . . . . . . .
/// // P P P P P P P P
/// // R N B Q K B N R
///
/// assert_eq!(board.piece_at(Square::E8), Some(Black.king()));
/// ```
///
/// [`Piece`]: struct.Piece.html
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct Board {
    occupied_co: [Bitboard; 2], // indexed by Color
    occupied: [Bitboard; 7], // all and pieces indexed by Role
    promoted: Bitboard,
}

impl Board {
    pub fn new() -> Board {
        Board {
            occupied_co: [Bitboard(0xffff_0000_0000_0000), Bitboard(0xffff)],
            occupied: [
                Bitboard(0xffff_0000_0000_ffff),
                Bitboard(0x00ff_0000_0000_ff00), // pawns
                Bitboard(0x4200_0000_0000_0042), // knights
                Bitboard(0x2400_0000_0000_0024), // bishops
                Bitboard(0x8100_0000_0000_0081), // rooks
                Bitboard(0x0800_0000_0000_0008), // queens
                Bitboard(0x1000_0000_0000_0010), // kings
            ],
            promoted: Bitboard(0),
        }
    }

    pub fn empty() -> Board {
        Board {
            occupied_co: [Bitboard(0), Bitboard(0)],
            occupied: [Bitboard(0); 7],
            promoted: Bitboard(0),
        }
    }

    pub fn racing_kings() -> Board {
        Board {
            occupied_co: [Bitboard(0x0f0f), Bitboard(0xf0f0)],
            occupied: [
                Bitboard(0xffff),
                Bitboard(0x0000), // pawns
                Bitboard(0x1818), // knights
                Bitboard(0x2424), // bishops
                Bitboard(0x4242), // rooks
                Bitboard(0x0081), // queens
                Bitboard(0x8100), // kings
            ],
            promoted: Bitboard(0),
        }
    }

    pub fn horde() -> Board {
        Board {
            occupied_co: [
                Bitboard(0xffff_0000_0000_0000), // black
                Bitboard(0x0000_0066_ffff_ffff), // white
            ],
            occupied: [
                Bitboard(0xffff_0066_ffff_ffff),
                Bitboard(0x00ff_0066_ffff_ffff), // pawns
                Bitboard(0x4200_0000_0000_0000), // knights
                Bitboard(0x2400_0000_0000_0000), // bishops
                Bitboard(0x8100_0000_0000_0000), // rooks
                Bitboard(0x0800_0000_0000_0000), // queens
                Bitboard(0x1000_0000_0000_0000), // kings
            ],
            promoted: Bitboard(0),
        }
    }

    #[inline]
    pub fn occupied(&self) -> Bitboard { self.occupied[0] }

    #[inline]
    pub fn pawns(&self)   -> Bitboard { self.occupied[Role::Pawn as usize] }
    #[inline]
    pub fn knights(&self) -> Bitboard { self.occupied[Role::Knight as usize] }
    #[inline]
    pub fn bishops(&self) -> Bitboard { self.occupied[Role::Bishop as usize] }
    #[inline]
    pub fn rooks(&self)   -> Bitboard { self.occupied[Role::Rook as usize] }
    #[inline]
    pub fn queens(&self)  -> Bitboard { self.occupied[Role::Queen as usize] }
    #[inline]
    pub fn kings(&self)   -> Bitboard { self.occupied[Role::King as usize] }

    #[inline]
    pub fn white(&self) -> Bitboard { self.occupied_co[Color::White as usize] }
    #[inline]
    pub fn black(&self) -> Bitboard { self.occupied_co[Color::Black as usize] }

    #[inline]
    pub fn promoted(&self) -> Bitboard { self.promoted }

    /// Bishops, rooks and queens.
    #[inline]
    pub fn sliders(&self) -> Bitboard { self.bishops() ^ self.rooks() ^ self.queens() }
    /// Pawns, knights and kings.
    #[inline]
    pub fn steppers(&self) -> Bitboard { self.pawns() ^ self.knights() ^ self.kings() }

    #[inline]
    pub fn rooks_and_queens(&self) -> Bitboard { self.rooks() ^ self.queens() }
    #[inline]
    pub fn bishops_and_queens(&self) -> Bitboard { self.bishops() ^ self.queens() }

    /// The (unique, unpromoted) king of the given side.
    #[inline]
    pub fn king_of(&self, color: Color) -> Option<Square> {
        (self.kings() & self.by_color(color) & !self.promoted).single_square()
    }

    #[inline]
    pub fn color_at(&self, sq: Square) -> Option<Color> {
        if self.white().contains(sq) {
            Some(Color::White)
        } else if self.black().contains(sq) {
            Some(Color::Black)
        } else {
            None
        }
    }

    #[inline]
    pub fn role_at(&self, sq: Square) -> Option<Role> {
        if !self.occupied[0].contains(sq) {
            None // catch early
        } else if self.pawns().contains(sq) {
            Some(Role::Pawn)
        } else if self.knights().contains(sq) {
            Some(Role::Knight)
        } else if self.bishops().contains(sq) {
            Some(Role::Bishop)
        } else if self.rooks().contains(sq) {
            Some(Role::Rook)
        } else if self.queens().contains(sq) {
            Some(Role::Queen)
        } else {
            Some(Role::King)
        }
    }

    #[inline]
    pub fn piece_at(&self, sq: Square) -> Option<Piece> {
        self.role_at(sq).map(|role| Piece {
            color: Color::from_white(self.white().contains(sq)),
            role,
        })
    }

    #[inline]
    pub fn remove_piece_at(&mut self, sq: Square) -> Option<Piece> {
        let piece = self.piece_at(sq);
        if let Some(p) = piece {
            self.occupied[0].toggle(sq);
            self.by_color_mut(p.color).toggle(sq);
            self.by_role_mut(p.role).toggle(sq);
            self.promoted.discard(sq);
        }
        piece
    }

    #[inline]
    pub fn discard_piece_at(&mut self, sq: Square) {
        self.occupied_co[0].discard(sq);
        self.occupied_co[1].discard(sq);
        self.occupied[0].discard(sq);
        self.occupied[1].discard(sq);
        self.occupied[2].discard(sq);
        self.occupied[3].discard(sq);
        self.occupied[4].discard(sq);
        self.occupied[5].discard(sq);
        self.occupied[6].discard(sq);
        self.promoted.discard(sq);
    }

    #[inline]
    pub fn set_piece_at(&mut self, sq: Square, Piece { color, role }: Piece, promoted: bool) {
        self.discard_piece_at(sq);
        self.occupied[0].toggle(sq);
        self.by_color_mut(color).toggle(sq);
        self.by_role_mut(role).toggle(sq);
        if promoted {
            self.promoted.toggle(sq);
        }
    }

    #[inline]
    pub fn by_color(&self, color: Color) -> Bitboard {
        self.occupied_co[color as usize]
    }

    #[inline]
    fn by_color_mut(&mut self, color: Color) -> &mut Bitboard {
        &mut self.occupied_co[color as usize]
    }

    #[inline]
    pub fn by_role(&self, role: Role) -> Bitboard {
        self.occupied[role as usize]
    }

    #[inline]
    fn by_role_mut(&mut self, role: Role) -> &mut Bitboard {
        &mut self.occupied[role as usize]
    }

    #[inline]
    pub fn by_piece(&self, piece: Piece) -> Bitboard {
        self.by_color(piece.color) & self.by_role(piece.role)
    }

    pub fn attacks_from(&self, sq: Square) -> Bitboard {
        self.piece_at(sq).map_or(Bitboard(0), |piece| {
            attacks::attacks(sq, piece, self.occupied[0])
        })
    }

    #[inline]
    pub fn attacks_to(&self, sq: Square, attacker: Color, occupied: Bitboard) -> Bitboard {
        self.by_color(attacker) & (
            (attacks::rook_attacks(sq, occupied) & self.rooks_and_queens()) |
            (attacks::bishop_attacks(sq, occupied) & self.bishops_and_queens()) |
            (attacks::knight_attacks(sq) & self.knights()) |
            (attacks::king_attacks(sq) & self.kings()) |
            (attacks::pawn_attacks(!attacker, sq) & self.pawns()))
    }

    pub fn pieces(&self) -> Pieces {
        Pieces {
            pawns: self.pawns(),
            knights: self.knights(),
            bishops: self.bishops(),
            rooks: self.rooks(),
            queens: self.queens(),
            kings: self.kings(),
            white: self.white(),
        }
    }

    pub fn material_side(&self, color: Color) -> MaterialSide {
        let side = self.by_color(color);

        MaterialSide {
            pawns: (self.pawns() & side).count() as u8,
            knights: (self.knights() & side).count() as u8,
            bishops: (self.bishops() & side).count() as u8,
            rooks: (self.rooks() & side).count() as u8,
            queens: (self.queens() & side).count() as u8,
            kings: (self.kings() & side).count() as u8,
        }
    }

    pub fn material(&self) -> Material {
        Material {
            black: self.material_side(Color::Black),
            white: self.material_side(Color::White),
        }
    }
}

impl Default for Board {
    fn default() -> Self {
        Board::new()
    }
}

impl fmt::Debug for Board {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for rank in (0..8).map(Rank::new).rev() {
            for file in (0..8).map(File::new) {
                let square = Square::from_coords(file, rank);
                f.write_char(self.piece_at(square).map_or('.', Piece::char))?;
                f.write_char(if file < File::H { ' ' } else { '\n' })?;
            }
        }

        Ok(())
    }
}

impl Extend<(Square, Piece)> for Board {
    fn extend<T: IntoIterator<Item = (Square, Piece)>>(&mut self, iter: T) {
        for (sq, piece) in iter {
            self.set_piece_at(sq, piece, false);
        }
    }
}

impl FromIterator<(Square, Piece)> for Board {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = (Square, Piece)>,
    {
        let mut board = Board::empty();
        board.extend(iter);
        board
    }
}

/// Iterator over the pieces of a [`Board`].
///
/// [`Board`]: struct.Board.html
#[derive(Clone)]
pub struct Pieces {
    pawns: Bitboard,
    knights: Bitboard,
    bishops: Bitboard,
    rooks: Bitboard,
    queens: Bitboard,
    kings: Bitboard,
    white: Bitboard,
}

impl fmt::Debug for Pieces {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Pieces").finish()
    }
}

impl Iterator for Pieces {
    type Item = (Square, Piece);

    fn next(&mut self) -> Option<(Square, Piece)> {
        if let Some(sq) = self.pawns.pop_front() {
            return Some((sq, (Color::from_white(self.white.contains(sq)).pawn())));
        }
        if let Some(sq) = self.knights.pop_front() {
            return Some((sq, (Color::from_white(self.white.contains(sq)).knight())));
        }
        if let Some(sq) = self.bishops.pop_front() {
            return Some((sq, (Color::from_white(self.white.contains(sq)).bishop())));
        }
        if let Some(sq) = self.rooks.pop_front() {
            return Some((sq, (Color::from_white(self.white.contains(sq)).rook())));
        }
        if let Some(sq) = self.queens.pop_front() {
            return Some((sq, (Color::from_white(self.white.contains(sq)).queen())));
        }
        if let Some(sq) = self.kings.pop_front() {
            return Some((sq, (Color::from_white(self.white.contains(sq)).king())));
        }
        None
    }

    fn count(self) -> usize {
        self.len()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.len();
        (len, Some(len))
    }
}

impl ExactSizeIterator for Pieces {
    fn len(&self) -> usize {
        self.pawns.count() + self.knights.count() + self.bishops.count()
            + self.rooks.count() + self.queens.count() + self.kings.count()
    }
}

impl ::std::iter::FusedIterator for Pieces {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{Black, White};

    #[test]
    fn test_piece_at() {
        let board = Board::new();
        assert_eq!(board.piece_at(Square::A2), Some(White.pawn()));
        assert_eq!(board.piece_at(Square::B1), Some(White.knight()));
    }

    #[test]
    fn test_set_piece_at() {
        let mut board = Board::new();
        board.set_piece_at(Square::A3, White.pawn(), false);
        assert_eq!(board.piece_at(Square::A3), Some(White.pawn()));
    }

    #[test]
    fn test_promoted() {
        let board: Board = "4k3/8/8/8/8/8/8/2q~1K3".parse().expect("valid fen");
        assert_eq!(board.piece_at(Square::C1), Some(Black.queen()));
        assert!(board.promoted().contains(Square::C1));
    }
}