From 1d694a6fca265229ab4325467c5cf025c2e1a3e5 Mon Sep 17 00:00:00 2001 From: koba-e964 <3303362+koba-e964@users.noreply.github.com> Date: Mon, 19 Jun 2023 00:18:59 +0900 Subject: [PATCH] Add PositionWrapper::give_piece --- mate_solver/src/position_wrapper.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/mate_solver/src/position_wrapper.rs b/mate_solver/src/position_wrapper.rs index 3e6b85a..b4d96da 100644 --- a/mate_solver/src/position_wrapper.rs +++ b/mate_solver/src/position_wrapper.rs @@ -97,6 +97,18 @@ impl PositionWrapper { } } + /// 攻め方 (先手) の持ち駒を相手の駒台に移動させる。ハッシュ値も更新する。 + pub fn give_piece(&mut self, piece_kind: PieceKind) { + let s = self.inner.hand_of_a_player_mut(Color::Black); + *s = s.removed(piece_kind).unwrap(); + self.hash ^= TABLE.hands[Color::Black.array_index()][piece_kind.array_index()] + [s.count(piece_kind).unwrap() as usize]; + let s = self.inner.hand_of_a_player_mut(Color::White); + self.hash ^= TABLE.hands[Color::White.array_index()][piece_kind.array_index()] + [s.count(piece_kind).unwrap() as usize]; + *s = s.added(piece_kind).unwrap(); + } + /// 局面のハッシュ値を計算する。 fn compute_hash(position: &PartialPosition) -> Key { let mut x = 0; @@ -251,4 +263,19 @@ mod tests { } assert_eq!(hashes[2], hashes[8]); } + + #[test] + fn give_piece_works() { + let piece_kind = PieceKind::Pawn; + let mut position = PartialPosition::startpos(); + position + .hand_of_a_player_mut(Color::Black) + .Hand_add(piece_kind); + let mut position = PositionWrapper::new(position); + position.give_piece(piece_kind); + assert_eq!( + position.hash, + PositionWrapper::compute_hash(&position.inner), + ); + } }