Skip to content

Commit

Permalink
Create loop
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanLT committed Mar 11, 2024
1 parent fcc3494 commit 659a185
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 30 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
21 changes: 20 additions & 1 deletion src/classes/creature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -130,9 +140,18 @@ mod tests {
assert_eq!(creature.alive, false);
}

#[test]
fn test_check_is_alive() {
let matrix: Vec<Vec<Creature>> = 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<Creature>> = vec![
vec![Creature::new(0, 0, 2), Creature::new(0, 0, 2)],
vec![Creature::new(0, 0, 2), Creature::new(0, 0, 1)],
];
Expand Down
13 changes: 11 additions & 2 deletions src/classes/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Matrix {
}
println!();
}
println!();
}

/// Updates the matrix by calling with ref_mat the check_still_alive
Expand All @@ -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<bool> = 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,
}
}
}

Expand All @@ -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![
Expand Down
67 changes: 40 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::classes::matrix::Matrix;
use std::{thread, time};

mod classes;

Expand All @@ -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);
}
}

0 comments on commit 659a185

Please sign in to comment.