nozaq / shogi-rs

A Bitboard-based shogi library in Rust. Board representation, move generation/validation and time control utilities.
https://docs.rs/shogi/latest/shogi/
MIT License
46 stars 3 forks source link

unmake_move with capture doesn't decrement number of pieces in hands #25

Closed na2hiro closed 3 years ago

na2hiro commented 3 years ago

I appreciate that you've been maintaining the great library. I'm trying to write some code to solve tsume shogi with this.

It seems that unmake_move doesn't decrement a piece count of the type which was captured by the move, but it should. Unmaking such moves with captures adds extra piece in the game every time.

Reproduction code

use shogi::{Move, Position, Color, Piece, Square};
use shogi::bitboard::Factory as BBFactory;
use shogi::piece_type::PieceType;

fn main() {
    BBFactory::init();
    let mut pos = Position::new();
    let sfen = "8l/8+R/8k/9/9/9/9/9/9 w r2b4g4s4n3l18p 1";
    pos.set_sfen(sfen).unwrap();
    assert_eq!(1, pos.hand(Piece{piece_type: PieceType::Rook, color: Color::White}), "initial");
    pos.make_move(Move::from_sfen("1a1b").unwrap()).unwrap();
    assert_eq!(2, pos.hand(Piece{piece_type: PieceType::Rook, color: Color::White}), "after make_move");
    pos.unmake_move().unwrap();
    assert_eq!(1, pos.hand(Piece{piece_type: PieceType::Rook, color: Color::White}), "after unmake_move");
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `2`: after unmake_move', src/main.rs:47:5
stack backtrace:

The position used for the example in KIF format:

後手の持駒:飛 角二 金四 銀四 桂四 香三 歩十八 
 9 8 7 6 5 4 3 2 1
+---------------------------+
| ・ ・ ・ ・ ・ ・ ・ ・v香|一
| ・ ・ ・ ・ ・ ・ ・ ・ 龍|二
| ・ ・ ・ ・ ・ ・ ・ ・v王|三
| ・ ・ ・ ・ ・ ・ ・ ・ ・|四
| ・ ・ ・ ・ ・ ・ ・ ・ ・|五
| ・ ・ ・ ・ ・ ・ ・ ・ ・|六
| ・ ・ ・ ・ ・ ・ ・ ・ ・|七
| ・ ・ ・ ・ ・ ・ ・ ・ ・|八
| ・ ・ ・ ・ ・ ・ ・ ・ ・|九
+---------------------------+
先手の持駒:なし
na2hiro commented 3 years ago

Sorry, I have messed up the initial testing. The example code nor the position didn't make sense.

Updated the post above: The issue happens when unmaking a move of capturing promoted pieces. I'm working on creating PR for this.