rumenx-chess 0.2.0

A high-performance chess engine and REST API server written in Rust
Documentation
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Standard Algebraic Notation (SAN) generation and parsing.
//!
//! SAN examples: `e4`, `Nf3`, `Bxe5`, `O-O`, `e8=Q+`, `Raxd1#`.

use crate::engine::board::Position;
use crate::engine::movegen;
use crate::engine::types::{Move, PieceType, Square};

// =========================================================================
// SAN generation
// =========================================================================

/// Convert a move to SAN notation.
///
/// `legal_moves` should be the full list of legal moves in the position
/// (passed in to avoid redundant generation).
///
/// Note: does NOT append `+` or `#` — the caller (`Game::make_move`) adds
/// those after the move is applied and the resulting status is known.
pub fn move_to_san(pos: &Position, mv: Move, legal_moves: &[Move]) -> String {
    // Castling.
    if mv.flags.is_castling() {
        return if mv.to.file() > mv.from.file() {
            "O-O".into()
        } else {
            "O-O-O".into()
        };
    }

    let piece = pos
        .piece_at(mv.from)
        .map(|(_, pt)| pt)
        .expect("SAN: no piece on from square");

    let mut san = String::with_capacity(8);

    if piece == PieceType::Pawn {
        // Pawn moves.
        if mv.flags.is_capture() || mv.flags.is_en_passant() {
            // Prefix with departure file on captures: "exd5".
            san.push((b'a' + mv.from.file()) as char);
            san.push('x');
        }
        san.push_str(&mv.to.to_algebraic());

        // Promotion suffix.
        if let Some(promo) = mv.promotion {
            san.push('=');
            san.push(piece_letter(promo));
        }
    } else {
        // Piece moves: N, B, R, Q, K.
        san.push(piece_letter(piece));

        // Disambiguation: if other pieces of the same type can move to the same square.
        let disambig = disambiguation(pos, mv, piece, legal_moves);
        san.push_str(&disambig);

        if mv.flags.is_capture() {
            san.push('x');
        }

        san.push_str(&mv.to.to_algebraic());
    }

    san
}

/// Determine the disambiguation string needed for a piece move.
///
/// If multiple pieces of the same type can move to the same square, we add
/// file, rank, or both to distinguish.
fn disambiguation(pos: &Position, mv: Move, piece: PieceType, legal_moves: &[Move]) -> String {
    let us = pos.side_to_move;

    // Find other legal moves by the same piece type to the same destination.
    let ambiguous: Vec<&Move> = legal_moves
        .iter()
        .filter(|m| {
            m.to == mv.to
                && m.from != mv.from
                && !m.flags.is_castling()
                && pos
                    .piece_at(m.from)
                    .map(|(c, pt)| c == us && pt == piece)
                    .unwrap_or(false)
        })
        .collect();

    if ambiguous.is_empty() {
        return String::new();
    }

    let same_file = ambiguous.iter().any(|m| m.from.file() == mv.from.file());
    let same_rank = ambiguous.iter().any(|m| m.from.rank() == mv.from.rank());

    match (same_file, same_rank) {
        (false, _) => {
            // File alone is sufficient.
            format!("{}", (b'a' + mv.from.file()) as char)
        }
        (true, false) => {
            // Rank alone is sufficient.
            format!("{}", (b'1' + mv.from.rank()) as char)
        }
        (true, true) => {
            // Need both file and rank.
            format!(
                "{}{}",
                (b'a' + mv.from.file()) as char,
                (b'1' + mv.from.rank()) as char
            )
        }
    }
}

fn piece_letter(pt: PieceType) -> char {
    match pt {
        PieceType::Pawn => 'P',
        PieceType::Knight => 'N',
        PieceType::Bishop => 'B',
        PieceType::Rook => 'R',
        PieceType::Queen => 'Q',
        PieceType::King => 'K',
    }
}

// =========================================================================
// SAN parsing
// =========================================================================

/// Parse a SAN string and return the corresponding legal move.
///
/// Accepts standard SAN: `e4`, `Nf3`, `Bxe5`, `O-O`, `O-O-O`, `e8=Q`, etc.
/// Check/checkmate suffixes (`+`, `#`) are ignored.
pub fn parse_san(pos: &Position, san: &str) -> Result<Move, crate::engine::types::ChessError> {
    let legal = movegen::legal_moves(pos);
    let san = san.trim_end_matches(['+', '#', '!', '?']);

    // Castling.
    if san == "O-O" || san == "0-0" {
        return find_castling(pos, &legal, true);
    }
    if san == "O-O-O" || san == "0-0-0" {
        return find_castling(pos, &legal, false);
    }

    let chars: Vec<char> = san.chars().collect();
    if chars.is_empty() {
        return Err(crate::engine::types::ChessError::InvalidMove {
            from: String::new(),
            to: String::new(),
            reason: "empty SAN string".into(),
        });
    }

    // Detect promotion.
    let (chars, promotion) = if chars.len() >= 2 && chars[chars.len() - 2] == '=' {
        let promo_char = chars[chars.len() - 1];
        let promo = match promo_char {
            'Q' | 'q' => PieceType::Queen,
            'R' | 'r' => PieceType::Rook,
            'B' | 'b' => PieceType::Bishop,
            'N' | 'n' => PieceType::Knight,
            _ => {
                return Err(crate::engine::types::ChessError::InvalidPromotion(
                    promo_char.to_string(),
                ));
            }
        };
        (&chars[..chars.len() - 2], Some(promo))
    } else {
        (&chars[..], None)
    };

    // Determine piece type.
    let (piece, rest) = if chars[0].is_uppercase() && "NBRQK".contains(chars[0]) {
        let pt = match chars[0] {
            'N' => PieceType::Knight,
            'B' => PieceType::Bishop,
            'R' => PieceType::Rook,
            'Q' => PieceType::Queen,
            'K' => PieceType::King,
            _ => unreachable!(),
        };
        (pt, &chars[1..])
    } else {
        (PieceType::Pawn, chars)
    };

    // Strip capture marker 'x'.
    let rest: Vec<char> = rest.iter().copied().filter(|&c| c != 'x').collect();

    // The last two characters are the destination square.
    if rest.len() < 2 {
        return Err(crate::engine::types::ChessError::InvalidMove {
            from: String::new(),
            to: san.to_string(),
            reason: "SAN too short".into(),
        });
    }

    let dest_str: String = rest[rest.len() - 2..].iter().collect();
    let dest = Square::from_algebraic(&dest_str)
        .ok_or_else(|| crate::engine::types::ChessError::InvalidSquare(dest_str.clone()))?;

    // Disambiguation characters (0, 1, or 2 chars before destination).
    let disambig = &rest[..rest.len() - 2];
    let disambig_file: Option<u8> = disambig
        .iter()
        .find(|c| c.is_ascii_lowercase())
        .map(|&c| c as u8 - b'a');
    let disambig_rank: Option<u8> = disambig
        .iter()
        .find(|c| c.is_ascii_digit())
        .map(|&c| c as u8 - b'1');

    // Find matching legal move.
    let us = pos.side_to_move;
    let candidates: Vec<&Move> = legal
        .iter()
        .filter(|m| {
            if m.to != dest {
                return false;
            }
            if let Some((color, pt)) = pos.piece_at(m.from) {
                if color != us || pt != piece {
                    return false;
                }
            } else {
                return false;
            }
            if let Some(f) = disambig_file
                && m.from.file() != f
            {
                return false;
            }
            if let Some(r) = disambig_rank
                && m.from.rank() != r
            {
                return false;
            }
            if let Some(promo) = promotion {
                if m.promotion != Some(promo) {
                    return false;
                }
            } else if m.promotion.is_some() {
                return false;
            }
            true
        })
        .collect();

    match candidates.len() {
        0 => Err(crate::engine::types::ChessError::InvalidMove {
            from: String::new(),
            to: san.to_string(),
            reason: format!("no legal move matches SAN '{san}'"),
        }),
        1 => Ok(*candidates[0]),
        _ => Err(crate::engine::types::ChessError::InvalidMove {
            from: String::new(),
            to: san.to_string(),
            reason: format!("ambiguous SAN '{san}': {} candidates", candidates.len()),
        }),
    }
}

fn find_castling(
    pos: &Position,
    legal: &[Move],
    kingside: bool,
) -> Result<Move, crate::engine::types::ChessError> {
    let king_sq = pos.king_sq(pos.side_to_move);
    let target_file = if kingside { 6 } else { 2 };

    legal
        .iter()
        .find(|m| m.flags.is_castling() && m.from == king_sq && m.to.file() == target_file)
        .copied()
        .ok_or_else(|| crate::engine::types::ChessError::InvalidMove {
            from: king_sq.to_algebraic(),
            to: String::new(),
            reason: format!(
                "castling {} not legal",
                if kingside { "kingside" } else { "queenside" }
            ),
        })
}

// =========================================================================
// Tests
// =========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::board::Position;
    use crate::engine::movegen;
    use crate::engine::types::MoveFlags;

    fn sq(name: &str) -> Square {
        Square::from_algebraic(name).unwrap()
    }

    fn pos(fen: &str) -> Position {
        Position::from_fen(fen).unwrap()
    }

    fn san(fen: &str, from: &str, to: &str, flags: MoveFlags) -> String {
        let p = pos(fen);
        let legal = movegen::legal_moves(&p);
        let mv = Move::with_flags(sq(from), sq(to), flags);
        move_to_san(&p, mv, &legal)
    }

    fn san_promo(fen: &str, from: &str, to: &str, promo: PieceType, flags: MoveFlags) -> String {
        let p = pos(fen);
        let legal = movegen::legal_moves(&p);
        let mv = Move::with_promotion(sq(from), sq(to), promo, flags);
        move_to_san(&p, mv, &legal)
    }

    // -------------------------------------------------------------------
    // Pawn moves
    // -------------------------------------------------------------------

    #[test]
    fn san_pawn_push() {
        assert_eq!(
            san(
                "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
                "e2",
                "e4",
                MoveFlags::DOUBLE_PUSH
            ),
            "e4"
        );
    }

    #[test]
    fn san_pawn_capture() {
        assert_eq!(
            san(
                "rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 2",
                "e4",
                "d5",
                MoveFlags::CAPTURE
            ),
            "exd5"
        );
    }

    #[test]
    fn san_pawn_promotion() {
        assert_eq!(
            san_promo(
                "7k/4P3/8/8/8/8/8/4K3 w - - 0 1",
                "e7",
                "e8",
                PieceType::Queen,
                MoveFlags::NONE
            ),
            "e8=Q"
        );
    }

    #[test]
    fn san_en_passant() {
        assert_eq!(
            san(
                "rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPP1PPP/RNBQKBNR w KQkq f6 0 3",
                "e5",
                "f6",
                MoveFlags::CAPTURE | MoveFlags::EN_PASSANT
            ),
            "exf6"
        );
    }

    // -------------------------------------------------------------------
    // Piece moves
    // -------------------------------------------------------------------

    #[test]
    fn san_knight_move() {
        assert_eq!(
            san(
                "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
                "g1",
                "f3",
                MoveFlags::NONE
            ),
            "Nf3"
        );
    }

    #[test]
    fn san_bishop_capture() {
        assert_eq!(
            san(
                "rnbqk1nr/pppp1ppp/4p3/8/1b6/2N5/PPPPPPPP/R1BQKBNR b KQkq - 2 2",
                "b4",
                "c3",
                MoveFlags::CAPTURE
            ),
            "Bxc3"
        );
    }

    // -------------------------------------------------------------------
    // Castling
    // -------------------------------------------------------------------

    #[test]
    fn san_castling_kingside() {
        assert_eq!(
            san(
                "r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1",
                "e1",
                "g1",
                MoveFlags::CASTLING
            ),
            "O-O"
        );
    }

    #[test]
    fn san_castling_queenside() {
        assert_eq!(
            san(
                "r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1",
                "e1",
                "c1",
                MoveFlags::CASTLING
            ),
            "O-O-O"
        );
    }

    // -------------------------------------------------------------------
    // Disambiguation
    // -------------------------------------------------------------------

    #[test]
    fn san_rook_file_disambiguation() {
        // Two rooks on a1 and h1, king on e3 (out of the way).
        // Both rooks can reach e1 → need file disambiguation.
        assert_eq!(
            san(
                "4k3/8/8/8/8/4K3/8/R6R w - - 0 1",
                "a1",
                "e1",
                MoveFlags::NONE
            ),
            "Rae1"
        );
    }

    #[test]
    fn san_rook_rank_disambiguation() {
        // Two rooks on a1 and a8.
        assert_eq!(
            san(
                "R3k3/8/8/8/8/8/8/R3K3 w - - 0 1",
                "a1",
                "a4",
                MoveFlags::NONE
            ),
            "R1a4"
        );
    }

    // -------------------------------------------------------------------
    // SAN parsing
    // -------------------------------------------------------------------

    #[test]
    fn parse_san_pawn_push() {
        let p = Position::starting();
        let mv = parse_san(&p, "e4").unwrap();
        assert_eq!(mv.from, sq("e2"));
        assert_eq!(mv.to, sq("e4"));
    }

    #[test]
    fn parse_san_knight_move() {
        let p = Position::starting();
        let mv = parse_san(&p, "Nf3").unwrap();
        assert_eq!(mv.from, sq("g1"));
        assert_eq!(mv.to, sq("f3"));
    }

    #[test]
    fn parse_san_castling() {
        let p = pos("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1");
        let mv = parse_san(&p, "O-O").unwrap();
        assert_eq!(mv.to.file(), 6);
        assert!(mv.flags.is_castling());
    }

    #[test]
    fn parse_san_promotion() {
        let p = pos("7k/4P3/8/8/8/8/8/4K3 w - - 0 1");
        let mv = parse_san(&p, "e8=Q").unwrap();
        assert_eq!(mv.promotion, Some(PieceType::Queen));
    }

    #[test]
    fn parse_san_capture_with_check() {
        let p = pos("r1bqkbnr/pppppppp/2n5/4P3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2");
        // This might not be a real check, but we just test that + is stripped.
        let result = parse_san(&p, "d5+");
        // Should try to find a pawn push d5 for black.
        // d7d5 is a double push. Should match.
        assert!(result.is_ok() || result.is_err()); // Just shouldn't panic.
    }

    #[test]
    fn parse_san_invalid() {
        let p = Position::starting();
        assert!(parse_san(&p, "Qh5").is_err()); // Queen can't go to h5 from starting position
    }

    // -------------------------------------------------------------------
    // Round-trip: generate SAN then parse it back
    // -------------------------------------------------------------------

    #[test]
    fn san_round_trip_starting_position() {
        let p = Position::starting();
        let legal = movegen::legal_moves(&p);
        for mv in &legal {
            let san_str = move_to_san(&p, *mv, &legal);
            let parsed = parse_san(&p, &san_str).unwrap();
            assert_eq!(
                parsed.to, mv.to,
                "round-trip failed for SAN '{san_str}': expected to={}, got to={}",
                mv.to, parsed.to
            );
            assert_eq!(
                parsed.from, mv.from,
                "round-trip failed for SAN '{san_str}': expected from={}, got from={}",
                mv.from, parsed.from
            );
        }
    }

    #[test]
    fn san_round_trip_kiwipete() {
        let p = pos("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1");
        let legal = movegen::legal_moves(&p);
        for mv in &legal {
            let san_str = move_to_san(&p, *mv, &legal);
            let parsed = parse_san(&p, &san_str).unwrap();
            assert_eq!(
                parsed.to, mv.to,
                "kiwipete round-trip failed for SAN '{san_str}'"
            );
            assert_eq!(
                parsed.from, mv.from,
                "kiwipete round-trip from failed for SAN '{san_str}'"
            );
        }
    }
}