diff --git a/src/game.rs b/src/game.rs index 147e311..4b0c786 100644 --- a/src/game.rs +++ b/src/game.rs @@ -121,9 +121,9 @@ impl Piece { fn moves_at(&self, from: &BoardIndex) -> HashSet { self.moves().filter_map(|(x, y)| { - let bi = *from + (*x, *y); + let bi = (from.x as isize + x, from.y as isize + y); // Check if this goes out of bounds, if so exclude from the list of possible moves - (bi.x <= 7 && bi.y <= 3).then_some(bi) + (bi.0 <= 7 && bi.0 >= 0 && bi.1 <= 3 && bi.1 >= 0).then_some(BoardIndex { x: bi.0 as usize, y: bi.1 as usize }) }) .collect() } @@ -284,28 +284,6 @@ impl std::ops::Add for BoardIndex { } } -impl std::ops::Add<(isize, isize)> for BoardIndex { - type Output = BoardIndex; - - fn add(self, (a, b): (isize, isize)) -> Self { - BoardIndex { - x: self.x.saturating_add_signed(a), - y: self.y.saturating_add_signed(b), - } - } -} - -impl std::ops::Add<(usize, usize)> for BoardIndex { - type Output = BoardIndex; - - fn add(self, (a, b): (usize, usize)) -> Self { - BoardIndex { - x: self.x.saturating_add(a), - y: self.y.saturating_add(b), - } - } -} - impl From<(usize, usize)> for BoardIndex { fn from((x, y): (usize, usize)) -> BoardIndex { BoardIndex { x, y } @@ -388,10 +366,14 @@ impl Board { fn new() -> Board { Board::from_ascii( - r#".....dqq - dpp..pdq - qdp..ppd - qqd....."#, + r#"...###.. + ####q### + ...###.. + ..#.#.#."#, + // r#".....dqq + // dpp..pdq + // qdp..ppd + // qqd....."#, ) } @@ -691,7 +673,6 @@ mod test { let given = board.valid_moves((4, 2).into()); let expected: HashSet = HashSet::from([ (0, 2).into(), - (1, 0).into(), (1, 2).into(), (2, 0).into(), (2, 2).into(), @@ -706,10 +687,10 @@ mod test { (5, 3).into(), (6, 0).into(), (6, 2).into(), - (7, 0).into(), (7, 2).into(), ]); + assert_eq!(expected, Piece::Queen.moves_at(&(4, 2).into()), "Generated queen moves"); assert_eq!(expected, given, "Basic queen moves"); } @@ -782,7 +763,7 @@ mod test { #[test] fn case_02() { let board = Board::from_ascii( - r#"......qq + r#"..#...qq dpp#d#d. qd##qppd qq.###.."#, @@ -792,8 +773,17 @@ mod test { let given = board.valid_moves((4, 1).into()); let expected: HashSet = HashSet::from([ - (3,0).into(), (4,0).into(), (5,0).into(), (2, 1).into(), (2, 3).into(), (3, 1).into(), (5, 1).into(), (3, 2).into(), (5, 2).into() + (1, 1).into(), + (2, 1).into(), + (2, 3).into(), + (3, 0).into(), + (3, 1).into(), + (3, 2).into(), + (4, 0).into(), + (5, 0).into(), + (5, 2).into(), ]); + println!("Moves: {:?}", Piece::Queen.moves_at(&(4, 1).into())); assert_eq!( expected, given, "Mostly blocked queen, moves include captures" @@ -801,6 +791,42 @@ mod test { } } + #[test] + fn case_01() { + let board = Board::from_ascii( + r#".p.....q + dp.p.pdq + qd..dppd + qqdq...."#, + ); + + { + let given = board.valid_moves((0, 0).into()); + let expected: HashSet = HashSet::from([]); + assert_eq!(expected, given); + } + + { + let given = board.valid_moves((1, 0).into()); + let expected: HashSet = + HashSet::from([ + (2, 1).into() + ]); + assert_eq!(expected, given); + } + + { + let given = board.valid_moves((2, 0).into()); + let expected: HashSet = + HashSet::from([ + (2, 1).into(), + (2, 2).into(), + ]); + assert_eq!(expected, given); + } + + } + #[test] fn test_piece_moves_at() { let given = Piece::Drone.moves_at(&(4, 1).into());