rsshogi 1.0.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
use super::helpers::{apply_usi_move, move_from_usi};
use super::*;
use crate::types::{Color, HandPiece, PieceType, RepetitionState};

#[test]
// StateStack初期化時にrepetition_counterとplies_from_nullが0で始まることを確認
fn test_repetition_state_initializes_to_zero() {
    let pos = crate::board::hirate_position();

    assert_eq!(pos.state_stack().current().repetition_counter, 0);
    assert_eq!(pos.state_stack().current().plies_from_null, 0);
}

#[test]
// 通常手のみを進めた場合にplies_from_nullが手数分だけ増えるか検証
fn test_plies_from_null_increments_without_events() {
    let mut pos = crate::board::hirate_position();

    apply_usi_move(&mut pos, "2g2f");
    assert_eq!(pos.state_stack().current().plies_from_null, 1);

    apply_usi_move(&mut pos, "8c8d");
    assert_eq!(pos.state_stack().current().plies_from_null, 2);
}

#[test]
// 駒取り発生時にもplies_from_nullがリセットされないことを検証
fn test_plies_from_null_does_not_reset_on_capture() {
    let mut pos = crate::board::hirate_position();

    // ▲7六歩△8四歩▲8六歩△8五歩
    for usi in ["7g7f", "8c8d", "8g8f", "8d8e"] {
        apply_usi_move(&mut pos, usi);
    }

    assert_eq!(pos.state_stack().current().plies_from_null, 4);

    // ▲8五歩×同歩 (駒取りでも継続)
    apply_usi_move(&mut pos, "8f8e");

    assert_eq!(pos.state_stack().current().plies_from_null, 5);
    assert_ne!(pos.state_stack().current().cold.captured_piece(), Piece::NONE);
}

#[test]
// 駒打ち発生時にもplies_from_nullがリセットされないことを検証
fn test_plies_from_null_does_not_reset_on_drop() {
    let mut pos = crate::board::hirate_position();

    // ▲7六歩△3四歩▲2二角成△同銀
    for usi in ["7g7f", "3c3d", "8h2b+", "3a2b"] {
        apply_usi_move(&mut pos, usi);
    }

    assert_eq!(pos.state_stack().current().plies_from_null, 4);
    assert_eq!(pos.turn(), Color::BLACK);

    // 持ち駒に角が1枚あることを確認
    let hand_piece = HandPiece::from_piece_type(PieceType::BISHOP).unwrap();
    let count_of = pos.hand(pos.turn()).count(hand_piece);
    assert_eq!(count_of, 1);

    // ▲4五角(角を打った)
    let drop_mv = move_from_usi(&pos, "B*4e");
    pos.apply_move32(drop_mv);

    assert_eq!(pos.state_stack().current().plies_from_null, 5);
}

#[test]
// 同一局面に戻った際にrepetition_counterがインクリメントされることを確認
fn test_repetition_counter_increments_when_position_repeats() {
    let mut pos = crate::board::hirate_position();

    // ▲3八飛△7二飛▲2八飛△8二飛
    let sequences = ["2h3h", "8b7b", "3h2h", "7b8b"];
    let initial_zobrist = pos.key();

    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }

    assert_eq!(pos.key(), initial_zobrist);
    assert_eq!(pos.state_stack().current().repetition_counter, 1);

    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }

    assert_eq!(pos.key(), initial_zobrist);
    assert_eq!(pos.state_stack().current().repetition_counter, 2);
}

#[test]
// repetition_counterを用いた三回(三度目)繰り返し判定を検証
fn test_is_repetition_detects_threefold() {
    let mut pos = crate::board::hirate_position();

    assert!(!pos.is_repetition(1));
    assert!(!pos.is_repetition(3));

    // ▲3八飛△7二飛▲2八飛△8二飛
    let sequences = ["2h3h", "8b7b", "3h2h", "7b8b"];

    for repeat in 0..3 {
        for usi in sequences {
            apply_usi_move(&mut pos, usi);
        }

        match repeat {
            0 => {
                assert_eq!(pos.state_stack().current().repetition_counter, 1);
                assert!(!pos.is_repetition(3));
            }
            1 => {
                assert_eq!(pos.state_stack().current().repetition_counter, 2);
                assert!(!pos.is_repetition(3));
            }
            _ => {}
        }
    }

    assert!(pos.is_repetition(3));
}

#[test]
fn test_clone_for_search_truncates_history_and_preserves_current_state() {
    let mut pos = crate::board::hirate_position();

    let moves = [
        "7g7f", "3c3d", "2g2f", "4c4d", "3i4h", "3a4b", "5g5f", "5c5d", "4h5g", "4b5c", "5g4f",
        "5c4b", "4f5g", "4b5c", "5g4f", "5c4b", "4f5g", "4b5c", "5g4f", "5c4b",
    ];
    for usi in moves {
        apply_usi_move(&mut pos, usi);
    }

    let cloned = pos.clone_for_search();

    assert_eq!(cloned.to_sfen(None), pos.to_sfen(None));
    assert_eq!(cloned.key(), pos.key());
    assert_eq!(cloned.board_key(), pos.board_key());
    assert_eq!(cloned.hand_key(), pos.hand_key());
    assert_eq!(cloned.checkers(), pos.checkers());
    assert_eq!(cloned.last_move(), pos.last_move());
    assert_eq!(cloned.last_moved_piece_type(), pos.last_moved_piece_type());
    assert_eq!(cloned.repetition_counter(), pos.repetition_counter());
    assert_eq!(cloned.repetition_distance(), pos.repetition_distance());
    assert_eq!(cloned.state_stack_depth(), 16);
    assert!(cloned.state_stack_depth() < pos.state_stack_depth());
}

#[test]
fn test_clone_for_search_keeps_enough_history_for_repetition() {
    let mut pos = crate::board::hirate_position();

    let loop_moves = ["2h3h", "8b7b", "3h2h", "7b8b", "2h3h", "8b7b", "3h2h", "7b8b"];
    for usi in loop_moves {
        apply_usi_move(&mut pos, usi);
    }

    let mut cloned = pos.clone_for_search();

    for usi in ["2h3h", "8b7b", "3h2h", "7b8b"] {
        apply_usi_move(&mut cloned, usi);
    }

    assert_eq!(cloned.repetition_state(), RepetitionState::Draw);
}

#[test]
// 連続王手カウンタが王手をかけた際に増加することを確認
fn test_continuous_check_counter_increments_on_check() {
    // 連続王手の千日手局面: 先手が連続で王手をかけ続ける
    let sfen = "lr2+R2G1/6g1k/p3ppppp/2p1b4/9/2P+b4P/P+p3PPP1/4G1SK1/LN3+p1NL b 2SNLgsn3p 1";
    let mut pos = crate::board::position_from_sfen(sfen).expect("valid sfen");

    // 初期状態では連続王手カウンタは0
    assert_eq!(pos.state_stack().current().continuous_check[Color::BLACK.to_index()], 0u16);
    assert_eq!(pos.state_stack().current().continuous_check[Color::WHITE.to_index()], 0u16);

    // 先手が王手をかける手を指す: 2a1a
    apply_usi_move(&mut pos, "2a1a");
    // 先手が王手をかけたので、先手の連続王手カウンタが2に増加
    assert_eq!(pos.state_stack().current().continuous_check[Color::BLACK.to_index()], 2u16);
    assert_eq!(pos.state_stack().current().continuous_check[Color::WHITE.to_index()], 0u16);

    // 後手が王手を回避: 1b2b
    apply_usi_move(&mut pos, "1b2b");
    // 後手は王手をかけていないので、後手のカウンタは0のまま
    // 先手のカウンタは継続(リセットされない)
    assert_eq!(pos.state_stack().current().continuous_check[Color::BLACK.to_index()], 2u16);
    assert_eq!(pos.state_stack().current().continuous_check[Color::WHITE.to_index()], 0u16);

    // 先手が再び王手: 1a2a
    apply_usi_move(&mut pos, "1a2a");
    // 先手の連続王手カウンタが4に増加
    assert_eq!(pos.state_stack().current().continuous_check[Color::BLACK.to_index()], 4u16);
    assert_eq!(pos.state_stack().current().continuous_check[Color::WHITE.to_index()], 0u16);

    // 後手が王手を回避: 2b1b
    apply_usi_move(&mut pos, "2b1b");
    // 先手のカウンタは継続
    assert_eq!(pos.state_stack().current().continuous_check[Color::BLACK.to_index()], 4u16);
    assert_eq!(pos.state_stack().current().continuous_check[Color::WHITE.to_index()], 0u16);
}

#[test]
// 連続王手カウンタが王手でない手で0のままであることを確認
fn test_continuous_check_counter_stays_zero_on_non_check() {
    let mut pos = crate::board::hirate_position();

    // 初期状態では連続王手カウンタは0
    assert_eq!(pos.state_stack().current().continuous_check[Color::BLACK.to_index()], 0u16);
    assert_eq!(pos.state_stack().current().continuous_check[Color::WHITE.to_index()], 0u16);

    // 王手でない手を指す
    apply_usi_move(&mut pos, "7g7f");

    // 王手でない手を指したので、先手のカウンタは0のまま
    assert_eq!(pos.state_stack().current().continuous_check[Color::BLACK.to_index()], 0u16);
    assert_eq!(pos.state_stack().current().continuous_check[Color::WHITE.to_index()], 0u16);

    // 後手も王手でない手を指す
    apply_usi_move(&mut pos, "8c8d");

    // 王手でない手なので、両者とも0のまま
    assert_eq!(pos.state_stack().current().continuous_check[Color::BLACK.to_index()], 0u16);
    assert_eq!(pos.state_stack().current().continuous_check[Color::WHITE.to_index()], 0u16);
}

#[test]
// get_repetition_state()が通常の千日手を検出することを確認
fn test_get_repetition_state_detects_normal_repetition() {
    let mut pos = crate::board::hirate_position();

    // 初期状態では千日手でない
    assert_eq!(pos.repetition_state(), RepetitionState::None);

    // ▲3八飛△7二飛▲2八飛△8二飛を3回繰り返す
    let sequences = ["2h3h", "8b7b", "3h2h", "7b8b"];

    // 1回目
    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }
    assert_eq!(pos.repetition_state(), RepetitionState::None);

    // 2回目
    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }
    assert_eq!(pos.repetition_state(), RepetitionState::None);

    // 3回目(4回目の同一局面出現)
    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }
    assert_eq!(pos.repetition_state(), RepetitionState::Draw);
}

#[test]
fn test_cached_repetition_state_with_ply_uses_signed_cached_distance() {
    let mut pos = crate::board::hirate_position();

    for usi in ["2h3h", "8b7b", "3h2h", "7b8b"] {
        apply_usi_move(&mut pos, usi);
    }

    assert_eq!(pos.repetition_counter(), 1);
    assert_eq!(pos.repetition_distance(), 4);
    assert_eq!(pos.current_state().repetition_type, RepetitionState::Draw);

    // cached API は `repetition_distance < ply` の
    // signed 比較を使うので、distance == ply では検出しない。
    assert_eq!(pos.cached_repetition_state_with_ply(4), RepetitionState::None);
    assert_eq!(pos.cached_repetition_state_with_ply(5), RepetitionState::Draw);

    // full scan API は state stack を遡って調べるため、同じ ply でも検出しうる。
    assert_eq!(pos.repetition_state_with_ply(4), RepetitionState::Draw);
}

#[test]
fn test_cached_repetition_state_with_ply_returns_fourfold_even_at_ply_zero() {
    let mut pos = crate::board::hirate_position();

    for _ in 0..3 {
        for usi in ["2h3h", "8b7b", "3h2h", "7b8b"] {
            apply_usi_move(&mut pos, usi);
        }
    }

    assert_eq!(pos.repetition_counter(), 3);
    assert_eq!(pos.repetition_distance(), -4);

    // 4回目以降は cached repetition_distance が負になるため、
    // signed 比較では ply=0 でも即座に返る。
    // 検出距離 (=4) は上の repetition_distance() == -4 で被覆済み。
    assert_eq!(pos.cached_repetition_state_with_ply(0), RepetitionState::Draw);
}

#[test]
// get_repetition_state()が連続王手の千日手を検出することを確認(先手が王手をかけ続ける)
fn test_get_repetition_state_detects_perpetual_check_by_black() {
    // 連続王手の千日手局面: 先手が連続で王手をかけ続ける
    let sfen = "lr2+R2G1/6g1k/p3ppppp/2p1b4/9/2P+b4P/P+p3PPP1/4G1SK1/LN3+p1NL b 2SNLgsn3p 1";
    let mut pos = crate::board::position_from_sfen(sfen).expect("valid sfen");

    // 初期状態では千日手でない
    assert_eq!(pos.repetition_state(), RepetitionState::None);

    // 王手の手順: 2a1a 1b2b 1a2a 2b1b を3回繰り返す
    let sequences = ["2a1a", "1b2b", "1a2a", "2b1b"];

    // 1回目
    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }
    assert_eq!(pos.repetition_state(), RepetitionState::None);

    // 2回目
    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }
    assert_eq!(pos.repetition_state(), RepetitionState::None);

    // 3回目(4回目の同一局面出現)
    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }

    // 先手が連続王手をかけ続けたので、RepetitionState::Lose(先手の負け)
    assert_eq!(pos.repetition_state(), RepetitionState::Lose);
}

/// 千日手カウンタと閾値判定を検証する。
#[test]
fn test_repetition_counter_thresholds() {
    let mut pos = crate::board::hirate_position();

    // 1. repetition_counterは同一局面の登場回数をカウントする
    let sequences = ["2h3h", "8b7b", "3h2h", "7b8b"];
    let initial_zobrist = pos.key();

    // 1回目の繰り返し
    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }
    assert_eq!(pos.key(), initial_zobrist);
    assert_eq!(pos.state_stack().current().repetition_counter, 1);

    // 2回目の繰り返し
    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }
    assert_eq!(pos.key(), initial_zobrist);
    assert_eq!(pos.state_stack().current().repetition_counter, 2);

    // 3回目の繰り返し(4回目の同一局面出現)
    for usi in sequences {
        apply_usi_move(&mut pos, usi);
    }
    assert_eq!(pos.key(), initial_zobrist);
    assert_eq!(pos.state_stack().current().repetition_counter, 3);

    // 2. get_repetition_stateがrepetition_counter >= 3で千日手を検出
    assert_eq!(pos.repetition_state(), RepetitionState::Draw);

    // 3. is_repetition(threshold)がrepetition_counter >= thresholdで判定
    assert!(!pos.is_repetition(4)); // 4回目未満
    assert!(pos.is_repetition(3)); // 3回目以上
    assert!(pos.is_repetition(2)); // 2回目以上
    assert!(pos.is_repetition(1)); // 1回目以上
}

/// 連続王手の千日手を検出する。
#[test]
fn test_continuous_check_repetition() {
    // 連続王手の千日手局面: 先手が連続で王手をかけ続ける
    let sfen = "lr2+R2G1/6g1k/p3ppppp/2p1b4/9/2P+b4P/P+p3PPP1/4G1SK1/LN3+p1NL b 2SNLgsn3p 1";
    let mut pos = crate::board::position_from_sfen(sfen).expect("valid sfen");

    let sequences = ["2a1a", "1b2b", "1a2a", "2b1b"];

    // 3回繰り返して4回目の同一局面を出現させる
    for _ in 0..3 {
        for usi in sequences {
            apply_usi_move(&mut pos, usi);
        }
    }

    // 先手が連続王手をかけ続けたので、RepetitionState::Lose(先手の負け)
    assert_eq!(pos.repetition_state(), RepetitionState::Lose);

    // continuous_checkカウンタが立っていることを確認
    assert!(pos.state_stack().current().continuous_check[Color::BLACK.to_index()] > 0);
}

/// 優等局面と劣等局面の状態値を検証する。
#[test]
fn test_superior_inferior_state_variants() {
    // 優等/劣等局面のテストは複雑なため、簡易的な検証のみ実施
    // 実装の存在確認
    let pos = crate::board::hirate_position();

    // RepetitionState::SuperiorとInferiorが定義されていることを確認
    let _ = (RepetitionState::Superior, RepetitionState::Inferior);

    // 通常局面では千日手でない
    assert_eq!(pos.repetition_state(), RepetitionState::None);
}

/// `plies_from_null` が通常手と駒取りで増加することを検証する。
#[test]
fn test_plies_from_null_progression() {
    let mut pos = crate::board::hirate_position();

    // 通常手でplies_from_nullが増加
    apply_usi_move(&mut pos, "7g7f");
    assert_eq!(pos.state_stack().current().plies_from_null, 1);

    apply_usi_move(&mut pos, "8c8d");
    assert_eq!(pos.state_stack().current().plies_from_null, 2);

    // 駒取りでもplies_from_nullは継続
    apply_usi_move(&mut pos, "8g8f");
    apply_usi_move(&mut pos, "8d8e");
    apply_usi_move(&mut pos, "8f8e"); // 駒取り
    assert_eq!(pos.state_stack().current().plies_from_null, 5);
}

#[test]
// 優等/劣等局面の検出が指定局面で成立することを確認
fn test_repetition_state_detects_superior_inferior() {
    let sfen = "lr6l/4g1pkp/2ns1s3/4pp1SP/PPBP2Pp1/2gpPP3/2B2S3/2K6/L6RL b 4P2g3np 1";
    let mut pos = crate::board::position_from_sfen(sfen).expect("valid sfen");

    for usi in ["P*2c", "2b2a", "2c2b+", "2a2b"] {
        apply_usi_move(&mut pos, usi);
    }

    assert_eq!(pos.turn(), Color::BLACK);
    assert_eq!(pos.repetition_state(), RepetitionState::Inferior);
}

#[test]
// 連続王手が途切れる反復では通常の千日手になることを確認
fn test_repetition_state_handles_broken_perpetual_check() {
    let sfen = "kns5l/lsp2+R3/1p2B3+P/p1+S+B4p/9/3pP3P/PPG1+pP3/L7s/KGg+r5 b G3NL4P2p 1";
    let mut pos = crate::board::position_from_sfen(sfen).expect("valid sfen");

    let sequences = ["G*8h", "7i8i", "8h8i", "G*7i"];

    for _ in 0..3 {
        for usi in sequences {
            apply_usi_move(&mut pos, usi);
        }
    }

    assert_eq!(pos.repetition_state(), RepetitionState::Draw);
}