[explorer] Add a load command
Loads a board position from a FEN string. Plumb through setting the Zobrist state on an existing board. Rebuild the hash when setting the state.
This commit is contained in:
parent
428ace13dc
commit
634876822b
3 changed files with 47 additions and 1 deletions
|
@ -306,6 +306,15 @@ impl Board {
|
||||||
zobrist.set_hash_value(new_hash);
|
zobrist.set_hash_value(new_hash);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn zobrist_state(&self) -> Option<Arc<ZobristState>> {
|
||||||
|
self.zobrist_hash.as_ref().map(ZobristHash::state)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_zobrist_state(&mut self, state: Arc<ZobristState>) {
|
||||||
|
self.zobrist_hash = Some(ZobristHash::new(state));
|
||||||
|
self.recompute_zobrist_hash();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
|
|
|
@ -47,6 +47,12 @@ fn command_line() -> Command {
|
||||||
.subcommand_help_heading("COMMANDS")
|
.subcommand_help_heading("COMMANDS")
|
||||||
.help_template(PARSER_TEMPLATE)
|
.help_template(PARSER_TEMPLATE)
|
||||||
.subcommand(Command::new("fen").about("Print the current position as a FEN string"))
|
.subcommand(Command::new("fen").about("Print the current position as a FEN string"))
|
||||||
|
.subcommand(
|
||||||
|
Command::new("load")
|
||||||
|
.arg(Arg::new("fen").required(true))
|
||||||
|
.alias("l")
|
||||||
|
.about("Load a board position from a FEN string"),
|
||||||
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
Command::new("make")
|
Command::new("make")
|
||||||
.arg(Arg::new("from").required(true))
|
.arg(Arg::new("from").required(true))
|
||||||
|
@ -75,7 +81,7 @@ fn command_line() -> Command {
|
||||||
.subcommand(
|
.subcommand(
|
||||||
Command::new("movement")
|
Command::new("movement")
|
||||||
.arg(Arg::new("square").required(true))
|
.arg(Arg::new("square").required(true))
|
||||||
.about("Show moves of a piece on a square."),
|
.about("Show moves of a piece on a square"),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
Command::new("reset")
|
Command::new("reset")
|
||||||
|
@ -112,6 +118,7 @@ fn respond(line: &str, state: &mut State) -> anyhow::Result<CommandResult> {
|
||||||
let mut result = CommandResult::default();
|
let mut result = CommandResult::default();
|
||||||
|
|
||||||
match matches.subcommand() {
|
match matches.subcommand() {
|
||||||
|
Some(("load", matches)) => result = do_load_command(state, matches)?,
|
||||||
Some(("print", _matches)) => {}
|
Some(("print", _matches)) => {}
|
||||||
Some(("quit", _matches)) => {
|
Some(("quit", _matches)) => {
|
||||||
result.should_continue = false;
|
result.should_continue = false;
|
||||||
|
@ -184,6 +191,22 @@ fn respond(line: &str, state: &mut State) -> anyhow::Result<CommandResult> {
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn do_load_command(state: &mut State, matches: &clap::ArgMatches) -> anyhow::Result<CommandResult> {
|
||||||
|
let fen_string = matches
|
||||||
|
.get_one::<String>("fen")
|
||||||
|
.ok_or(CommandHandlingError::MissingArgument("fen"))?;
|
||||||
|
|
||||||
|
let mut board = Board::from_fen_str(fen_string.as_str())?;
|
||||||
|
board.set_zobrist_state(state.zobrist.clone());
|
||||||
|
|
||||||
|
state.position = Position::new(board);
|
||||||
|
|
||||||
|
Ok(CommandResult {
|
||||||
|
should_continue: true,
|
||||||
|
should_print_position: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn do_reset_command(
|
fn do_reset_command(
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
matches: &clap::ArgMatches,
|
matches: &clap::ArgMatches,
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
mod captures;
|
mod captures;
|
||||||
|
|
||||||
|
use crate::fen::{FromFenStr, FromFenStrError};
|
||||||
use captures::CapturesList;
|
use captures::CapturesList;
|
||||||
use chessfriend_bitboard::BitBoard;
|
use chessfriend_bitboard::BitBoard;
|
||||||
use chessfriend_board::{
|
use chessfriend_board::{
|
||||||
|
@ -230,6 +231,10 @@ impl Position {
|
||||||
pub fn zobrist_hash(&self) -> Option<u64> {
|
pub fn zobrist_hash(&self) -> Option<u64> {
|
||||||
self.board.zobrist_hash()
|
self.board.zobrist_hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_zobrist_state(&mut self, state: Arc<ZobristState>) {
|
||||||
|
self.board.set_zobrist_state(state);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Position {
|
impl Position {
|
||||||
|
@ -238,6 +243,15 @@ impl Position {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromFenStr for Position {
|
||||||
|
type Error = FromFenStrError;
|
||||||
|
|
||||||
|
fn from_fen_str(string: &str) -> Result<Self, Self::Error> {
|
||||||
|
let board = Board::from_fen_str(string)?;
|
||||||
|
Ok(Position::new(board))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ToFenStr for Position {
|
impl ToFenStr for Position {
|
||||||
type Error = <Board as ToFenStr>::Error;
|
type Error = <Board as ToFenStr>::Error;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue