[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:
Eryn Wells 2025-06-08 16:49:55 -07:00
parent 428ace13dc
commit 634876822b
3 changed files with 47 additions and 1 deletions

View file

@ -47,6 +47,12 @@ fn command_line() -> Command {
.subcommand_help_heading("COMMANDS")
.help_template(PARSER_TEMPLATE)
.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(
Command::new("make")
.arg(Arg::new("from").required(true))
@ -75,7 +81,7 @@ fn command_line() -> Command {
.subcommand(
Command::new("movement")
.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(
Command::new("reset")
@ -112,6 +118,7 @@ fn respond(line: &str, state: &mut State) -> anyhow::Result<CommandResult> {
let mut result = CommandResult::default();
match matches.subcommand() {
Some(("load", matches)) => result = do_load_command(state, matches)?,
Some(("print", _matches)) => {}
Some(("quit", _matches)) => {
result.should_continue = false;
@ -184,6 +191,22 @@ fn respond(line: &str, state: &mut State) -> anyhow::Result<CommandResult> {
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(
state: &mut State,
matches: &clap::ArgMatches,