diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ada8be9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb \ No newline at end of file diff --git a/src/classes/creature.rs b/src/classes/creature.rs index 6fc3689..7e4381b 100644 --- a/src/classes/creature.rs +++ b/src/classes/creature.rs @@ -33,6 +33,16 @@ impl Creature { } } + /// Returns whether the creature is alive or not. + /// + /// # Returns + /// + /// A boolean indicating whether the creature is alive or not. + #[allow(clippy::trivially_copy_pass_by_ref)] // This is a getter, we want to pass by reference. + pub fn is_alive(&self) -> bool { + self.alive + } + /// Sets the alive state of the creature. /// /// # Arguments @@ -130,9 +140,18 @@ mod tests { assert_eq!(creature.alive, false); } + #[test] + fn test_check_is_alive() { + let matrix: Vec> = vec![ + vec![Creature::new(0, 0, 2), Creature::new(0, 0, 2)], + vec![Creature::new(0, 0, 2), Creature::new(0, 0, 1)], + ]; + assert_eq!(matrix[0][0].is_alive(), true); + } + #[test] fn test_check_neighbors() { - let matrix = vec![ + let matrix: Vec> = vec![ vec![Creature::new(0, 0, 2), Creature::new(0, 0, 2)], vec![Creature::new(0, 0, 2), Creature::new(0, 0, 1)], ]; diff --git a/src/classes/matrix.rs b/src/classes/matrix.rs index a00d672..f98f26f 100644 --- a/src/classes/matrix.rs +++ b/src/classes/matrix.rs @@ -44,6 +44,7 @@ impl Matrix { } println!(); } + println!(); } /// Updates the matrix by calling with ref_mat the check_still_alive @@ -52,12 +53,20 @@ impl Matrix { /// # Arguments /// /// - `ref_mat`: The cloned matrix to reference. - pub fn update_matrix(&mut self, ref_mat: &Matrix) { + pub fn update_matrix(&mut self, ref_mat: &Matrix) -> bool { + let mut creature_still_alive: Vec = Vec::new(); for row in self.grid.iter_mut() { for c in row.iter_mut() { c.check_still_alive(ref_mat.grid.clone()); + if c.is_alive() { + creature_still_alive.push(true); + } } } + match creature_still_alive.len() { + 0 => false, + _ => true, + } } } @@ -76,7 +85,7 @@ mod tests { ], }; let ref_mat: Matrix = matrix.clone(); - matrix.update_matrix(&ref_mat); + assert_eq!(matrix.update_matrix(&ref_mat), true); assert_eq!( matrix.grid, vec![ diff --git a/src/main.rs b/src/main.rs index 7cd59cb..0b97c6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use crate::classes::matrix::Matrix; +use std::{thread, time}; mod classes; @@ -8,33 +8,46 @@ mod classes; /// and iteratively prints the matrix state, clones the matrix, /// updates the matrix state, and prints the updated matrix state. fn main() { - // Initialize the game matrix with 5x5 dimensions, - // and populate each cell with a new creature. - let mut matrix: classes::matrix::Matrix = classes::matrix::Matrix::new(5); - - // Print the initial reference matrix state - println!("Ref"); - matrix.print_matrix(); - - // Iterate 5 times - for i in 0..5 { - // Clone the matrix - let ref_mat: Matrix = matrix.clone(); - - // Print the cloned matrix state - println!("cloned {}", i); - ref_mat.print_matrix(); - - // Update the original matrix state by calling - // the check_still_alive function on each creature - matrix.update_matrix(&ref_mat); - - // Print the updated reference matrix state - println!("ref {}", i); + // Initialize the game matrix with 6x6 dimensions. + // This matrix will be used to simulate the game of life. + // The 'classes' module contains the Matrix struct that + // represents the game matrix and its operations. + let mut matrix: classes::matrix::Matrix = classes::matrix::Matrix::new(6); + + // Define the duration of 1 second in milliseconds. + // This duration will be used to pause the execution + // for 1 second in the main loop. + let a_sec: time::Duration = time::Duration::from_millis(1000); + + // Initialize the iteration counter to 0. + // This counter will be used to keep track of the + // number of iterations in the main loop. + let mut ite: u128 = 0; + + // Main game loop that iteratively prints the matrix state, + // clones the matrix, updates the matrix state, and prints + // the updated matrix state until no creature is still alive. + // + // The loop runs indefinitely, breaking when no creature + // is still alive. + loop { + // Print the current iteration number + println!("Iteration: {}", ite); + + // Increment the iteration counter + ite += 1; + + // Print the current state of the matrix matrix.print_matrix(); - // Print the cloned matrix state again - println!("cloned {}", i); - ref_mat.print_matrix(); + // Update the matrix state based on the neighboring + // creatures + if !matrix.update_matrix(&matrix.clone()) { + // If no creature is still alive, break the loop + break; + } + + // Pause the execution for 1 second + thread::sleep(a_sec); } }