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
use crate::*;

use super::zobrist::ZobristBoard;

helpers::simple_error! {
    /// An error while building a board.
    pub enum BoardBuilderError {
        InvalidBoard = "The board is invalid.",
        InvalidCastlingRights = "The castling rights are invalid.",
        InvalidEnPassant = "The en passant square is invalid.",
        InvalidHalfMoveClock = "The halfmove clock is invalid.",
        InvalidFullmoveNumber = "The fullmove number is invalid."
    }
}

/// A board builder to manipulate arbitrary boards.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BoardBuilder {
    /// The board state. Index by square to get the corresponding piece.
    pub board: [Option<(Piece, Color)>; Square::NUM],
    /// The side to move.
    pub side_to_move: Color,
    /// The castling rights. Index by color to get the corresponding side's rights.
    pub castle_rights: [CastleRights; Color::NUM],
    /// The en passant square.
    pub en_passant: Option<Square>,
    /// The halfmove clock.
    pub halfmove_clock: u8,
    /// The fullmove number.
    pub fullmove_number: u16
}

impl Default for BoardBuilder {
    fn default() -> Self {
        BoardBuilder::startpos()
    }
}

impl BoardBuilder {
    /// Get an empty builder. All fields are set to their empty values.
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let builder = BoardBuilder::empty();
    /// for &square in &Square::ALL {
    ///     assert!(builder.square(square).is_none());
    /// }
    /// ```
    pub fn empty() -> Self {
        Self {
            board: [None; Square::NUM],
            side_to_move: Color::White,
            castle_rights: [CastleRights::EMPTY; Color::NUM],
            en_passant: None,
            halfmove_clock: 0,
            fullmove_number: 1
        }
    }

    /// Get a builder set to the default start position.
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let startpos = Board::default();
    /// let builder = BoardBuilder::default();
    /// assert_eq!(builder.build().unwrap(), startpos);
    /// ```
    pub fn startpos() -> Self {
        Self::chess960_startpos(518)
    }

    /// Get a builder set to a chess960 start position.
    /// Converts a [scharnagl number](https://en.wikipedia.org/wiki/Fischer_random_chess_numbering_scheme)
    /// to its corresponding position.
    /// # Panics
    /// Panic if the scharnagl number is invalid (not within the range 0..960).
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let startpos = Board::default();
    /// // 518 is the scharnagl number for the default start position.
    /// let builder = BoardBuilder::chess960_startpos(518);
    /// assert_eq!(builder.build().unwrap(), startpos);
    /// ```
    pub fn chess960_startpos(scharnagl_number: u32) -> Self {
        Self::double_chess960_startpos(scharnagl_number, scharnagl_number)
    }

    /// Get a builder set to a double chess960 start position.
    /// Uses two [scharnagl numbers](https://en.wikipedia.org/wiki/Fischer_random_chess_numbering_scheme)
    /// for the initial setup for white and the initial setup for black.
    /// # Panics
    /// Panic if either scharnagl number is invalid (not within the range 0..960).
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let startpos = Board::default();
    /// // 518 is the scharnagl number for the default start position.
    /// let builder = BoardBuilder::double_chess960_startpos(518, 518);
    /// assert_eq!(builder.build().unwrap(), startpos);
    /// ```
    pub fn double_chess960_startpos(white_scharnagl_number: u32, black_scharnagl_number: u32) -> Self {
        let mut this = Self::empty();
        this.write_piece_config(white_scharnagl_number, Color::White);
        this.write_piece_config(black_scharnagl_number, Color::Black);
        this
    }

    fn write_piece_config(&mut self, scharnagl_number: u32, color: Color) {
        assert!(scharnagl_number < 960, "Scharnagl number must be in range 0..960");
        
        let n = scharnagl_number;
        let (n, light_bishop) = (n / 4, n % 4);
        let (n, dark_bishop) = (n / 4, n % 4);
        let (n, queen) = (n / 6, n % 6);
        let knights = n;

        let back_rank = Rank::First.relative_to(color);

        let mut free_squares = back_rank.bitboard();

        let light_bishop = match light_bishop {
            0 => File::B,
            1 => File::D,
            2 => File::F,
            3 => File::H,
            _ => unreachable!()
        };
        let light_bishop = Square::new(light_bishop, back_rank);
        free_squares ^= light_bishop.bitboard();

        let dark_bishop = match dark_bishop {
            0 => File::A,
            1 => File::C,
            2 => File::E,
            3 => File::G,
            _ => unreachable!()
        };
        let dark_bishop = Square::new(dark_bishop, back_rank);
        free_squares ^= dark_bishop.bitboard();

        let queen = free_squares.iter().nth(queen as usize).unwrap();
        free_squares ^= queen.bitboard();

        let (left_knight, right_knight) = match knights {
            0 => (0, 1),
            1 => (0, 2),
            2 => (0, 3),
            3 => (0, 4),

            4 => (1, 2),
            5 => (1, 3),
            6 => (1, 4),

            7 => (2, 3),
            8 => (2, 4),
            
            9 => (3, 4),

            _ => unreachable!()
        };
        let left_knight = free_squares.iter().nth(left_knight).unwrap();
        let right_knight = free_squares.iter().nth(right_knight).unwrap();
        free_squares ^= left_knight.bitboard();
        free_squares ^= right_knight.bitboard();

        let left_rook = free_squares.next_square().unwrap();
        free_squares ^= left_rook.bitboard();

        let king = free_squares.next_square().unwrap();
        free_squares ^= king.bitboard();

        let right_rook = free_squares.next_square().unwrap();
        free_squares ^= right_rook.bitboard();

        *self.square_mut(light_bishop) = Some((Piece::Bishop, color));
        *self.square_mut(dark_bishop)  = Some((Piece::Bishop, color));
        *self.square_mut(queen)        = Some((Piece::Queen, color));
        *self.square_mut(left_knight)  = Some((Piece::Knight, color));
        *self.square_mut(right_knight) = Some((Piece::Knight, color));
        *self.square_mut(left_rook)    = Some((Piece::Rook, color));
        *self.square_mut(king)         = Some((Piece::King, color));
        *self.square_mut(right_rook)   = Some((Piece::Rook, color));

        let pawn_rank = Rank::Second.relative_to(color);
        for square in pawn_rank.bitboard() {
            *self.square_mut(square) = Some((Piece::Pawn, color));
        }

        *self.castle_rights_mut(color) = CastleRights {
            short: Some(right_rook.file()),
            long: Some(left_rook.file())
        };
    }

    /// Create a builder from a [`Board`].
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let board = Board::default();
    /// let builder = BoardBuilder::from_board(&board);
    /// assert_eq!(builder.build().unwrap(), board);
    /// ```
    pub fn from_board(board: &Board) -> Self {
        let mut this = BoardBuilder::empty();
        for &color in &Color::ALL {
            let pieces = board.colors(color);
            for &piece in &Piece::ALL {
                let pieces = pieces & board.pieces(piece);
                for square in pieces {
                    *this.square_mut(square) = Some((piece, color));
                }
            }
            *this.castle_rights_mut(color) = *board.castle_rights(color);
        }
        this.side_to_move = board.side_to_move();
        let en_passant_rank = Rank::Third.relative_to(!board.side_to_move());
        this.en_passant = board.en_passant().map(|f| Square::new(f, en_passant_rank));
        this.halfmove_clock = board.halfmove_clock();
        this.fullmove_number = board.fullmove_number();
        this
    }

    /// Get a square on the board.
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let builder = BoardBuilder::default();
    /// assert_eq!(builder.square(Square::A1), Some((Piece::Rook, Color::White)));
    /// ```
    pub fn square(&self, square: Square) -> Option<(Piece, Color)> {
        self.board[square as usize]
    }

    /// Mutably get a square on the board.
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let mut builder = BoardBuilder::default();
    /// *builder.square_mut(Square::A1) = Some((Piece::Knight, Color::White));
    /// assert_eq!(builder.square(Square::A1), Some((Piece::Knight, Color::White)));
    /// ```
    pub fn square_mut(&mut self, square: Square) -> &mut Option<(Piece, Color)> {
        &mut self.board[square as usize]
    }

    /// Get the castle rights for a side.
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let builder = BoardBuilder::default();
    /// let rights = builder.castle_rights(Color::White);
    /// assert_eq!(rights.short, Some(File::H));
    /// assert_eq!(rights.long, Some(File::A));
    /// ```
    pub fn castle_rights(&self, color: Color) -> &CastleRights {
        &self.castle_rights[color as usize]
    }

    /// Mutably get the castle rights for a side.
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let mut builder = BoardBuilder::default();
    /// let rights = builder.castle_rights_mut(Color::White);
    /// rights.short = None;
    /// assert_eq!(rights.short, None);
    /// ```
    pub fn castle_rights_mut(&mut self, color: Color) -> &mut CastleRights {
        &mut self.castle_rights[color as usize]
    }

    /// Build a [`Board`] from this builder.
    /// # Errors
    /// This will error if the current state is invalid.
    /// # Examples
    /// ```
    /// # use cozy_chess::*;
    /// let builder = BoardBuilder::default().build().unwrap();
    /// assert_eq!(builder, Board::default());
    /// ```
    pub fn build(&self) -> Result<Board, BoardBuilderError> {
        use BoardBuilderError::*;

        let mut board = Board {
            inner: ZobristBoard::empty(),
            pinned: BitBoard::EMPTY,
            checkers: BitBoard::EMPTY,
            halfmove_clock: 0,
            fullmove_number: 0
        };

        self.add_board          (&mut board).map_err(|_| InvalidBoard)?;
        self.add_castle_rights  (&mut board).map_err(|_| InvalidCastlingRights)?;
        self.add_en_passant     (&mut board).map_err(|_| InvalidEnPassant)?;
        self.add_halfmove_clock (&mut board).map_err(|_| InvalidHalfMoveClock)?;
        self.add_fullmove_number(&mut board).map_err(|_| InvalidFullmoveNumber)?;
        
        Ok(board)
    }

    fn add_board(&self, board: &mut Board) -> Result<(), ()> {
        for &square in &Square::ALL {
            if let Some((piece, color)) = self.square(square) {
                board.inner.xor_square(piece, color, square);
            }
        }
        if self.side_to_move != board.side_to_move() {
            board.inner.toggle_side_to_move();
        }
        if !board.board_is_valid() {
            return Err(());
        }

        let (checkers, pinned) = board.calculate_checkers_and_pins(board.side_to_move());
        board.checkers = checkers;
        board.pinned = pinned;

        Ok(())
    }

    fn add_castle_rights(&self, board: &mut Board) -> Result<(), ()> {
        for &color in &Color::ALL {
            let rights = self.castle_rights[color as usize];
            board.inner.set_castle_right(color, true, rights.short);
            board.inner.set_castle_right(color, false, rights.long);
        }
        if !board.castle_rights_are_valid() {
            return Err(());
        }
        Ok(())
    }

    fn add_en_passant(&self, board: &mut Board) -> Result<(), ()> {
        if let Some(square) = self.en_passant {
            let en_passant_rank = Rank::Third.relative_to(!board.side_to_move());
            if square.rank() != en_passant_rank {
                return Err(());
            }
            board.inner.set_en_passant(Some(square.file()));
        }
        if !board.en_passant_is_valid() {
            return Err(());
        }
        Ok(())
    }

    fn add_halfmove_clock(&self, board: &mut Board) -> Result<(), ()> {
        board.halfmove_clock = self.halfmove_clock;
        if !board.halfmove_clock_is_valid() {
            return Err(());
        }
        Ok(())
    }

    fn add_fullmove_number(&self, board: &mut Board) -> Result<(), ()> {
        board.fullmove_number = self.fullmove_number;
        if !board.fullmove_number_is_valid() {
            return Err(());
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn roundtrip_board() {
        let positions = include_str!("test_data/valid.sfens");
        for fen in positions.lines() {
            let board = Board::from_fen(fen, true).unwrap();
            let builder = BoardBuilder::from_board(&board);
            assert_eq!(builder.build().unwrap(), board);
        }
    }

    #[test]
    fn scharnagl_to_board() {
        let positions = include_str!("test_data/chess960_start_positions.sfens");
        for (scharnagl_number, fen) in positions.lines().enumerate() {
            let board = Board::from_fen(fen, true).unwrap();
            let builder = BoardBuilder::chess960_startpos(scharnagl_number as u32);
            assert_eq!(builder.build().unwrap(), board);
        }
    }
}