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
//! Additional common utilities

use core::fmt::Display;

use crate::*;

#[cfg(test)]
mod tests;

/// Parses a UCI move into a [`Move`].
///
/// This differs from [`Move`]'s [`FromStr`](core::str::FromStr) implementation in that
/// it converts the standard UCI castling notation to the king-captures-rook
/// notation that `cozy-chess` uses (e.g. `e1g1` parses as `e1h1`).
///
/// # Examples
///
/// ```
/// # use cozy_chess::*;
/// # use cozy_chess::util::*;
/// let board: Board = "rnbqkb1r/ppp2ppp/4pn2/3p4/8/5NP1/PPPPPPBP/RNBQK2R w KQkq - 0 4"
///     .parse().unwrap();
/// assert_eq!(
///     parse_uci_move(&board, "e1g1").unwrap(),
///     "e1h1".parse::<Move>().unwrap()
/// );
/// ```
pub fn parse_uci_move(board: &Board, mv: &str) -> Result<Move, MoveParseError> {
    let mut mv: Move = mv.parse()?;

    let first_rank = Rank::First.relative_to(board.side_to_move());
    let uci_castle_start = Square::new(File::E, first_rank);
    let uci_castle_short = Square::new(File::G, first_rank);
    let uci_castle_long = Square::new(File::C, first_rank);
    let rights = board.castle_rights(board.side_to_move());

    if board.king(board.side_to_move()) == mv.from && mv.from == uci_castle_start {
        if mv.to == uci_castle_short {
            if let Some(rook_file) = rights.short {
                mv.to = Square::new(rook_file, first_rank);
            }
        }
        if mv.to == uci_castle_long {
            if let Some(rook_file) = rights.long {
                mv.to = Square::new(rook_file, first_rank);
            }
        }
    }

    Ok(mv)
}

/// Returns an object that allows printing a [`Move`] in UCI format.
///
/// This differs from [`Move`]'s [`Display`] implementation in that
/// it converts the king-captures-rook notation that `cozy-chess`
/// uses to standard UCI castling (e.g. `e1h1` displays as `e1g1`).
/// 
/// # Examples
///
/// ```
/// # use cozy_chess::*;
/// # use cozy_chess::util::*;
/// let board: Board = "rnbqkb1r/ppp2ppp/4pn2/3p4/8/5NP1/PPPPPPBP/RNBQK2R w KQkq - 0 4"
///     .parse().unwrap();
/// let castle: Move = "e1h1".parse().unwrap();
/// assert_eq!(format!("{}", display_uci_move(&board, castle)), "e1g1");
/// ```
pub fn display_uci_move(board: &Board, mv: Move) -> impl core::fmt::Display {
    let mut mv = mv;

    let first_rank = Rank::First.relative_to(board.side_to_move());
    let rights = board.castle_rights(board.side_to_move());
    let frc_castle_short = rights.short.map(|f| Square::new(f, first_rank));
    let frc_castle_long = rights.long.map(|f| Square::new(f, first_rank));

    if board.king(board.side_to_move()) == mv.from {
        if Some(mv.to) == frc_castle_short {
            mv.to = Square::new(File::G, first_rank);
        }
        if Some(mv.to) == frc_castle_long {
            mv.to = Square::new(File::C, first_rank);
        }
    }

    mv
}

/// Parses a Standard Algebraic Notation move into a [`Move`].
///
/// Canonical SAN is guaranteed to parse correctly, but non-canonical SAN may or may not parse.
/// The returned move is always legal.
///
/// # Examples
///
/// ```
/// # use cozy_chess::*;
/// # use cozy_chess::util::*;
/// let board: Board = "3k2n1/7P/Q3p3/4BPp1/Q1Q4q/8/5B2/R3K2R w KQ g6 0 1"
///     .parse().unwrap();
/// let mv: Move = "h7g8r".parse().unwrap();
/// assert_eq!(parse_san_move(&board, "hxg8=R").unwrap(), mv);
/// let mv: Move = "e1a1".parse().unwrap();
/// assert_eq!(parse_san_move(&board, "O-O-O+").unwrap(), mv);
/// let mv: Move = "e5d4".parse().unwrap();
/// assert_eq!(parse_san_move(&board, "Bd4").unwrap(), mv);
/// ```
pub fn parse_san_move(board: &Board, mv: &str) -> Result<Move, MoveParseError> {
    // SAN is easier to parse backwards
    let mut chars = mv.chars().rev().peekable();

    // Ignore check/checkmate character, we don't need it
    chars.next_if(|&c| c == '+' || c == '#');

    let dst;
    let src_rank: Option<Rank>;
    let src_file: Option<File>;
    let piece;
    let promotion;

    if chars.next_if_eq(&'O').is_some() {
        // Castles

        chars.next_if_eq(&'-').ok_or(MoveParseError)?;
        chars.next_if_eq(&'O').ok_or(MoveParseError)?;

        let rook_file = if chars.next_if_eq(&'-').is_some() {
            chars.next_if_eq(&'O').ok_or(MoveParseError)?;
            board.castle_rights(board.side_to_move()).long
        } else {
            board.castle_rights(board.side_to_move()).short
        };

        dst = Square::new(
            rook_file.ok_or(MoveParseError)?,
            board.king(board.side_to_move()).rank(),
        );
        piece = Piece::King;
        src_file = None;
        src_rank = None;
        promotion = None;
    } else {
        // Non-castles

        promotion = chars
            .peek()
            .filter(|&c| c.is_ascii_uppercase())
            .and_then(|&c| c.to_ascii_lowercase().try_into().ok());

        if promotion.is_some() {
            // Consume promotion character
            chars.next();
            // Consume optional '='
            chars.next_if_eq(&'=');
        }
        // Destination square
        let dst_rank = chars
            .next()
            .and_then(|c| c.try_into().ok())
            .ok_or(MoveParseError)?;
        let dst_file = chars
            .next()
            .and_then(|c| c.try_into().ok())
            .ok_or(MoveParseError)?;
        dst = Square::new(dst_file, dst_rank);

        // Consume optional captures
        chars.next_if_eq(&'x');

        // Source square
        src_rank = chars.peek().and_then(|&c| c.try_into().ok());
        if src_rank.is_some() {
            chars.next();
        }
        src_file = chars.peek().and_then(|&c| c.try_into().ok());
        if src_file.is_some() {
            chars.next();
        }

        // Piece
        piece = chars.next().map_or(Ok(Piece::Pawn), |c| {
            c.is_ascii_uppercase()
                .then_some(c.to_ascii_lowercase())
                .ok_or(MoveParseError)?
                .try_into()
                .map_err(|_| MoveParseError)
        })?;
    }

    if chars.next().is_some() {
        // too many characters
        return Err(MoveParseError);
    }

    let mut src_mask = board.colored_pieces(board.side_to_move(), piece);
    if let Some(src_rank) = src_rank {
        src_mask &= src_rank.bitboard();
    }
    if let Some(src_file) = src_file {
        src_mask &= src_file.bitboard();
    }

    let mut mv = None;
    board.generate_moves_for(src_mask, |mut mvs| {
        mvs.to &= dst.bitboard();
        for m in mvs {
            if m.promotion != promotion {
                continue;
            }
            if mv.is_some() {
                // ambiguous; error out
                mv = None;
                return true;
            }
            mv = Some(m);
        }
        false
    });

    mv.ok_or(MoveParseError)
}

/// Returns an object that allows printing a [`Move`] in Standard Algebraic Notation.
///
/// # Panics
/// This is guaranteed to panic if the move is illegal.
///
/// # Examples
///
/// ```
/// # use cozy_chess::*;
/// # use cozy_chess::util::*;
/// let board: Board = "3k2n1/7P/Q3p3/4BPp1/Q1Q4q/8/5B2/R3K2R w KQ g6 0 1"
///     .parse().unwrap();
/// let mv: Move = "h7g8r".parse().unwrap();
/// assert_eq!(format!("{}", display_san_move(&board, mv)), "hxg8=R+");
/// let mv: Move = "e1a1".parse().unwrap();
/// assert_eq!(format!("{}", display_san_move(&board, mv)), "O-O-O+");
/// let mv: Move = "e5d4".parse().unwrap();
/// assert_eq!(format!("{}", display_san_move(&board, mv)), "Bd4");
/// ```
pub fn display_san_move(board: &Board, mv: Move) -> impl Display {
    let mut after_board = board.clone();
    after_board.play(mv);

    let check = !after_board.checkers().is_empty();
    let checkmate = check && !after_board.generate_moves(|_| true);

    let piece = board.piece_on(mv.from).unwrap();
    let captures = board.occupied().len() > after_board.occupied().len();

    let first_rank = Rank::First.relative_to(board.side_to_move());
    let rights = board.castle_rights(board.side_to_move());
    let castle_short = rights.short.map(|f| Square::new(f, first_rank));
    let castle_long = rights.long.map(|f| Square::new(f, first_rank));

    if piece == Piece::King && Some(mv.to) == castle_short || Some(mv.to) == castle_long {
        return SanDisplay {
            piece: None,
            from_file: None,
            from_rank: None,
            captures,
            to_sq: mv.to,
            promotion: None,
            check,
            checkmate,
            long_castles: Some(mv.to) == castle_long,
            short_castles: Some(mv.to) == castle_short,
        };
    }

    let mut file_disambiguates = true;
    let mut rank_disambiguates = true;
    let mut ambiguous = false;

    board.generate_moves_for(board.colored_pieces(board.side_to_move(), piece), |mvs| {
        if mvs.from != mv.from && mvs.to.has(mv.to) {
            ambiguous = true;
            if mvs.from.file() == mv.from.file() {
                file_disambiguates = false;
            }
            if mvs.from.rank() == mv.from.rank() {
                rank_disambiguates = false;
            }
        }
        false
    });

    if piece == Piece::Pawn && captures {
        ambiguous = true;
    }

    let (from_file, from_rank) = match (ambiguous, file_disambiguates, rank_disambiguates) {
        (false, _, _) => (None, None),
        (true, true, _) => (Some(mv.from.file()), None),
        (true, false, false) => (Some(mv.from.file()), Some(mv.from.rank())),
        (true, false, true) => (None, Some(mv.from.rank())),
    };

    SanDisplay {
        piece: (piece != Piece::Pawn).then_some(piece),
        from_file,
        from_rank,
        captures,
        to_sq: mv.to,
        promotion: mv.promotion,
        check,
        checkmate,
        long_castles: false,
        short_castles: false,
    }
}

struct SanDisplay {
    piece: Option<Piece>,
    from_file: Option<File>,
    from_rank: Option<Rank>,
    captures: bool,
    to_sq: Square,
    promotion: Option<Piece>,
    check: bool,
    checkmate: bool,
    long_castles: bool,
    short_castles: bool,
}

impl Display for SanDisplay {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if self.long_castles {
            write!(f, "O-O-O")?;
        } else if self.short_castles {
            write!(f, "O-O")?;
        } else {
            if let Some(piece) = self.piece {
                write!(f, "{}", char::to_ascii_uppercase(&piece.into()))?;
            }
            if let Some(file) = self.from_file {
                write!(f, "{file}")?;
            }
            if let Some(rank) = self.from_rank {
                write!(f, "{rank}")?;
            }
            if self.captures {
                write!(f, "x")?;
            }
            write!(f, "{}", self.to_sq)?;
            if let Some(promo) = self.promotion {
                write!(f, "={}", char::to_ascii_uppercase(&promo.into()))?;
            }
        }

        if self.checkmate {
            write!(f, "#")?;
        } else if self.check {
            write!(f, "+")?;
        }

        Ok(())
    }
}