[expolorer] Add two new commands for showing available moves and sight of a piece on a square

This commit is contained in:
Eryn Wells 2025-05-19 08:28:23 -07:00
parent a78526befa
commit 539b1fca6e

View file

@ -54,6 +54,16 @@ fn command_line() -> Command {
.arg(Arg::new("square").required(true))
.about("Place a piece on the board"),
)
.subcommand(
Command::new("sight")
.arg(Arg::new("square").required(true))
.about("Show sight of a piece on a square"),
)
.subcommand(
Command::new("moves")
.arg(Arg::new("square").required(true))
.about("Show moves of a piece on a square"),
)
.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"))
@ -134,6 +144,34 @@ fn respond(line: &str, state: &mut State) -> Result<CommandResult, String> {
state.builder.place_piece(piece);
state.position = state.builder.build();
}
Some(("sight", matches)) => {
let square = matches
.get_one::<String>("square")
.ok_or("Missing square")?;
let square = Square::from_algebraic_str(square)
.map_err(|_| "Error: invalid square specifier")?;
let sight = state.position.sight(square);
let display = state.position.display().highlight(sight);
println!("\n{display}");
result.should_print_position = false;
}
Some(("moves", matches)) => {
let square = matches
.get_one::<String>("square")
.ok_or("Missing square")?;
let square = Square::from_algebraic_str(square)
.map_err(|_| "Error: invalid square specifier")?;
let movement = state.position.movement(square);
let display = state.position.display().highlight(movement);
println!("\n{display}");
result.should_print_position = false;
}
Some(("starting", _matches)) => {
let starting_position = Position::starting();
state.builder = PositionBuilder::from_position(&starting_position);