timecat/utils/
moves.rs

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

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub struct Move {
    source: Square,
    dest: Square,
    promotion: Option<PieceType>,
}

impl Move {
    #[inline]
    pub const fn new_unchecked(source: Square, dest: Square, promotion: Option<PieceType>) -> Self {
        Self {
            source,
            dest,
            promotion,
        }
    }

    #[inline]
    pub const fn new(source: Square, dest: Square, promotion: Option<PieceType>) -> Result<Self> {
        if source.to_int() == dest.to_int() {
            return Err(TimecatError::InvalidMoveStructGeneration);
        }
        Ok(Self::new_unchecked(source, dest, promotion))
    }

    #[inline]
    pub const fn get_source(&self) -> Square {
        self.source
    }

    #[inline]
    pub const fn get_dest(&self) -> Square {
        self.dest
    }

    #[inline]
    pub const fn get_promotion(&self) -> Option<PieceType> {
        self.promotion
    }

    #[inline]
    pub fn from_uci(uci: &str) -> Result<Self> {
        Self::from_str(uci)
    }

    pub fn from_san(position: &BoardPosition, san: &str) -> Result<Self> {
        // TODO: Make the logic better
        let san = san.trim().replace('0', "O");
        for move_ in position.generate_legal_moves() {
            if move_.san(position).unwrap() == san {
                return Ok(move_);
            }
        }
        Err(TimecatError::InvalidSanMoveString { s: san.to_string() })
    }

    pub fn from_lan(position: &BoardPosition, lan: &str) -> Result<Self> {
        // TODO: Make the logic better
        let lan = lan.trim().replace('0', "O");
        let lan = lan.replace('0', "O");
        for valid_or_null_move in position.generate_legal_moves() {
            if valid_or_null_move.lan(position).unwrap() == lan {
                return Ok(valid_or_null_move);
            }
        }
        Err(TimecatError::InvalidLanMoveString { s: lan.to_string() })
    }

    pub fn algebraic_without_suffix(self, position: &BoardPosition, long: bool) -> Result<String> {
        let source = self.get_source();
        let dest = self.get_dest();

        // Castling.
        if position.is_castling(self) {
            return if dest.get_file() < source.get_file() {
                Ok("O-O-O".to_string())
            } else {
                Ok("O-O".to_string())
            };
        }

        let piece =
            position
                .get_piece_type_at(source)
                .ok_or(TimecatError::InvalidSanOrLanMove {
                    valid_or_null_move: self.into(),
                    fen: position.get_fen(),
                })?;
        let capture = position.is_capture(self);
        let mut san = if piece == Pawn {
            String::new()
        } else {
            piece.to_colored_piece_string(White)
        };

        if long {
            san += &source.to_string();
        } else if piece != Pawn {
            // Get ambiguous move candidates.
            // Relevant candidates: not exactly the current move,
            // but to the same square.
            let mut others = BitBoard::EMPTY;
            let from_mask =
                position.get_piece_mask(piece) & position.self_occupied() & !source.to_bitboard();
            let to_mask = dest.to_bitboard();
            for candidate in position.generate_masked_legal_moves(from_mask, to_mask) {
                others |= candidate.get_source().to_bitboard();
            }

            // Disambiguate.
            if !others.is_empty() {
                let (mut row, mut column) = (false, false);
                if !(others & source.get_rank_bb()).is_empty() {
                    column = true;
                }
                if !(others & source.get_file_bb()).is_empty() {
                    row = true;
                } else {
                    column = true;
                }
                if column {
                    san.push(
                        "abcdefgh"
                            .chars()
                            .nth(source.get_file().to_index())
                            .unwrap(),
                    );
                }
                if row {
                    san += &(source.get_rank().to_index() + 1).to_string();
                }
            }
        } else if capture {
            san.push(
                "abcdefgh"
                    .chars()
                    .nth(source.get_file().to_index())
                    .unwrap(),
            );
        }

        // Captures.
        if capture {
            san += "x";
        } else if long {
            san += "-";
        }

        // Destination square.
        san += &dest.to_string();

        // Promotion.
        if let Some(promotion) = self.get_promotion() {
            san += &format!("={}", promotion.to_colored_piece_string(White))
        }

        Ok(san)
    }

    pub fn algebraic_and_new_position(
        self,
        position: &BoardPosition,
        long: bool,
    ) -> Result<(String, BoardPosition)> {
        let san = self.algebraic_without_suffix(position, long)?;

        // Look ahead for check or checkmate.
        let new_position = position.make_move_new(self);
        let is_checkmate = new_position.is_checkmate();

        // Add check or checkmate suffix.
        let san = if is_checkmate {
            san + "#"
        } else if new_position.is_check() {
            san + "+"
        } else {
            san
        };
        Ok((san, new_position))
    }

    #[cfg(feature = "pyo3")]
    fn from_py_move(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
        let source = ob.getattr("from_square")?.extract()?;
        let dest = ob.getattr("to_square")?.extract()?;
        let promotion = ob.getattr("promotion")?.extract()?;
        Ok(Self::new(source, dest, promotion)?)
    }
}

impl FromStr for Move {
    type Err = TimecatError;

    fn from_str(mut s: &str) -> Result<Self> {
        let error = TimecatError::InvalidUciMoveString { s: s.to_string() };
        s = s.trim();
        if s.len() > 6 {
            return Err(error.clone());
        }
        let source = Square::from_str(s.get(0..2).ok_or(error.clone())?)?;
        let dest = Square::from_str(s.get(2..4).ok_or(error.clone())?)?;

        let mut promotion = None;
        if s.len() == 5 {
            promotion = Some(match s.chars().last().ok_or(error.clone())? {
                'q' => Queen,
                'r' => Rook,
                'n' => Knight,
                'b' => Bishop,
                _ => return Err(error.clone()),
            });
        }

        Self::new(source, dest, promotion)
    }
}

impl fmt::Display for Move {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.promotion {
            Some(piece) => write!(f, "{}{}{}", self.source, self.dest, piece),
            None => write!(f, "{}{}", self.source, self.dest),
        }
    }
}

#[cfg(feature = "pyo3")]
impl<'source> FromPyObject<'source> for Move {
    fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult<Self> {
        if let Ok(move_text) = ob.extract::<&str>() {
            if let Ok(move_) = Self::from_str(move_text) {
                return Ok(move_);
            }
        }
        if let Ok(move_) = Self::from_py_move(ob) {
            return Ok(move_);
        }
        Err(Pyo3Error::Pyo3TypeConversionError {
            from: ob.to_string(),
            to: std::any::type_name::<Self>().to_string(),
        }
        .into())
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
#[repr(transparent)]
pub struct ValidOrNullMove(Option<Move>);

impl ValidOrNullMove {
    #[expect(non_upper_case_globals)]
    pub const NullMove: Self = Self(None);

    #[inline]
    pub const fn new_unchecked(source: Square, dest: Square, promotion: Option<PieceType>) -> Self {
        Self(Some(Move::new_unchecked(source, dest, promotion)))
    }

    #[inline]
    pub fn new(source: Square, dest: Square, promotion: Option<PieceType>) -> Result<Self> {
        Ok(Self(Some(Move::new(source, dest, promotion)?)))
    }

    #[inline]
    pub const fn into_inner(&self) -> Option<&Move> {
        self.0.as_ref()
    }

    #[inline]
    pub const fn into_inner_mut(&mut self) -> Option<&mut Move> {
        self.0.as_mut()
    }

    #[inline]
    pub const fn is_null(&self) -> bool {
        self.into_inner().is_none()
    }

    #[inline]
    pub fn get_source(&self) -> Option<Square> {
        self.into_inner().map(|move_| move_.source)
    }

    #[inline]
    pub fn get_dest(&self) -> Option<Square> {
        self.into_inner().map(|move_| move_.dest)
    }

    #[inline]
    pub fn get_promotion(&self) -> Option<PieceType> {
        self.into_inner()?.promotion
    }

    pub fn from_san(position: &BoardPosition, san: &str) -> Result<Self> {
        // TODO: Make the logic better
        let san = san.trim();
        if san == "--" || san == "0000" {
            return Ok(ValidOrNullMove::NullMove);
        }
        Ok(Move::from_san(position, san)?.into())
    }

    pub fn from_lan(position: &BoardPosition, lan: &str) -> Result<Self> {
        // TODO: Make the logic better
        let lan = lan.trim();
        if lan == "--" || lan == "0000" {
            return Ok(ValidOrNullMove::NullMove);
        }
        Ok(Move::from_lan(position, lan)?.into())
    }

    #[inline]
    pub fn algebraic_without_suffix(self, position: &BoardPosition, long: bool) -> Result<String> {
        self.map(|move_| move_.algebraic_without_suffix(position, long))
            .unwrap_or(Ok("--".to_string()))
    }

    #[inline]
    pub fn algebraic_and_new_position(
        self,
        position: &BoardPosition,
        long: bool,
    ) -> Result<(String, BoardPosition)> {
        self.map(|move_| move_.algebraic_and_new_position(position, long))
            .unwrap_or(Ok(("--".to_string(), position.null_move()?)))
    }
}

impl Default for ValidOrNullMove {
    fn default() -> Self {
        Self::NullMove
    }
}

impl fmt::Display for ValidOrNullMove {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(move_) = self.into_inner() {
            write!(f, "{}", move_)
        } else {
            write!(f, "--")
        }
    }
}

impl FromStr for ValidOrNullMove {
    type Err = TimecatError;

    fn from_str(s: &str) -> Result<Self> {
        if s == "--" || s == "0000" {
            return Ok(Self::NullMove);
        }
        Ok(Move::from_str(s)?.into())
    }
}

impl From<Move> for ValidOrNullMove {
    #[inline]
    fn from(value: Move) -> Self {
        Some(value).into()
    }
}

impl From<&Move> for ValidOrNullMove {
    #[inline]
    fn from(value: &Move) -> Self {
        Some(value).into()
    }
}

impl From<Option<Move>> for ValidOrNullMove {
    #[inline]
    fn from(value: Option<Move>) -> Self {
        Self(value)
    }
}

impl From<Option<&Move>> for ValidOrNullMove {
    #[inline]
    fn from(value: Option<&Move>) -> Self {
        value.copied().into()
    }
}

impl Deref for ValidOrNullMove {
    type Target = Option<Move>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for ValidOrNullMove {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

#[cfg(feature = "pyo3")]
impl<'source> FromPyObject<'source> for ValidOrNullMove {
    fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult<Self> {
        if let Ok(move_) = ob.extract::<Move>() {
            return Ok(move_.into());
        }
        if let Ok(move_text) = ob.extract::<&str>() {
            if let Ok(valid_or_null_move) = Self::from_str(move_text) {
                return Ok(valid_or_null_move);
            }
        }
        Err(Pyo3Error::Pyo3TypeConversionError {
            from: ob.to_string(),
            to: std::any::type_name::<Self>().to_string(),
        }
        .into())
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct WeightedMove {
    pub move_: Move,
    pub weight: MoveWeight,
}

impl PartialOrd for WeightedMove {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for WeightedMove {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.weight.cmp(&other.weight)
    }
}

impl WeightedMove {
    #[inline]
    pub fn new(move_: Move, weight: MoveWeight) -> Self {
        Self { move_, weight }
    }
}

impl fmt::Display for WeightedMove {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "({}, {})", self.move_, self.weight)
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum CastleMoveType {
    KingSide,
    QueenSide,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, Hash)]
pub enum MoveType {
    Capture {
        is_en_passant: bool,
    },
    Castle(CastleMoveType),
    DoublePawnPush,
    Promotion(PieceType),
    #[default]
    Other,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MoveWithInfo {
    valid_or_null_move: ValidOrNullMove,
    type_: MoveType,
    is_check: bool,
}