rsshogi 1.0.1

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
//! 持ち駒の型定義
//!
//! [`Hand`] は各プレイヤーの持ち駒を `u32` のビットフィールドで管理する。
//! 7 種の駒(歩、香、桂、銀、角、飛、金)の枚数を 1 ワードにパックし、
//! 加減算やビット演算で高速に操作できる。
//!
//! [`HandPiece`] は持ち駒の駒種インデックス(0~6)を表す。
//!
//! # ビットレイアウト
//!
//! ```text
//! bit  0.. 4: 歩 (5bit, 最大 18 枚)
//! bit  8..10: 香 (3bit, 最大 4 枚)
//! bit 12..14: 桂 (3bit, 最大 4 枚)
//! bit 16..18: 銀 (3bit, 最大 4 枚)
//! bit 20..21: 角 (2bit, 最大 2 枚)
//! bit 24..25: 飛 (2bit, 最大 2 枚)
//! bit 28..30: 金 (3bit, 最大 4 枚)
//! ```

use super::PieceType;

/// 持ち駒の駒種
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct HandPiece(i8);

const _: () = {
    assert!(core::mem::size_of::<HandPiece>() == 1);
    assert!(core::mem::align_of::<HandPiece>() == 1);
};

/// 全駒種の有効ビットを結合したマスク。持ち駒の存在判定やビット操作に使用。
pub const HAND_BIT_MASK: u32 =
    0x1f | (0x7 << 8) | (0x7 << 12) | (0x7 << 16) | (0x3 << 20) | (0x3 << 24) | (0x7 << 28);
/// 各駒種フィールドの上位 1 ビット(繰り下がり検出用)。
/// 持ち駒の優劣比較で使用する。
pub const HAND_BORROW_MASK: u32 = (HAND_BIT_MASK << 1) & !HAND_BIT_MASK;
impl HandPiece {
    // 持ち駒定数(歩から金まで)
    pub const PAWN: Self = Self(0);
    pub const LANCE: Self = Self(1);
    pub const KNIGHT: Self = Self(2);
    pub const SILVER: Self = Self(3);
    pub const BISHOP: Self = Self(4);
    pub const ROOK: Self = Self(5);
    pub const GOLD: Self = Self(6);
    pub const COUNT: usize = 7;

    /// 内部値から `HandPiece` を生成する。
    ///
    /// 値の妥当性は呼び出し側が保証する(互換性とテスト用途を想定)。
    #[inline]
    #[must_use]
    pub const fn new(raw: i8) -> Self {
        Self(raw)
    }

    /// `PieceType` から変換
    #[must_use]
    pub const fn from_piece_type(pt: PieceType) -> Option<Self> {
        match pt {
            PieceType::PAWN => Some(Self::PAWN),
            PieceType::LANCE => Some(Self::LANCE),
            PieceType::KNIGHT => Some(Self::KNIGHT),
            PieceType::SILVER => Some(Self::SILVER),
            PieceType::BISHOP => Some(Self::BISHOP),
            PieceType::ROOK => Some(Self::ROOK),
            PieceType::GOLD => Some(Self::GOLD),
            _ => None,
        }
    }

    /// `PieceType` へ変換
    #[must_use]
    pub const fn to_piece_type(self) -> PieceType {
        match self {
            Self::PAWN => PieceType::PAWN,
            Self::LANCE => PieceType::LANCE,
            Self::KNIGHT => PieceType::KNIGHT,
            Self::SILVER => PieceType::SILVER,
            Self::BISHOP => PieceType::BISHOP,
            Self::ROOK => PieceType::ROOK,
            Self::GOLD => PieceType::GOLD,
            _ => PieceType::NONE,
        }
    }

    /// 有効な値かどうか判定
    #[must_use]
    pub const fn is_valid(self) -> bool {
        self.raw() >= 0 && self.raw() <= Self::GOLD.raw()
    }

    /// 内部値を取得する(主にテスト用)
    #[inline]
    #[must_use]
    pub const fn raw(self) -> i8 {
        self.0
    }

    /// 全ての持ち駒種(PAWN, LANCE, KNIGHT, SILVER, BISHOP, ROOK, GOLD)を順番にイテレート
    ///
    /// # Examples
    ///
    /// ```
    /// use rsshogi::types::HandPiece;
    ///
    /// let pieces: Vec<_> = HandPiece::iter().collect();
    /// assert_eq!(pieces.len(), 7);
    /// ```
    #[inline]
    #[must_use]
    pub fn iter() -> impl DoubleEndedIterator<Item = Self> + ExactSizeIterator {
        (0..Self::COUNT).map(|i| Self::new(i as i8))
    }
}

/// 持ち駒
/// ビットフィールド構成:
/// - 歩: bit0-4 (5bit, 最大31枚)
/// - 香: bit8-10 (3bit, 最大7枚)
/// - 桂: bit12-14 (3bit, 最大7枚)
/// - 銀: bit16-18 (3bit, 最大7枚)
/// - 角: bit20-21 (2bit, 最大3枚)
/// - 飛: bit24-25 (2bit, 最大3枚)
/// - 金: bit28-30 (3bit, 最大7枚)
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct Hand(u32);

const _: () = {
    assert!(core::mem::size_of::<Hand>() == 4);
    assert!(core::mem::align_of::<Hand>() == 4);
};

impl Hand {
    pub const ZERO: Self = Self(0);

    /// 生のビット表現から `Hand` を生成する(互換性と低レベル用途)。
    #[inline]
    #[must_use]
    pub const fn from_bits(bits: u32) -> Self {
        Self(bits)
    }

    /// 内部のビット表現を取得する(低レベル用途とテスト用)。
    #[inline]
    #[must_use]
    pub const fn bits(self) -> u32 {
        self.0
    }

    // ビット配置定数(開始ビット位置)
    const PIECE_BITS: [u32; 7] = [
        0,  // 歩: bit 0から
        8,  // 香: bit 8から
        12, // 桂: bit 12から
        16, // 銀: bit 16から
        20, // 角: bit 20から
        24, // 飛: bit 24から
        28, // 金: bit 28から
    ];

    // ビットマスク(各駒種のビット幅)
    const PIECE_BIT_MASK: [u32; 7] = [
        0x1f, // 歩: 5bit
        0x07, // 香: 3bit
        0x07, // 桂: 3bit
        0x07, // 銀: 3bit
        0x03, // 角: 2bit
        0x03, // 飛: 2bit
        0x07, // 金: 3bit
    ];

    // 単体マスク(1枚分の値)
    const PIECE_BIT_ONE: [u32; 7] = [
        1 << 0,  //        1 << 8,  //        1 << 12, //        1 << 16, //        1 << 20, //        1 << 24, //        1 << 28, //    ];

    // 優劣判定用マスク

    /// 指定した駒種の枚数を取得
    #[inline]
    #[must_use]
    pub const fn count_of(hand: Self, hp: HandPiece) -> u32 {
        if let Some(idx) = Self::index_for(hp) {
            let shift = Self::PIECE_BITS[idx];
            let mask = Self::PIECE_BIT_MASK[idx];
            (hand.0 >> shift) & mask
        } else {
            0
        }
    }

    /// 指定した駒種が存在するか判定
    #[inline]
    #[must_use]
    pub const fn has(hand: Self, hp: HandPiece) -> bool {
        Self::count_of(hand, hp) > 0
    }

    /// 指定した駒種を1枚追加
    #[inline]
    #[must_use]
    pub const fn add_one(hand: Self, hp: HandPiece) -> Self {
        if let Some(idx) = Self::index_for(hp) {
            Self(hand.0 + Self::PIECE_BIT_ONE[idx])
        } else {
            hand
        }
    }

    /// 指定した駒種を1枚減らす。
    #[inline]
    #[must_use]
    pub const fn sub_one(hand: Self, hp: HandPiece) -> Self {
        if let Some(idx) = Self::index_for(hp) {
            Self(hand.0 - Self::PIECE_BIT_ONE[idx])
        } else {
            hand
        }
    }

    /// h1がh2以上の持ち駒を持っているか判定
    /// `HAND_BORROW_MASK` を使った高速判定
    #[must_use]
    pub const fn is_equal_or_superior(h1: Self, h2: Self) -> bool {
        (h1.0.wrapping_sub(h2.0) & HAND_BORROW_MASK) == 0
    }

    /// ビットフィールドにオーバーフロー(borrow)が発生しているか判定
    ///
    /// # Examples
    ///
    /// ```
    /// use rsshogi::types::{Hand, HandPiece};
    ///
    /// let mut hand = Hand::ZERO;
    /// for _ in 0..31 {
    ///     hand = Hand::add_one(hand, HandPiece::PAWN);
    /// }
    /// assert!(!hand.has_overflow());
    ///
    /// hand = Hand::add_one(hand, HandPiece::PAWN);
    /// assert!(hand.has_overflow());
    /// ```
    #[must_use]
    #[inline]
    pub const fn has_overflow(self) -> bool {
        (self.0 & HAND_BORROW_MASK) != 0
    }

    /// 指定した駒種を `n` 枚追加し、ビットフィールド幅を超えなければ新しい `Hand` を返す。
    ///
    /// フィールド幅(歩5bit、香3bit...)を超える場合は `None` を返す。
    ///
    /// # Examples
    ///
    /// ```
    /// use rsshogi::types::{Hand, HandPiece};
    ///
    /// let hand = Hand::ZERO.checked_add(HandPiece::PAWN, 18).unwrap();
    /// assert_eq!(hand.count(HandPiece::PAWN), 18);
    ///
    /// assert!(hand.checked_add(HandPiece::PAWN, 32).is_none());
    /// ```
    #[must_use]
    #[inline]
    pub fn checked_add(self, hp: HandPiece, n: u32) -> Option<Self> {
        let hand = if let Some(idx) = Self::index_for(hp) {
            let shift = Self::PIECE_BITS[idx];
            let mask = Self::PIECE_BIT_MASK[idx];
            let current = (self.0 >> shift) & mask;
            let next = current.checked_add(n)?;
            if next > mask {
                return None;
            }
            let cleared = self.0 & !(mask << shift);
            Self(cleared | (next << shift))
        } else {
            self
        };

        if hand.has_overflow() { None } else { Some(hand) }
    }

    // 簡便なメソッド追加
    /// 指定した駒種の枚数を取得
    #[inline]
    #[must_use]
    pub const fn count(&self, hp: HandPiece) -> u32 {
        Self::count_of(*self, hp)
    }

    /// 指定した駒種を指定枚数追加
    #[inline]
    pub fn add(&mut self, hp: HandPiece, n: u32) {
        if let Some(idx) = Self::index_for(hp) {
            self.0 += Self::PIECE_BIT_ONE[idx] * n;
        }
    }

    /// 指定した駒種を指定枚数減らす
    #[inline]
    pub fn sub(&mut self, hp: HandPiece, n: u32) {
        if let Some(idx) = Self::index_for(hp) {
            self.0 -= Self::PIECE_BIT_ONE[idx] * n;
        }
    }

    /// 歩の枚数を取得
    #[inline]
    #[must_use]
    pub const fn pawn_count(&self) -> u32 {
        self.count(HandPiece::PAWN)
    }

    /// 持ち駒種に対応するインデックス
    const fn index_for(hp: HandPiece) -> Option<usize> {
        match hp {
            HandPiece::PAWN => Some(0),
            HandPiece::LANCE => Some(1),
            HandPiece::KNIGHT => Some(2),
            HandPiece::SILVER => Some(3),
            HandPiece::BISHOP => Some(4),
            HandPiece::ROOK => Some(5),
            HandPiece::GOLD => Some(6),
            _ => None,
        }
    }
}

impl Default for Hand {
    fn default() -> Self {
        Self::ZERO
    }
}

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

    #[test]
    fn test_hand_piece_constants() {
        assert_eq!(HandPiece::PAWN.raw(), 0);
        assert_eq!(HandPiece::LANCE.raw(), 1);
        assert_eq!(HandPiece::KNIGHT.raw(), 2);
        assert_eq!(HandPiece::SILVER.raw(), 3);
        assert_eq!(HandPiece::BISHOP.raw(), 4);
        assert_eq!(HandPiece::ROOK.raw(), 5);
        assert_eq!(HandPiece::GOLD.raw(), 6);
        assert_eq!(HandPiece::COUNT, 7);
    }

    #[test]
    fn test_hand_piece_conversion() {
        // from_piece_type -> to_piece_type の往復変換確認
        let test_cases = [
            (PieceType::PAWN, HandPiece::PAWN),
            (PieceType::LANCE, HandPiece::LANCE),
            (PieceType::KNIGHT, HandPiece::KNIGHT),
            (PieceType::SILVER, HandPiece::SILVER),
            (PieceType::BISHOP, HandPiece::BISHOP),
            (PieceType::ROOK, HandPiece::ROOK),
            (PieceType::GOLD, HandPiece::GOLD),
        ];

        for (pt, hp) in test_cases {
            assert_eq!(HandPiece::from_piece_type(pt), Some(hp));
            assert_eq!(hp.to_piece_type(), pt);
        }

        // 不正な駒種
        assert_eq!(HandPiece::from_piece_type(PieceType::KING), None);
        assert_eq!(HandPiece::from_piece_type(PieceType::PRO_PAWN), None);
        assert_eq!(HandPiece::from_piece_type(PieceType::NONE), None);
    }

    #[test]
    fn test_hand_piece_is_ok() {
        // 有効な値
        for i in 0..7 {
            assert!(HandPiece::new(i).is_valid());
        }

        // 無効な値
        assert!(!HandPiece::new(-1).is_valid());
        assert!(!HandPiece::new(7).is_valid());
        assert!(!HandPiece::new(100).is_valid());
    }

    #[test]
    fn test_hand_bit_packing() {
        // 空の持ち駒
        assert_eq!(Hand::ZERO.bits(), 0);

        // 各駒種を1枚ずつ追加
        let mut hand = Hand::ZERO;

        // 歩を1枚追加
        hand = Hand::add_one(hand, HandPiece::PAWN);
        assert_eq!(Hand::count_of(hand, HandPiece::PAWN), 1);
        assert_eq!(hand.bits(), 1);

        // 香を1枚追加
        hand = Hand::add_one(hand, HandPiece::LANCE);
        assert_eq!(Hand::count_of(hand, HandPiece::LANCE), 1);
        assert_eq!(hand.bits(), 1 | (1 << 8));

        // 桂を1枚追加
        hand = Hand::add_one(hand, HandPiece::KNIGHT);
        assert_eq!(Hand::count_of(hand, HandPiece::KNIGHT), 1);
        assert_eq!(hand.bits(), 1 | (1 << 8) | (1 << 12));
    }

    #[test]
    fn test_hand_add_sub() {
        let mut hand = Hand::ZERO;

        // 歩を5枚追加
        for _ in 0..5 {
            hand = Hand::add_one(hand, HandPiece::PAWN);
        }
        assert_eq!(Hand::count_of(hand, HandPiece::PAWN), 5);

        // 歩を2枚減らす
        hand.sub(HandPiece::PAWN, 2);
        assert_eq!(Hand::count_of(hand, HandPiece::PAWN), 3);

        // 角を2枚追加
        hand = Hand::add_one(hand, HandPiece::BISHOP);
        hand = Hand::add_one(hand, HandPiece::BISHOP);
        assert_eq!(Hand::count_of(hand, HandPiece::BISHOP), 2);

        // 飛車を1枚追加
        hand = Hand::add_one(hand, HandPiece::ROOK);
        assert_eq!(Hand::count_of(hand, HandPiece::ROOK), 1);
    }

    #[test]
    fn test_hand_exists() {
        let mut hand = Hand::ZERO;

        // 最初は何も持っていない
        assert!(!Hand::has(hand, HandPiece::PAWN));
        assert!(!Hand::has(hand, HandPiece::GOLD));

        // 金を1枚追加
        hand = Hand::add_one(hand, HandPiece::GOLD);
        assert!(Hand::has(hand, HandPiece::GOLD));
        assert!(!Hand::has(hand, HandPiece::PAWN));
    }

    #[test]
    fn test_hand_is_equal_or_superior() {
        let h1 = Hand::ZERO;
        let mut h2 = Hand::ZERO;

        // 同じ持ち駒
        assert!(Hand::is_equal_or_superior(h1, h2));

        // h1の方が少ない
        h2 = Hand::add_one(h2, HandPiece::PAWN);
        assert!(!Hand::is_equal_or_superior(h1, h2));

        // h1の方が多い
        let mut h3 = Hand::add_one(h1, HandPiece::PAWN);
        h3 = Hand::add_one(h3, HandPiece::PAWN);
        assert!(Hand::is_equal_or_superior(h3, h2));

        // 複数種類の駒での比較
        let mut h4 = Hand::ZERO;
        let mut h5 = Hand::ZERO;

        // h4: 歩3, 金1
        for _ in 0..3 {
            h4 = Hand::add_one(h4, HandPiece::PAWN);
        }
        h4 = Hand::add_one(h4, HandPiece::GOLD);

        // h5: 歩2, 金1
        for _ in 0..2 {
            h5 = Hand::add_one(h5, HandPiece::PAWN);
        }
        h5 = Hand::add_one(h5, HandPiece::GOLD);

        assert!(Hand::is_equal_or_superior(h4, h5));
        assert!(!Hand::is_equal_or_superior(h5, h4));
    }

    #[test]
    fn test_hand_overflow_boundary() {
        let mut hand = Hand::ZERO;

        // 歩を最大枚数(18枚)近くまで追加
        for _ in 0..18 {
            hand = Hand::add_one(hand, HandPiece::PAWN);
        }
        assert_eq!(Hand::count_of(hand, HandPiece::PAWN), 18);

        // 金を最大枚数(4枚)追加
        for _ in 0..4 {
            hand = Hand::add_one(hand, HandPiece::GOLD);
        }
        assert_eq!(Hand::count_of(hand, HandPiece::GOLD), 4);

        // 他の駒種が影響を受けていないことを確認
        assert_eq!(Hand::count_of(hand, HandPiece::PAWN), 18);
        assert_eq!(Hand::count_of(hand, HandPiece::LANCE), 0);
    }

    #[test]
    fn test_hand_has_overflow() {
        let mut hand = Hand::ZERO;
        for _ in 0..31 {
            hand = Hand::add_one(hand, HandPiece::PAWN);
        }
        assert!(!hand.has_overflow());

        hand = Hand::add_one(hand, HandPiece::PAWN);
        assert!(hand.has_overflow());
    }

    #[test]
    fn test_hand_checked_add() {
        let hand =
            Hand::ZERO.checked_add(HandPiece::PAWN, 18).expect("pawn count within field width");
        assert_eq!(hand.count(HandPiece::PAWN), 18);
        assert!(!hand.has_overflow());

        let hand =
            Hand::ZERO.checked_add(HandPiece::GOLD, 7).expect("gold count within field width");
        assert_eq!(hand.count(HandPiece::GOLD), 7);
        assert!(hand.checked_add(HandPiece::GOLD, 1).is_none());

        assert!(Hand::ZERO.checked_add(HandPiece::PAWN, 32).is_none());
    }
}