aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock199
-rw-r--r--Cargo.toml8
-rw-r--r--src/main.rs128
4 files changed, 336 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..4fabac6
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,199 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "bitflags"
+version = "1.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
+
+[[package]]
+name = "bitflags"
+version = "2.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "game-of-life"
+version = "0.1.0"
+dependencies = [
+ "rand",
+ "sdl2",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "r-efi",
+ "wasi",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
+
+[[package]]
+name = "libc"
+version = "0.2.169"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
+dependencies = [
+ "zerocopy",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.95"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.40"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "r-efi"
+version = "5.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
+
+[[package]]
+name = "rand"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
+dependencies = [
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.9.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "sdl2"
+version = "0.37.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3b498da7d14d1ad6c839729bd4ad6fc11d90a57583605f3b4df2cd709a9cd380"
+dependencies = [
+ "bitflags 1.3.2",
+ "lazy_static",
+ "libc",
+ "sdl2-sys",
+]
+
+[[package]]
+name = "sdl2-sys"
+version = "0.37.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "951deab27af08ed9c6068b7b0d05a93c91f0a8eb16b6b816a5e73452a43521d3"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "version-compare",
+]
+
+[[package]]
+name = "syn"
+version = "2.0.101"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
+
+[[package]]
+name = "version-compare"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29"
+
+[[package]]
+name = "wasi"
+version = "0.14.2+wasi-0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
+dependencies = [
+ "wit-bindgen-rt",
+]
+
+[[package]]
+name = "wit-bindgen-rt"
+version = "0.39.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
+dependencies = [
+ "bitflags 2.9.1",
+]
+
+[[package]]
+name = "zerocopy"
+version = "0.8.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb"
+dependencies = [
+ "zerocopy-derive",
+]
+
+[[package]]
+name = "zerocopy-derive"
+version = "0.8.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..fe1ae5f
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "game-of-life"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+rand = "0.9.1"
+sdl2 = "0.37.0"
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..1e0870f
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,128 @@
+extern crate sdl2;
+
+use sdl2::pixels::Color;
+use sdl2::event::Event;
+use sdl2::keyboard::Keycode;
+use sdl2::rect::{Rect};
+use std::time::Duration;
+
+use rand::Rng;
+
+fn iterate_game(grid: &mut Vec<Vec<i32>>, grid2: &mut Vec<Vec<i32>>, cols: &i32, rows: &i32){
+ let mut i:i32=0;
+ while i < *cols{
+ let mut j:i32=0;
+ while j < *rows{
+ let state=grid[i as usize][j as usize];
+ let mut sum = 0;
+
+ for k in -1..2{
+ for l in -1..2{
+ let col: i32 = (i + k + cols) % cols;
+ let row: i32 = (j + l + rows) % rows;
+ sum = sum + grid[col as usize][row as usize];
+ }
+ }
+
+ sum = sum - state;
+
+ if state==0&&sum==3 {
+ grid2[i as usize][j as usize]=1;
+ } else if state==1&&(sum<2 || sum>3) {
+ grid2[i as usize][j as usize]=0;
+ } else {
+ grid2[i as usize][j as usize]=state;
+ }
+
+ j += 1;
+ }
+ i += 1;
+ }
+
+ let mut i:i32=0;
+ while i < *cols{
+ let mut j:i32=0;
+ while j < *rows{
+ grid[i as usize][j as usize]=grid2[i as usize][j as usize];
+ j += 1;
+ }
+ i += 1;
+ }
+}
+
+
+fn draw(canvas: &mut sdl2::render::WindowCanvas, cols: &i32, rows: &i32, boxsize: &i32, grid: &mut Vec<Vec<i32>>, grid2: &mut Vec<Vec<i32>>){
+ canvas.set_draw_color(Color::RGB(0,0,0));
+ canvas.clear();
+
+ for (i, vec) in grid.iter().enumerate(){
+ for (j, num) in vec.iter().enumerate(){
+ if *num==1 {
+ //putRectRGB(renderer,i*boxsize,j*boxsize,boxsize-1,150,50,255);
+ canvas.set_draw_color(Color::RGB(150,50,255));
+ let x: i32= i as i32 * *boxsize;
+ let y: i32= j as i32 * *boxsize;
+ canvas.fill_rect(Rect::new(x, y, *boxsize as u32 -1, *boxsize as u32-1));
+ }
+ }
+ }
+ iterate_game(grid,grid2,cols,rows);
+
+ canvas.present();
+}
+
+
+
+fn main() {
+ let sdl2_context = sdl2::init().unwrap();
+ let video_subsystem = sdl2_context.video().unwrap();
+
+
+ let width: u32=1900;
+ let height: u32=1100;
+
+ let window = video_subsystem.window("game of life", width, height)
+ .position_centered()
+ .build()
+ .unwrap();
+
+ let mut canvas = window.into_canvas().build().unwrap();
+
+
+ let boxsize:i32=15;
+ let cols:i32=width as i32/boxsize;
+ let rows:i32=height as i32/boxsize;
+
+ let mut grid: Vec<Vec<i32>> = vec![vec![0; rows as usize]; cols as usize];
+ let mut grid2: Vec<Vec<i32>> = vec![vec![0; rows as usize]; cols as usize];
+
+
+ let mut rng = rand::rng();
+
+ for vec in &mut grid{
+ for num in vec{
+ *num = rng.random_range(0..2);
+ }
+ }
+
+
+ draw(&mut canvas,&cols,&rows,&boxsize,&mut grid,&mut grid2);
+
+
+ let mut event_pump = sdl2_context.event_pump().unwrap();
+ 'running: loop {
+ draw(&mut canvas,&cols,&rows,&boxsize,&mut grid,&mut grid2);
+ for event in event_pump.poll_iter() {
+ match event {
+ Event::Quit {..} |
+ Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
+ break 'running
+ },
+ _ => {}
+ }
+ }
+ // The rest of the game loop goes here...
+
+ ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
+ }
+}