rsshogi 1.2.2

Reusable Rust shogi primitives for board state, move generation, legality, and record parsing.
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
567
568
569
570
571
572
573
574
575
576
//! 1 手分の KI2 記法を表す値型。
//!
//! [`Ki2Notation`] は、KI2 文字列を構成する要素をヒープ確保なしで保持する。
//! 表示、比較、ハッシュ、文字列からの解析、局面上の合法手への解決に使える。

use core::{fmt, str::FromStr};

use crate::board::{self, Move32List, Position};
use crate::types::{Bitboard, Color, File, Move, Move32, PieceType, Rank, Square};

const SIDE_SHIFT: u32 = 0;
const SAME_SHIFT: u32 = 1;
const SQUARE_SHIFT: u32 = 2;
const PIECE_SHIFT: u32 = 9;
const SUFFIX_SHIFT: u32 = 13;
const PROMOTION_SHIFT: u32 = 17;

/// 1 手分の正規化された KI2 記法。
///
/// 値の等価性は [`Move32::to_ki2`] が返す文字列の等価性と一致する。
/// `同` と表記される場合は移動先が文字列に現れないため、この値も移動先を保持しない。
/// 着手へ戻すときは [`Ki2Notation::to_move32`] に局面を渡す必要がある。
///
/// # Examples
///
/// ```
/// use rsshogi::board;
/// use rsshogi::types::Ki2Notation;
///
/// board::init();
/// let position = board::hirate_position();
/// let mv = board::move_from_usi_expect(&position, "7g7f");
/// let notation = mv.to_ki2_notation(&position).expect("legal move has KI2 notation");
///
/// assert_eq!(notation.to_string(), "▲7六歩");
/// assert_eq!("▲7六歩".parse::<Ki2Notation>(), Ok(notation));
/// assert_eq!(notation.to_move32(&position), Ok(mv));
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Ki2Notation(u32);

/// 1 手分の KI2 記法を解析できなかったことを表すエラー。
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ParseKi2NotationError;

impl fmt::Display for ParseKi2NotationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid KI2 notation")
    }
}

impl std::error::Error for ParseKi2NotationError {}

/// KI2 記法を局面上の合法手へ一意に解決できなかったことを表すエラー。
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Ki2ResolveError {
    /// 記法に一致する合法手がない。
    NoMatchingMove,
    /// 記法に一致する合法手が複数ある。
    Ambiguous,
}

impl fmt::Display for Ki2ResolveError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::NoMatchingMove => "KI2 notation does not match a legal move",
            Self::Ambiguous => "KI2 notation matches multiple legal moves",
        })
    }
}

impl std::error::Error for Ki2ResolveError {}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Ki2Suffix {
    None,
    Advance,
    Retreat,
    Sideways,
    Straight,
    Right,
    Left,
    RightAdvance,
    RightRetreat,
    LeftAdvance,
    LeftRetreat,
    Drop,
}

impl Ki2Suffix {
    const fn code(self) -> u32 {
        match self {
            Self::None => 0,
            Self::Advance => 1,
            Self::Retreat => 2,
            Self::Sideways => 3,
            Self::Straight => 4,
            Self::Right => 5,
            Self::Left => 6,
            Self::RightAdvance => 7,
            Self::RightRetreat => 8,
            Self::LeftAdvance => 9,
            Self::LeftRetreat => 10,
            Self::Drop => 11,
        }
    }

    const fn text(self) -> &'static str {
        match self {
            Self::None => "",
            Self::Advance => "",
            Self::Retreat => "",
            Self::Sideways => "",
            Self::Straight => "",
            Self::Right => "",
            Self::Left => "",
            Self::RightAdvance => "右上",
            Self::RightRetreat => "右引",
            Self::LeftAdvance => "左上",
            Self::LeftRetreat => "左引",
            Self::Drop => "",
        }
    }

    fn parse(text: &str) -> Option<Self> {
        Some(match text {
            "" => Self::None,
            "" => Self::Advance,
            "" => Self::Retreat,
            "" => Self::Sideways,
            "" => Self::Straight,
            "" => Self::Right,
            "" => Self::Left,
            "右上" => Self::RightAdvance,
            "右引" => Self::RightRetreat,
            "左上" => Self::LeftAdvance,
            "左引" => Self::LeftRetreat,
            "" => Self::Drop,
            _ => return None,
        })
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Ki2Promotion {
    None,
    Promote,
    Decline,
}

impl Ki2Promotion {
    const fn code(self) -> u32 {
        match self {
            Self::None => 0,
            Self::Promote => 1,
            Self::Decline => 2,
        }
    }

    const fn text(self) -> &'static str {
        match self {
            Self::None => "",
            Self::Promote => "",
            Self::Decline => "不成",
        }
    }
}

impl Ki2Notation {
    /// 局面 `position` で着手 `mv` を指したときの KI2 記法を返す。
    ///
    /// 通常手でない着手、または駒情報を取得できない着手では `None` を返す。
    #[must_use]
    pub fn from_move32(mv: Move32, position: &Position) -> Option<Self> {
        if !mv.is_normal() {
            return None;
        }
        let piece = if mv.has_piece_info() {
            mv.piece_after_move()
        } else {
            position.moved_piece_after(mv)
        };
        if !piece.is_valid() || piece.is_empty() || !mv.to_sq().is_valid() {
            return None;
        }

        let side = piece.color();
        let to = mv.to_sq();
        let same = position.last_move().is_normal() && position.last_move().to_sq() == to;
        let displayed =
            if mv.is_promotion() { piece.piece_type().demote() } else { piece.piece_type() };
        piece_text(displayed)?;

        let (suffix, promotion) = if mv.is_drop() {
            let suffix = if board_candidates(position, to, side, piece.piece_type()).is_empty() {
                Ki2Suffix::None
            } else {
                Ki2Suffix::Drop
            };
            (suffix, Ki2Promotion::None)
        } else {
            let from = mv.from_sq();
            if !from.is_valid() {
                return None;
            }
            let suffix = suffix_of(
                from,
                to,
                side,
                displayed,
                board_candidates(position, to, side, displayed),
            );
            let promotion = if mv.is_promotion() {
                Ki2Promotion::Promote
            } else if position.is_legal_move(Move::promotion(from, to)) {
                Ki2Promotion::Decline
            } else {
                Ki2Promotion::None
            };
            (suffix, promotion)
        };

        Some(Self::pack(side, same, to, displayed, suffix, promotion))
    }

    /// 現在局面でこの記法に一致する合法手を返す。
    ///
    /// # Errors
    ///
    /// 一致する合法手がない場合は [`Ki2ResolveError::NoMatchingMove`]、複数ある場合は
    /// [`Ki2ResolveError::Ambiguous`] を返す。
    pub fn to_move32(self, position: &Position) -> Result<Move32, Ki2ResolveError> {
        let mut moves = Move32List::new();
        board::generate_legal_all_move32(position, &mut moves);
        let mut matched =
            moves.iter().copied().filter(|&mv| Self::from_move32(mv, position) == Some(self));
        let result = matched.next().ok_or(Ki2ResolveError::NoMatchingMove)?;
        if matched.next().is_some() {
            return Err(Ki2ResolveError::Ambiguous);
        }
        Ok(result)
    }

    fn pack(
        side: Color,
        same: bool,
        to: Square,
        displayed: PieceType,
        suffix: Ki2Suffix,
        promotion: Ki2Promotion,
    ) -> Self {
        let square = if same { 0 } else { (to.to_index() as u32) + 1 };
        Self(
            (u32::from(side == Color::WHITE) << SIDE_SHIFT)
                | (u32::from(same) << SAME_SHIFT)
                | (square << SQUARE_SHIFT)
                | ((displayed.raw() as u32) << PIECE_SHIFT)
                | (suffix.code() << SUFFIX_SHIFT)
                | (promotion.code() << PROMOTION_SHIFT),
        )
    }

    fn side(self) -> Color {
        if (self.0 >> SIDE_SHIFT) & 1 == 0 { Color::BLACK } else { Color::WHITE }
    }

    fn is_same_destination(self) -> bool {
        (self.0 >> SAME_SHIFT) & 1 == 1
    }

    fn square(self) -> Square {
        let raw = (self.0 >> SQUARE_SHIFT) & 0x7f;
        debug_assert!(!self.is_same_destination() && (1..=Square::COUNT as u32).contains(&raw));
        Square::from_index((raw - 1) as usize)
    }

    fn piece_type(self) -> PieceType {
        PieceType::new(((self.0 >> PIECE_SHIFT) & 0x0f) as i8)
    }

    fn suffix(self) -> Ki2Suffix {
        match (self.0 >> SUFFIX_SHIFT) & 0x0f {
            0 => Ki2Suffix::None,
            1 => Ki2Suffix::Advance,
            2 => Ki2Suffix::Retreat,
            3 => Ki2Suffix::Sideways,
            4 => Ki2Suffix::Straight,
            5 => Ki2Suffix::Right,
            6 => Ki2Suffix::Left,
            7 => Ki2Suffix::RightAdvance,
            8 => Ki2Suffix::RightRetreat,
            9 => Ki2Suffix::LeftAdvance,
            10 => Ki2Suffix::LeftRetreat,
            11 => Ki2Suffix::Drop,
            _ => unreachable!("Ki2Notation contains a valid suffix"),
        }
    }

    fn promotion(self) -> Ki2Promotion {
        match (self.0 >> PROMOTION_SHIFT) & 0x03 {
            0 => Ki2Promotion::None,
            1 => Ki2Promotion::Promote,
            2 => Ki2Promotion::Decline,
            _ => unreachable!("Ki2Notation contains a valid promotion"),
        }
    }
}

impl fmt::Display for Ki2Notation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(if self.side() == Color::BLACK { "" } else { "" })?;
        if self.is_same_destination() {
            f.write_str("")?;
            if self.suffix() == Ki2Suffix::None && self.promotion() == Ki2Promotion::None {
                f.write_str(" ")?;
            }
        } else {
            let square = self.square();
            write!(
                f,
                "{}{}",
                wide_digit(square.file().raw() + 1),
                kanji_rank(square.rank().raw() + 1)
            )?;
        }
        f.write_str(piece_text(self.piece_type()).expect("Ki2Notation contains a valid piece"))?;
        f.write_str(self.suffix().text())?;
        f.write_str(self.promotion().text())
    }
}

impl fmt::Debug for Ki2Notation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Ki2Notation(\"{self}\")")
    }
}

impl FromStr for Ki2Notation {
    type Err = ParseKi2NotationError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        let mut chars = text.chars();
        let side = match chars.next() {
            Some('') => Color::BLACK,
            Some('') => Color::WHITE,
            _ => return Err(ParseKi2NotationError),
        };
        let first_destination = chars.next().ok_or(ParseKi2NotationError)?;
        let (same, to) = if first_destination == '' {
            if chars.as_str().starts_with(' ') {
                chars.next();
            }
            (true, Square::from_index(0))
        } else {
            let file = file_from_wide_digit(first_destination).ok_or(ParseKi2NotationError)?;
            let rank = rank_from_kanji(chars.next().ok_or(ParseKi2NotationError)?)
                .ok_or(ParseKi2NotationError)?;
            (false, Square::from_file_rank(file, rank))
        };
        let piece = piece_from_text(chars.next().ok_or(ParseKi2NotationError)?)
            .ok_or(ParseKi2NotationError)?;
        let tail = chars.as_str();
        let (suffix_text, promotion) = if let Some(value) = tail.strip_suffix("不成") {
            (value, Ki2Promotion::Decline)
        } else if let Some(value) = tail.strip_suffix('') {
            (value, Ki2Promotion::Promote)
        } else {
            (tail, Ki2Promotion::None)
        };
        let suffix = Ki2Suffix::parse(suffix_text).ok_or(ParseKi2NotationError)?;
        if suffix == Ki2Suffix::Drop && promotion != Ki2Promotion::None {
            return Err(ParseKi2NotationError);
        }
        if suffix == Ki2Suffix::Drop && !piece.is_hand_piece() {
            return Err(ParseKi2NotationError);
        }
        if promotion != Ki2Promotion::None && !piece.is_promotable() {
            return Err(ParseKi2NotationError);
        }
        Ok(Self::pack(side, same, to, piece, suffix, promotion))
    }
}

fn board_candidates(
    position: &Position,
    to: Square,
    side: Color,
    piece_type: PieceType,
) -> Bitboard {
    position.attackers_to_color_current(side, to) & position.pieces_for(piece_type, side)
}

fn suffix_of(
    from: Square,
    to: Square,
    side: Color,
    piece_type: PieceType,
    candidates: Bitboard,
) -> Ki2Suffix {
    if candidates.count() <= 1 {
        return Ki2Suffix::None;
    }
    let horizontal = horizontal_of(from, to, side);
    let motion = motion_of(from, to, side);
    if candidates.iter().filter(|&square| motion_of(square, to, side) == motion).count() == 1 {
        return motion;
    }
    if horizontal == Ki2Suffix::Straight {
        if matches!(
            piece_type,
            PieceType::LANCE
                | PieceType::BISHOP
                | PieceType::ROOK
                | PieceType::HORSE
                | PieceType::DRAGON
        ) {
            if candidates.iter().any(|square| horizontal_of(square, to, side) == Ki2Suffix::Right) {
                return Ki2Suffix::Left;
            }
            if candidates.iter().any(|square| horizontal_of(square, to, side) == Ki2Suffix::Left) {
                return Ki2Suffix::Right;
            }
        }
        return Ki2Suffix::Straight;
    }
    let same_horizontal =
        candidates.iter().filter(|&square| horizontal_of(square, to, side) == horizontal).count();
    if same_horizontal > 1 && candidates.count() != 3 {
        combine(horizontal, motion)
    } else {
        horizontal
    }
}

fn horizontal_of(from: Square, to: Square, side: Color) -> Ki2Suffix {
    let (from_file, _) = oriented(from, side);
    let (to_file, _) = oriented(to, side);
    if from_file < to_file {
        Ki2Suffix::Right
    } else if from_file > to_file {
        Ki2Suffix::Left
    } else {
        Ki2Suffix::Straight
    }
}

fn motion_of(from: Square, to: Square, side: Color) -> Ki2Suffix {
    let (_, from_rank) = oriented(from, side);
    let (_, to_rank) = oriented(to, side);
    if from_rank < to_rank {
        Ki2Suffix::Retreat
    } else if from_rank > to_rank {
        Ki2Suffix::Advance
    } else {
        Ki2Suffix::Sideways
    }
}

const fn combine(horizontal: Ki2Suffix, motion: Ki2Suffix) -> Ki2Suffix {
    match (horizontal, motion) {
        (Ki2Suffix::Right, Ki2Suffix::Advance) => Ki2Suffix::RightAdvance,
        (Ki2Suffix::Right, Ki2Suffix::Retreat) => Ki2Suffix::RightRetreat,
        (Ki2Suffix::Left, Ki2Suffix::Advance) => Ki2Suffix::LeftAdvance,
        (Ki2Suffix::Left, Ki2Suffix::Retreat) => Ki2Suffix::LeftRetreat,
        _ => horizontal,
    }
}

fn oriented(square: Square, side: Color) -> (i8, i8) {
    if side == Color::BLACK {
        (square.file().raw(), square.rank().raw())
    } else {
        (8 - square.file().raw(), 8 - square.rank().raw())
    }
}

fn wide_digit(value: i8) -> char {
    ['', '', '', '', '', '', '', '', ''][(value - 1) as usize]
}

fn file_from_wide_digit(value: char) -> Option<File> {
    "123456789"
        .chars()
        .position(|candidate| candidate == value)
        .map(|raw| File::new(raw as i8))
}

fn kanji_rank(value: i8) -> char {
    ['', '', '', '', '', '', '', '', ''][(value - 1) as usize]
}

fn rank_from_kanji(value: char) -> Option<Rank> {
    "一二三四五六七八九"
        .chars()
        .position(|candidate| candidate == value)
        .map(|raw| Rank::new(raw as i8))
}

fn piece_text(piece_type: PieceType) -> Option<&'static str> {
    Some(match piece_type {
        PieceType::PAWN => "",
        PieceType::LANCE => "",
        PieceType::KNIGHT => "",
        PieceType::SILVER => "",
        PieceType::GOLD => "",
        PieceType::BISHOP => "",
        PieceType::ROOK => "",
        PieceType::KING => "",
        PieceType::PRO_PAWN => "",
        PieceType::PRO_LANCE => "",
        PieceType::PRO_KNIGHT => "",
        PieceType::PRO_SILVER => "",
        PieceType::HORSE => "",
        PieceType::DRAGON => "",
        _ => return None,
    })
}

fn piece_from_text(value: char) -> Option<PieceType> {
    Some(match value {
        '' => PieceType::PAWN,
        '' => PieceType::LANCE,
        '' => PieceType::KNIGHT,
        '' => PieceType::SILVER,
        '' => PieceType::GOLD,
        '' => PieceType::BISHOP,
        '' => PieceType::ROOK,
        '' => PieceType::KING,
        '' => PieceType::PRO_PAWN,
        '' => PieceType::PRO_LANCE,
        '' => PieceType::PRO_KNIGHT,
        '' => PieceType::PRO_SILVER,
        '' => PieceType::HORSE,
        '' => PieceType::DRAGON,
        _ => return None,
    })
}

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

    #[test]
    fn notation_round_trips_and_resolves() {
        board::init();
        let position = board::hirate_position();
        let mv = board::move_from_usi_expect(&position, "7g7f");
        let notation = Ki2Notation::from_move32(mv, &position).expect("legal move has notation");

        assert_eq!(notation.to_string(), "▲7六歩");
        assert_eq!(notation.to_string().parse(), Ok(notation));
        assert_eq!(notation.to_move32(&position), Ok(mv));
    }

    #[test]
    fn notation_is_a_compact_copy_value() {
        assert_eq!(core::mem::size_of::<Ki2Notation>(), 4);
    }

    #[test]
    fn same_destination_padding_is_canonicalized() {
        let padded: Ki2Notation = "△同 角".parse().expect("valid notation");
        let unpadded: Ki2Notation = "△同角".parse().expect("valid notation");

        assert_eq!(padded, unpadded);
        assert_eq!(padded.to_string(), "△同 角");
    }

    #[test]
    fn invalid_notations_are_rejected() {
        for text in ["7六歩", "▲7六歩", "▲76歩", "▲7六王", "▲7六金打成", "▲7六と打", ""]
        {
            assert_eq!(text.parse::<Ki2Notation>(), Err(ParseKi2NotationError), "{text}");
        }
    }
}