Skip to content

Commit

Permalink
add check-move
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbedard committed Sep 8, 2024
1 parent d4a9513 commit bc8a3d9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Usage: hexchess <COMMAND>
Commands:
all-targets Get all legal moves
apply-sequence Apply sequence of moves to a position
check-move Check if a move is legal
get-targets Get legal moves from a position
parse Parse hexchess fen to JSON
help Print this message or the help of the given subcommand(s)
Expand Down
10 changes: 10 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ pub enum Command {
sequence: String,
},

/// Check if a move is legal
CheckMove {
/// Hexchess state
fen: String,

/// Move notation
notation: String,
},

/// Get legal moves from a position
GetTargets {
/// Hexchess state
Expand All @@ -45,6 +54,7 @@ pub fn handle(app: App) -> Result<String, String> {
match app.command {
Command::AllTargets { fen } => commands::all_targets::execute(fen),
Command::ApplySequence { fen, sequence } => commands::apply_sequence::execute(fen, sequence),
Command::CheckMove { fen, notation } => commands::check_move::execute(fen, notation),
Command::GetTargets { fen, position } => commands::get_targets::execute(fen, position),
Command::Parse { fen } => commands::parse::execute(fen),
}
Expand Down
61 changes: 61 additions & 0 deletions src/commands/check_move.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::game::hexchess::Hexchess;
use crate::game::notation::Notation;

pub fn execute(hexchess_arg: String, notation_arg: String) -> Result<String, String> {
let hexchess = match Hexchess::from(hexchess_arg.as_str()) {
Ok(result) => result,
Err(_) => return Err(format!("invalid state: {}", hexchess_arg)),
};

let notation = match Notation::from(notation_arg.as_str()) {
Ok(result) => result,
Err(_) => return Err(format!("invalid notation: {}", notation_arg)),
};

if hexchess.all_targets().contains(&notation) {
return Ok("ok".to_string());
}

return Err("illegal".to_string());
}

#[cfg(test)]
mod tests {
use crate::app::{App, Command, handle};

#[test]
fn test_check_move() {
let output = handle(App {
command: Command::CheckMove {
fen: crate::constants::INITIAL_HEXCHESS.to_string(),
notation: "g4g5".to_string(),
}
});

assert_eq!(Ok("ok".to_string()), output);
}

#[test]
fn test_check_move_invalid() {
let output = handle(App {
command: Command::CheckMove {
fen: crate::constants::INITIAL_HEXCHESS.to_string(),
notation: "whoops".to_string(),
}
});

assert_eq!(output, Err("invalid notation: whoops".to_string()));
}

#[test]
fn test_check_move_illegal() {
let output = handle(App {
command: Command::CheckMove {
fen: crate::constants::INITIAL_HEXCHESS.to_string(),
notation: "a1a2".to_string(),
}
});

assert_eq!(output, Err("illegal".to_string()));
}
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod all_targets;
pub mod apply_sequence;
pub mod check_move;
pub mod get_targets;
pub mod parse;

0 comments on commit bc8a3d9

Please sign in to comment.