Skip to content

Commit

Permalink
add all targets and remove failure enum
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbedard committed Sep 8, 2024
1 parent dd26535 commit 5bc3fe4
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 98 deletions.
17 changes: 17 additions & 0 deletions src/commands/all_targets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::game::hexchess::Hexchess;

pub fn execute(hexchess_arg: String) -> Result<String, String> {
let hexchess = match Hexchess::from(hexchess_arg.as_str()) {
Ok(result) => result,
Err(failure) => return Err(failure.to_string()),
};

let csv = hexchess
.all_targets()
.into_iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(",");

Ok(csv)
}
3 changes: 2 additions & 1 deletion src/commands/get_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub fn execute(hexchess_arg: String, position: String) -> Result<String, String>
Err(failure) => return Err(failure.to_string()),
};

let csv = hexchess.targets(from)
let csv = hexchess
.targets(from)
.into_iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
Expand Down
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod all_targets;
pub mod apply_sequence;
pub mod get_targets;
pub mod parse;
51 changes: 43 additions & 8 deletions src/game/board.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::constants::{INITIAL_BOARD, SORTED_POSITIONS};
use crate::game::failure::Failure;
use crate::game::piece::{Color, Piece};
use serde::{Deserialize, Serialize};
use std::fmt;
Expand Down Expand Up @@ -284,7 +283,7 @@ pub enum Position {
}

impl Position {
pub fn from(value: &str) -> Result<Self, Failure> {
pub fn from(value: &str) -> Result<Self, String> {
match value {
"a1" => Ok(Position::A1),
"a2" => Ok(Position::A2),
Expand Down Expand Up @@ -377,7 +376,7 @@ impl Position {
"l4" => Ok(Position::L4),
"l5" => Ok(Position::L5),
"l6" => Ok(Position::L6),
_ => return Err(Failure::InvalidPosition),
_ => return Err(format!("invalid position: {}", value)),
}
}
}
Expand Down Expand Up @@ -676,7 +675,7 @@ impl Board {
}

/// create board from string
pub fn from(value: &str) -> Result<Self, Failure> {
pub fn from(value: &str) -> Result<Self, String> {
let mut board = Self::new();

let mut valid: bool = true;
Expand Down Expand Up @@ -806,9 +805,8 @@ impl Board {

match valid {
true => Ok(board),
false => Err(Failure::InvalidBoard),
false => Err(format!("invalid board: {}", value)),
}

}

/// get piece value at position
Expand Down Expand Up @@ -913,6 +911,17 @@ impl Board {
Self::from(INITIAL_BOARD).unwrap()
}

/// get positions occupied by color
pub fn occupied_by(&self, color: Color) -> Vec<&Position> {
SORTED_POSITIONS
.iter()
.filter(|&position| match self.get(*position) {
Some(piece) => piece.color() == color,
None => false,
})
.collect()
}

/// set piece value at position
pub fn set(&mut self, position: Position, value: Option<Piece>) {
match position {
Expand Down Expand Up @@ -2395,7 +2404,7 @@ mod tests {
#[test]
fn test_to_position() {
assert_eq!(Ok(Position::A1), Position::from("a1"));
assert_eq!(Err(Failure::InvalidPosition), Position::from("whoops"));
assert_eq!(Err("invalid position: whoops".to_string()), Position::from("whoops"));
}

#[test]
Expand Down Expand Up @@ -2617,7 +2626,7 @@ mod tests {
fn test_parse_board_with_invalid_character() {
let board = Board::from("x/3/5/7/9/11/11/11/11/11/11");

assert_eq!(Err(Failure::InvalidBoard), board);
assert_eq!(Err("invalid board: x/3/5/7/9/11/11/11/11/11/11".to_string()), board);
}

#[test]
Expand Down Expand Up @@ -2780,4 +2789,30 @@ mod tests {
}
}
}

#[test]
fn test_get_occupied_by() {
let board = Board::initial();
let white = board.occupied_by(Color::White);

assert_eq!(white.len(), 18);
assert_eq!(white[0], &Position::F5);
assert_eq!(white[1], &Position::E4);
assert_eq!(white[2], &Position::G4);
assert_eq!(white[3], &Position::D3);
assert_eq!(white[4], &Position::F3);
assert_eq!(white[5], &Position::H3);
assert_eq!(white[6], &Position::C2);
assert_eq!(white[7], &Position::F2);
assert_eq!(white[8], &Position::I2);
assert_eq!(white[9], &Position::B1);
assert_eq!(white[10], &Position::C1);
assert_eq!(white[11], &Position::D1);
assert_eq!(white[12], &Position::E1);
assert_eq!(white[13], &Position::F1);
assert_eq!(white[14], &Position::G1);
assert_eq!(white[15], &Position::H1);
assert_eq!(white[16], &Position::I1);
assert_eq!(white[17], &Position::K1);
}
}
31 changes: 0 additions & 31 deletions src/game/failure.rs

This file was deleted.

Loading

0 comments on commit 5bc3fe4

Please sign in to comment.