mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 04:00:01 +00:00
Merge pull request #52 from Magnus167/updating-example
Updating example
This commit is contained in:
commit
7a580327e8
4
.github/workflows/docs-and-testcov.yml
vendored
4
.github/workflows/docs-and-testcov.yml
vendored
@ -105,6 +105,8 @@ jobs:
|
||||
> last-commit-date.json
|
||||
|
||||
- name: Download last available benchmark report
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.CUSTOM_GH_TOKEN }}
|
||||
run: |
|
||||
artifact_url=$(gh api -H "Accept: application/vnd.github+json" \
|
||||
/repos/${{ github.repository }}/actions/artifacts \
|
||||
@ -117,7 +119,7 @@ jobs:
|
||||
exit 0
|
||||
fi
|
||||
|
||||
curl -L -H "Authorization: Bearer ${{ secrets.CUSTOM_GH_TOKEN }}" \
|
||||
curl -L -H "Authorization: Bearer ${GH_TOKEN}" \
|
||||
"$artifact_url" -o benchmark-report.zip
|
||||
|
||||
# Print all files in the current directory
|
||||
|
@ -15,6 +15,9 @@ crate-type = ["cdylib", "lib"]
|
||||
chrono = "^0.4.10"
|
||||
criterion = { version = "0.5", features = ["html_reports"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
rand = "^0.9.1"
|
||||
|
||||
[features]
|
||||
bench = ["dep:criterion"]
|
||||
|
||||
|
12
README.md
12
README.md
@ -102,3 +102,15 @@ assert!(check);
|
||||
### More examples
|
||||
|
||||
See the [examples](./examples/) directory for some demonstrations of Rustframe's syntax and functionality.
|
||||
|
||||
To run the examples, use:
|
||||
|
||||
```bash
|
||||
cargo run --example <example_name>
|
||||
```
|
||||
|
||||
E.g. to run the `game_of_life` example:
|
||||
|
||||
```bash
|
||||
cargo run --example game_of_life
|
||||
```
|
||||
|
@ -1,7 +1,113 @@
|
||||
// src/gol.rs
|
||||
|
||||
use rand::{self, Rng};
|
||||
use rustframe::matrix::{BoolMatrix, IntMatrix, Matrix};
|
||||
use rustframe::matrix::{BoolMatrix, BoolOps, IntMatrix, Matrix};
|
||||
use std::{thread, time};
|
||||
|
||||
const BOARD_SIZE: usize = 50; // Size of the board (50x50)
|
||||
const TICK_DURATION_MS: u64 = 10; // Milliseconds per frame
|
||||
|
||||
fn main() {
|
||||
// Initialize the game board.
|
||||
// This demonstrates `BoolMatrix::from_vec`.
|
||||
let mut current_board =
|
||||
BoolMatrix::from_vec(vec![false; BOARD_SIZE * BOARD_SIZE], BOARD_SIZE, BOARD_SIZE);
|
||||
|
||||
let primes = generate_primes((BOARD_SIZE * BOARD_SIZE) as i32);
|
||||
|
||||
add_simulated_activity(&mut current_board, BOARD_SIZE);
|
||||
|
||||
let mut generation_count: u32 = 0;
|
||||
// `previous_board_state` will store a clone of the board.
|
||||
// This demonstrates `Matrix::clone()` and later `PartialEq` for `Matrix`.
|
||||
let mut previous_board_state: Option<BoolMatrix> = None;
|
||||
let mut board_hashes = Vec::new();
|
||||
// let mut print_board_bool = true;
|
||||
let mut print_bool_int = 0;
|
||||
|
||||
loop {
|
||||
// print!("{}[2J", 27 as char); // Clear screen and move cursor to top-left
|
||||
|
||||
// if print_board_bool {
|
||||
if print_bool_int % 10 == 0 {
|
||||
print!("{}[2J", 27 as char);
|
||||
println!("Conway's Game of Life - Generation: {}", generation_count);
|
||||
|
||||
print_board(¤t_board);
|
||||
println!("Alive cells: {}", ¤t_board.count());
|
||||
|
||||
// print_board_bool = false;
|
||||
print_bool_int = 0;
|
||||
} else {
|
||||
// print_board_bool = true;
|
||||
print_bool_int += 1;
|
||||
}
|
||||
// `current_board.count()` demonstrates a method from `BoolOps`.
|
||||
board_hashes.push(hash_board(¤t_board, primes.clone()));
|
||||
if detect_stable_state(¤t_board, &previous_board_state) {
|
||||
println!(
|
||||
"\nStable state detected at generation {}.",
|
||||
generation_count
|
||||
);
|
||||
add_simulated_activity(&mut current_board, BOARD_SIZE);
|
||||
}
|
||||
if detect_repeating_state(&mut board_hashes) {
|
||||
println!(
|
||||
"\nRepeating state detected at generation {}.",
|
||||
generation_count
|
||||
);
|
||||
add_simulated_activity(&mut current_board, BOARD_SIZE);
|
||||
}
|
||||
if !¤t_board.any() {
|
||||
println!("\nExtinction at generation {}.", generation_count);
|
||||
add_simulated_activity(&mut current_board, BOARD_SIZE);
|
||||
}
|
||||
|
||||
// `current_board.clone()` demonstrates `Clone` for `Matrix`.
|
||||
previous_board_state = Some(current_board.clone());
|
||||
|
||||
// This is the core call to your game logic.
|
||||
let next_board = game_of_life_next_frame(¤t_board);
|
||||
current_board = next_board;
|
||||
|
||||
generation_count += 1;
|
||||
thread::sleep(time::Duration::from_millis(TICK_DURATION_MS));
|
||||
|
||||
// if generation_count > 500 { // Optional limit
|
||||
// println!("\nReached generation limit.");
|
||||
// break;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/// Prints the Game of Life board to the console.
|
||||
///
|
||||
/// - `board`: A reference to the `BoolMatrix` representing the current game state.
|
||||
/// This function demonstrates `board.rows()`, `board.cols()`, and `board[(r, c)]` (Index trait).
|
||||
fn print_board(board: &BoolMatrix) {
|
||||
let mut print_str = String::new();
|
||||
print_str.push_str("+");
|
||||
for _ in 0..board.cols() {
|
||||
print_str.push_str("--");
|
||||
}
|
||||
print_str.push_str("+\n");
|
||||
for r in 0..board.rows() {
|
||||
print_str.push_str("| ");
|
||||
for c in 0..board.cols() {
|
||||
if board[(r, c)] {
|
||||
// Using Index trait for Matrix<bool>
|
||||
print_str.push_str("██");
|
||||
} else {
|
||||
print_str.push_str(" ");
|
||||
}
|
||||
}
|
||||
print_str.push_str(" |\n");
|
||||
}
|
||||
print_str.push_str("+");
|
||||
for _ in 0..board.cols() {
|
||||
print_str.push_str("--");
|
||||
}
|
||||
print_str.push_str("+\n\n");
|
||||
print!("{}", print_str);
|
||||
}
|
||||
|
||||
/// Helper function to create a shifted version of the game board.
|
||||
/// (Using the version provided by the user)
|
||||
|
Loading…
x
Reference in New Issue
Block a user