[explorer] Implement a reset command

Resets the board to an empty or starting state, or to a position specified by a FEN string.
This commit is contained in:
Eryn Wells 2025-05-19 08:42:34 -07:00
parent d67c2cfb99
commit 39ca74459d

View file

@ -63,6 +63,17 @@ fn command_line() -> Command {
.arg(Arg::new("square").required(true))
.about("Show moves of a piece on a square"),
)
.subcommand(
Command::new("reset")
.subcommand(Command::new("clear").about("Reset to a cleared board"))
.subcommand(Command::new("starting").about("Reset to the starting position"))
.subcommand(
Command::new("fen")
.arg(Arg::new("fen").required(true))
.about("Reset to a position described by a FEN string"),
)
.about("Reset the board"),
)
.subcommand(Command::new("print").about("Print the board"))
.subcommand(Command::new("quit").alias("exit").about("Quit the program"))
.subcommand(Command::new("starting").about("Reset the board to the starting position"))
@ -171,6 +182,7 @@ fn respond(line: &str, state: &mut State) -> Result<CommandResult, String> {
let starting_position = Position::starting();
state.position = starting_position;
}
Some(("reset", matches)) => do_reset_command(state, matches)?,
Some((name, _matches)) => unimplemented!("{name}"),
None => unreachable!("Subcommand required"),
}
@ -178,6 +190,21 @@ fn respond(line: &str, state: &mut State) -> Result<CommandResult, String> {
Ok(result)
}
fn do_reset_command(state: &mut State, matches: &clap::ArgMatches) -> Result<(), String> {
match matches.subcommand() {
None | Some(("clear", _)) => state.position = Position::empty(),
Some(("starting", _)) => state.position = Position::starting(),
Some(("fen", matches)) => {
let fen = matches.get_one::<String>("fen").ok_or("Missing FEN")?;
let board = Board::from_fen_str(fen).map_err(|err| format!("{err}"))?;
state.position = Position::new(board);
}
Some((name, _matches)) => unimplemented!("{name}"),
}
Ok(())
}
fn main() -> Result<(), String> {
let mut editor = DefaultEditor::new().map_err(|err| format!("Error: {err}"))?;