From 24ad9cfafd3a3d133895c77bcbb6a08cddc72821 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 4 Feb 2025 16:14:42 -0500 Subject: [PATCH] fix diagonals FULLY --- src/board.rs | 90 +++++++++++++++++++++------------------------------- src/misc.rs | 56 ++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 56 deletions(-) diff --git a/src/board.rs b/src/board.rs index 33d2aa9..70a8029 100644 --- a/src/board.rs +++ b/src/board.rs @@ -1,4 +1,7 @@ -use crate::{misc::split_from, piece::Piece}; +use crate::{ + misc::{diag, split_from}, + piece::Piece, +}; use std::{cmp::Ordering, fmt}; pub const BOARD_SIZE: usize = 8; @@ -138,25 +141,8 @@ impl Board { .map(|range| range.into_iter().map(|j| (i, j)).collect()), ); - // TODO! diagonals completely broken :( - // handle diagonals - chains.extend( - i_chain - .clone() - .into_iter() - .zip(j_chain.clone()) - .map(|(i_vec, j_vec)| i_vec.into_iter().zip(j_vec).collect()), - ); - - // handle top right diagonals too - chains.extend( - i_chain - .into_iter() - .rev() - .zip(j_chain) - .map(|(i_vec, j_vec)| i_vec.into_iter().zip(j_vec).collect()), - ); + chains.extend(diag(i, j, 0, 0, BOARD_SIZE - 1, BOARD_SIZE - 1)); let mut captured: usize = 0; @@ -326,16 +312,15 @@ mod test { fn corner_capture_top_left() { let mut board = Board::new(); - // Black pieces at (0, 0) and (1, 0), and (0, 1) to create capture opportunity + // Black pieces at (0, 0) and (2, 2) board.place_unchecked(0, 0, Piece::Black); - board.place_unchecked(1, 0, Piece::White); - board.place_unchecked(0, 1, Piece::White); + board.place_unchecked(1, 1, Piece::White); // to be captured + board.place_unchecked(2, 2, Piece::Black); board.propegate_from(0, 0); - // Capture white piece at (0, 1) and (1, 0) - assert_eq!(board.get(0, 1), &Some(Piece::Black)); - assert_eq!(board.get(1, 0), &Some(Piece::Black)); + // Capture white piece at (1,1) + assert_eq!(board.get(1, 1), &Some(Piece::Black), "\n{}", board); } // Test corner capture from top-right corner @@ -343,16 +328,15 @@ mod test { fn corner_capture_top_right() { let mut board = Board::new(); - // Black pieces at (0, 7) and (1, 7), and (0, 6) to create capture opportunity + // Black pieces at (0, 7) and (2, 5) board.place_unchecked(0, 7, Piece::Black); - board.place_unchecked(1, 7, Piece::White); - board.place_unchecked(0, 6, Piece::White); + board.place_unchecked(1, 6, Piece::White); // to be captured + board.place_unchecked(2, 5, Piece::Black); - board.propegate_from(0, 7); + board.propegate_from(2, 5); - // Capture white piece at (0, 6) and (1, 7) - assert_eq!(board.get(0, 6), &Some(Piece::Black)); - assert_eq!(board.get(1, 7), &Some(Piece::Black)); + // Capture white piece at (1, 6) + assert_eq!(board.get(1, 6), &Some(Piece::Black), "\n{}", board); } // Test corner capture from bottom-left corner @@ -360,16 +344,15 @@ mod test { fn corner_capture_bottom_left() { let mut board = Board::new(); - // Black pieces at (7, 0) and (6, 0), and (7, 1) to create capture opportunity + // Black pieces at (7, 0) and (5, 2) board.place_unchecked(7, 0, Piece::Black); - board.place_unchecked(6, 0, Piece::White); - board.place_unchecked(7, 1, Piece::White); + board.place_unchecked(6, 1, Piece::White); // to be captured + board.place_unchecked(5, 2, Piece::Black); - board.propegate_from(7, 0); + board.propegate_from(5, 2); - // Capture white piece at (7, 1) and (6, 0) - assert_eq!(board.get(7, 1), &Some(Piece::Black)); - assert_eq!(board.get(6, 0), &Some(Piece::Black)); + // Capture white piece at (6, 1) + assert_eq!(board.get(6, 1), &Some(Piece::Black), "\n{}", board); } // Test corner capture from bottom-right corner @@ -377,16 +360,15 @@ mod test { fn corner_capture_bottom_right() { let mut board = Board::new(); - // Black pieces at (7, 7) and (6, 7), and (7, 6) to create capture opportunity + // Black pieces at (7, 7) and (5, 5) board.place_unchecked(7, 7, Piece::Black); - board.place_unchecked(6, 7, Piece::White); - board.place_unchecked(7, 6, Piece::White); + board.place_unchecked(6, 6, Piece::White); // to be captured + board.place_unchecked(5, 5, Piece::Black); - board.propegate_from(7, 7); + board.propegate_from(5, 5); - // Capture white piece at (7, 6) and (6, 7) - assert_eq!(board.get(7, 6), &Some(Piece::Black)); - assert_eq!(board.get(6, 7), &Some(Piece::Black)); + // Capture white piece at (6, 6) + assert_eq!(board.get(6, 6), &Some(Piece::Black), "\n{}", board); } // Test capture from top-left corner (horizontal) @@ -396,12 +378,12 @@ mod test { // Create a scenario where a capture should happen horizontally from (0, 0) board.place_unchecked(0, 0, Piece::Black); - board.place_unchecked(0, 1, Piece::White); + board.place_unchecked(0, 1, Piece::White); // to be captured board.place_unchecked(0, 2, Piece::Black); board.propegate_from(0, 2); - assert_eq!(board.get(0, 1), &Some(Piece::Black)); + assert_eq!(board.get(0, 1), &Some(Piece::Black), "\n{}", board); } // Test capture from top-right corner (horizontal) @@ -411,12 +393,12 @@ mod test { // Create a scenario where a capture should happen horizontally from (0, 7) board.place_unchecked(0, 7, Piece::Black); - board.place_unchecked(0, 6, Piece::White); + board.place_unchecked(0, 6, Piece::White); // to be captured board.place_unchecked(0, 5, Piece::Black); board.propegate_from(0, 5); - assert_eq!(board.get(0, 6), &Some(Piece::Black)); + assert_eq!(board.get(0, 6), &Some(Piece::Black), "\n{}", board); } // Test capture from top-left corner (vertical) @@ -426,12 +408,12 @@ mod test { // Create a scenario where a capture should happen vertically from (0, 0) board.place_unchecked(0, 0, Piece::Black); - board.place_unchecked(1, 0, Piece::White); + board.place_unchecked(1, 0, Piece::White); // to be captured board.place_unchecked(2, 0, Piece::Black); board.propegate_from(2, 0); - assert_eq!(board.get(1, 0), &Some(Piece::Black)); + assert_eq!(board.get(1, 0), &Some(Piece::Black), "\n{}", board); } // Test capture from bottom-left corner (vertical) @@ -441,11 +423,11 @@ mod test { // Create a scenario where a capture should happen vertically from (7, 0) board.place_unchecked(7, 0, Piece::Black); - board.place_unchecked(6, 0, Piece::White); + board.place_unchecked(6, 0, Piece::White); // to be captured board.place_unchecked(5, 0, Piece::Black); board.propegate_from(5, 0); - assert_eq!(board.get(6, 0), &Some(Piece::Black)); + assert_eq!(board.get(6, 0), &Some(Piece::Black), "\n{}", board); } } diff --git a/src/misc.rs b/src/misc.rs index cdd8b3b..0a6b0ea 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -15,14 +15,53 @@ where if x > min + T::one() { let x_lower = x - T::one(); output.push((min..=x_lower).rev().collect()); + } else { + output.push(Vec::new()); } if x + T::one() < max { output.push(((x + T::one())..=max).collect()); + } else { + output.push(Vec::new()); } output } +pub fn diag( + i: usize, + j: usize, + min_i: usize, + min_j: usize, + max_i: usize, + max_j: usize, +) -> Vec> { + let i_chains = split_from(min_i, max_i, i); + let j_chains = split_from(min_j, max_j, j); + + vec![ + i_chains[0] + .clone() + .into_iter() + .zip(j_chains[0].clone()) + .collect(), + i_chains[1] + .clone() + .into_iter() + .zip(j_chains[1].clone()) + .collect(), + i_chains[1] + .clone() + .into_iter() + .zip(j_chains[0].clone()) + .collect(), + i_chains[0] + .clone() + .into_iter() + .zip(j_chains[1].clone()) + .collect(), + ] +} + #[cfg(test)] mod test { use super::*; @@ -31,9 +70,9 @@ mod test { fn split_test() { assert_eq!(split_from(0, 6, 2), vec![vec![1, 0], vec![3, 4, 5, 6]]); - assert_eq!(split_from(0, 6, 0), vec![vec![1, 2, 3, 4, 5, 6]]); + assert_eq!(split_from(0, 6, 0), vec![vec![], vec![1, 2, 3, 4, 5, 6]]); - assert_eq!(split_from(0, 6, 6), vec![vec![5, 4, 3, 2, 1, 0]]); + assert_eq!(split_from(0, 6, 6), vec![vec![5, 4, 3, 2, 1, 0], vec![]]); // test out-of-bounds and also generics assert_eq!( @@ -41,4 +80,17 @@ mod test { Vec::>::new() ); } + + #[test] + fn diag_test() { + assert_eq!( + diag(2, 3, 0, 0, 7, 7), + vec![ + vec![(1, 2), (0, 1)], + vec![(3, 4), (4, 5), (5, 6), (6, 7)], + vec![(3, 2), (4, 1), (5, 0)], + vec![(1, 4), (0, 5)] + ] + ); + } }