esca 0.3.0

A chess model that answers what is true about a position: variants, positions, games, and move text.
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
//! The position: placement and state, with no rules attached.

use core::fmt;

use cozy_chess as cc;

use crate::error::FenError;
use crate::fen;
use crate::moves::Move;
use crate::types::{Colour, File, Piece, Rank, Role, Square, SquareSet};

/// A Zobrist key.
///
/// Valid as an identity within one process run: the constants it is built
/// from are fixed for the run, not across runs, so a key is not a value to
/// store or to send.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Key(u64);

impl Key {
    /// The key as a number.
    #[inline]
    pub const fn get(self) -> u64 {
        self.0
    }
}

impl fmt::Display for Key {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:016x}", self.0)
    }
}

/// An evaluation of a position. Positive favours the side to move; `Mate(n)`
/// is a forced mate in *n* moves, negative when it is against the side to
/// move.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Score {
    /// Centipawns.
    Cp(i32),
    /// Moves to a forced mate.
    Mate(i32),
}

impl fmt::Display for Score {
    /// The UCI spelling: `cp 25`, `mate -3`.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Score::Cp(cp) => write!(f, "cp {cp}"),
            Score::Mate(n) => write!(f, "mate {n}"),
        }
    }
}

/// Which castlings remain available, each named by its rook's starting file.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub struct CastlingRights {
    /// Indexed by colour, then by 0 for short and 1 for long.
    files: [[Option<File>; 2]; 2],
}

impl CastlingRights {
    /// No rights at all.
    pub const EMPTY: CastlingRights = CastlingRights {
        files: [[None; 2]; 2],
    };

    /// The rights of a classic starting array.
    pub const CLASSIC: CastlingRights = CastlingRights {
        files: [[Some(File::H), Some(File::A)]; 2],
    };

    /// The file of the rook `colour` may castle short with.
    #[inline]
    pub fn short(&self, colour: Colour) -> Option<File> {
        self.files[colour.index()][0]
    }

    /// The file of the rook `colour` may castle long with.
    #[inline]
    pub fn long(&self, colour: Colour) -> Option<File> {
        self.files[colour.index()][1]
    }

    /// Whether `colour` may still castle either way.
    #[inline]
    pub fn any(&self, colour: Colour) -> bool {
        self.short(colour).is_some() || self.long(colour).is_some()
    }

    /// Whether neither side may castle.
    #[inline]
    pub fn is_empty(&self) -> bool {
        !self.any(Colour::White) && !self.any(Colour::Black)
    }

    /// The FEN castling field: `KQkq` when every remaining right names a
    /// classic rook file, the Shredder form `AHah` otherwise, `-` when there
    /// are none.
    pub fn to_fen_field(&self) -> String {
        if self.is_empty() {
            return "-".to_string();
        }
        let classic = Colour::ALL.iter().all(|&colour| {
            self.short(colour).is_none_or(|f| f == File::H)
                && self.long(colour).is_none_or(|f| f == File::A)
        });
        let mut out = String::with_capacity(4);
        for &colour in &Colour::ALL {
            for (side, letter) in [(0usize, 'k'), (1usize, 'q')] {
                if let Some(file) = self.files[colour.index()][side] {
                    let c = if classic { letter } else { file.to_char() };
                    out.push(match colour {
                        Colour::White => c.to_ascii_uppercase(),
                        Colour::Black => c,
                    });
                }
            }
        }
        out
    }

    pub(crate) fn set(&mut self, colour: Colour, short: bool, file: Option<File>) {
        self.files[colour.index()][usize::from(!short)] = file;
    }

    pub(crate) fn to_cozy(self) -> [cc::CastleRights; 2] {
        Colour::ALL.map(|colour| cc::CastleRights {
            short: self.short(colour).map(File::to_cozy),
            long: self.long(colour).map(File::to_cozy),
        })
    }

    fn from_cozy(board: &cc::Board) -> CastlingRights {
        let mut rights = CastlingRights::EMPTY;
        for &colour in &Colour::ALL {
            let cozy = board.castle_rights(colour.to_cozy());
            rights.set(colour, true, cozy.short.map(File::from_cozy));
            rights.set(colour, false, cozy.long.map(File::from_cozy));
        }
        rights
    }
}

impl fmt::Display for CastlingRights {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_fen_field())
    }
}

/// An immutable, variant-agnostic snapshot: placement, side to move, castling
/// rights, en-passant square and clocks.
///
/// Every question that needs rules is asked of a `Variant` or of a `Game`.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Position {
    board: cc::Board,
    halfmove_clock: u32,
    fullmove_number: u32,
    clocks_known: bool,
}

impl Position {
    pub(crate) fn from_parts(
        board: cc::Board,
        halfmove_clock: u32,
        fullmove_number: u32,
        clocks_known: bool,
    ) -> Position {
        // The clocks live here, uncapped; the board keeps its own at the
        // fixed values so that board identity is placement and state only.
        let mut board = board;
        board.set_halfmove_clock(0);
        board.set_fullmove_number(1);
        Position {
            board,
            halfmove_clock,
            fullmove_number,
            clocks_known,
        }
    }

    pub(crate) fn board(&self) -> &cc::Board {
        &self.board
    }

    /// Reads a six-field FEN, or a four-field one (an EPD without
    /// operations), which takes halfmove clock 0, full move 1 and leaves the
    /// clocks marked unknown.
    pub fn from_fen(text: &str) -> Result<Position, FenError> {
        fen::parse(text)
    }

    /// The six-field FEN.
    pub fn fen(&self) -> String {
        fen::format(self, true)
    }

    /// The first four FEN fields.
    pub fn epd(&self) -> String {
        fen::format(self, false)
    }

    /// Whose turn it is.
    #[inline]
    pub fn side_to_move(&self) -> Colour {
        Colour::from_cozy(self.board.side_to_move())
    }

    /// The unit standing on `square`.
    #[inline]
    pub fn piece_at(&self, square: Square) -> Option<Piece> {
        let sq = square.to_cozy();
        let role = self.board.piece_on(sq)?;
        let colour = self.board.color_on(sq)?;
        Some(Piece::new(Role::from_cozy(role), Colour::from_cozy(colour)))
    }

    /// Every square holding a unit of `role`, of either colour.
    #[inline]
    pub fn by_role(&self, role: Role) -> SquareSet {
        SquareSet::from_cozy(self.board.pieces(role.to_cozy()))
    }

    /// Every square holding a unit of `colour`.
    #[inline]
    pub fn by_colour(&self, colour: Colour) -> SquareSet {
        SquareSet::from_cozy(self.board.colors(colour.to_cozy()))
    }

    /// Every square holding exactly `piece`.
    #[inline]
    pub fn by_piece(&self, piece: Piece) -> SquareSet {
        SquareSet::from_cozy(
            self.board
                .colored_pieces(piece.colour.to_cozy(), piece.role.to_cozy()),
        )
    }

    /// Every square holding a unit.
    #[inline]
    pub fn occupied(&self) -> SquareSet {
        SquareSet::from_cozy(self.board.occupied())
    }

    /// Where `colour`'s king stands.
    #[inline]
    pub fn king_of(&self, colour: Colour) -> Square {
        Square::from_cozy(self.board.king(colour.to_cozy()))
    }

    /// The castlings still available.
    #[inline]
    pub fn castling_rights(&self) -> CastlingRights {
        CastlingRights::from_cozy(&self.board)
    }

    /// The square a pawn skipped on the previous ply.
    #[inline]
    pub fn en_passant(&self) -> Option<Square> {
        let file = self.board.en_passant()?;
        let rank = Rank::Sixth.relative_to(self.side_to_move());
        Some(Square::new(File::from_cozy(file), rank))
    }

    /// Plies since the last capture or pawn move.
    #[inline]
    pub fn halfmove_clock(&self) -> u32 {
        self.halfmove_clock
    }

    /// The ordinal of the current full move, from 1.
    #[inline]
    pub fn fullmove_number(&self) -> u32 {
        self.fullmove_number
    }

    /// False when the position came from a four-field FEN.
    #[inline]
    pub fn clocks_known(&self) -> bool {
        self.clocks_known
    }

    /// Whether the side to move stands in check.
    #[inline]
    pub fn in_check(&self) -> bool {
        !self.board.checkers().is_empty()
    }

    /// The units giving check to the side to move.
    #[inline]
    pub fn checkers(&self) -> SquareSet {
        SquareSet::from_cozy(self.board.checkers())
    }

    /// Whether `mv` is legal here by the rules the built-in variants share.
    #[inline]
    pub(crate) fn allows(&self, mv: Move) -> bool {
        self.board.is_legal(mv.to_cozy())
    }

    /// The same placement with the other side to move and no en-passant
    /// square; `None` when the side to move stands in check.
    pub(crate) fn null_move(&self) -> Option<Position> {
        let board = self.board.null_move()?;
        Some(Position::from_parts(
            board,
            self.halfmove_clock,
            self.fullmove_number,
            self.clocks_known,
        ))
    }

    /// The Zobrist key: equal for equal placement, side to move, castling
    /// rights and en-passant square, and independent of the clocks.
    #[inline]
    pub fn key(&self) -> Key {
        Key(self.board.hash())
    }

    /// The Polyglot key: the hash that format indexes a position by.
    ///
    /// Its constants are fixed by the format, so the key is the same number
    /// in every run and in every program that implements it, which is what
    /// makes it a value to store and to send. The en-passant file is part of
    /// it only when a pawn of the side to move stands beside the pawn that
    /// has just advanced two squares.
    #[inline]
    pub fn polyglot_key(&self) -> u64 {
        crate::zobrist::key(self)
    }

    /// The key repetition is counted by: the en-passant square is part of it
    /// only when a pawn could legally take that way, as the FIDE rule says.
    pub(crate) fn repetition_key(&self) -> u64 {
        match self.en_passant() {
            Some(square) if self.en_passant_is_playable(square) => self.board.hash(),
            _ => self.board.hash_without_ep(),
        }
    }

    fn en_passant_is_playable(&self, square: Square) -> bool {
        let colour = self.side_to_move();
        let rank = Rank::Fifth.relative_to(colour);
        [-1isize, 1]
            .into_iter()
            .filter_map(|offset| {
                File::from_index(square.file().index().checked_add_signed(offset)?)
            })
            .any(|file| {
                let from = Square::new(file, rank);
                self.piece_at(from) == Some(Piece::new(Role::Pawn, colour))
                    && self.board.is_legal(cc::Move {
                        from: from.to_cozy(),
                        to: square.to_cozy(),
                        promotion: None,
                    })
            })
    }

    /// The position with the colours swapped and the ranks flipped.
    pub fn mirrored(&self) -> Position {
        let mut builder = cc::BoardBuilder::empty();
        for square in Square::ALL {
            if let Some(piece) = self.piece_at(square) {
                let mirrored = Piece::new(piece.role, !piece.colour);
                builder.board[square.flip_rank().index()] =
                    Some((mirrored.role.to_cozy(), mirrored.colour.to_cozy()));
            }
        }
        builder.side_to_move = (!self.side_to_move()).to_cozy();
        let rights = self.castling_rights();
        let mut mirrored_rights = CastlingRights::EMPTY;
        for &colour in &Colour::ALL {
            mirrored_rights.set(!colour, true, rights.short(colour));
            mirrored_rights.set(!colour, false, rights.long(colour));
        }
        builder.castle_rights = mirrored_rights.to_cozy();
        builder.en_passant = self.en_passant().map(|sq| sq.flip_rank().to_cozy());
        let board = builder.build().expect("a mirrored legal position is legal");
        Position::from_parts(
            board,
            self.halfmove_clock,
            self.fullmove_number,
            self.clocks_known,
        )
    }

    /// Board, side to move and state, for a human reader. The text is not a
    /// stable format.
    pub fn summary(&self) -> String {
        let mut out = String::new();
        for rank in Rank::ALL.iter().rev() {
            out.push(rank.to_char());
            out.push(' ');
            for file in File::ALL {
                out.push(match self.piece_at(Square::new(file, *rank)) {
                    Some(piece) => piece.to_char(),
                    None => '.',
                });
                out.push(' ');
            }
            out.push('\n');
        }
        out.push_str("  a b c d e f g h\n");
        out.push_str(match self.side_to_move() {
            Colour::White => "White to move",
            Colour::Black => "Black to move",
        });
        if self.in_check() {
            out.push_str(", in check");
        }
        out.push('\n');
        let ep = match self.en_passant() {
            Some(square) => square.to_string(),
            None => "-".to_string(),
        };
        out.push_str(&format!(
            "castling {}, en passant {}, halfmove clock {}, move {}\n",
            self.castling_rights().to_fen_field(),
            ep,
            self.halfmove_clock,
            self.fullmove_number,
        ));
        out
    }
}

impl fmt::Display for Position {
    /// The six-field FEN.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.fen())
    }
}