test grid for life

This commit is contained in:
lemon-cat committed 2026-05-15 15:54:40 +03:00
commit 125fd73625
4 files changed
+119 -70

No files matched your search

+19 -20
View File
@@ -15,54 +15,53 @@ fn init_life(g: &mut TorusGrid<bool>, size: (i32, i32)) -> &mut TorusGrid<bool>
for i in 0..size.0 { for i in 0..size.0 {
for j in 0..size.1 { for j in 0..size.1 {
if let Some(a) = g.get_mut((i, j)) { if let Some(a) = g.get_mut((i, j)) {
*a = rng.gen_range(0..=100) / 2 == 0; *a = rng.gen_range(0..=1) == 0;
} }
} }
} }
g g
} }
/*
fn life(g: &mut TorusGrid<bool>, size: (i32, i32)) -> &mut TorusGrid<bool> { fn life(g: &mut TorusGrid<bool>, size: (i32, i32)) -> &mut TorusGrid<bool> {
let mut a = g.clone(); let a = g.clone();
for i in 0..size.0 { for i in 0..size.0 {
for j in 0..size.1 { for j in 0..size.1 {
let mut s = 0; let mut s: i32 = 0;
let z = *a.get_wrap((i, j)); s += (*a.get_wrap((i - 1, j + 1)) == true) as i32;
s += z as i32; s += (*a.get_wrap((i, j + 1)) == true) as i32;
s += (*a.get_wrap((i + 1, j + 1)) == true) as i32;
s += (*a.get_wrap((i - 1, j)) == true) as i32;
//s += (*a.get_wrap((i, j)) == true) as i32;
s += (*a.get_wrap((i + 1, j)) == true) as i32;
s += (*a.get_wrap((i - 1, j - 1)) == true) as i32;
s += (*a.get_wrap((i, j - 1)) == true) as i32;
s += (*a.get_wrap((i + 1, j - 1)) == true) as i32;
let v = g.get_mut_wrap((i, j));
*v = (s == 2 && *v == true) || (s == 3 && *v == false);
} }
} }
a g
} }
*/
enum Input_type { enum Input_type {
comand, comand,
keybard, keybard,
} }
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");
let mut rng = rand::thread_rng();
let mut it = Input_type::comand; let mut it = Input_type::comand;
let mut R = Render::new(); let mut R = Render::new();
R.clear(); R.clear();
R.set_rez(40, 80); _ = R.set_rez(40, 80);
let g_rez: (usize, usize) = (100, 50); let g_rez: (usize, usize) = (100, 50);
/*
let mut grid: TorusGrid<i32> = TorusGrid::new(0, g_rez);
for i in 0..g_rez.0 {
for j in 0..g_rez.1 {
if let Some(a) = grid.get_mut((i as i32, j as i32)) {
*a = rng.gen_range(0..=255) as i32;
}
}
}
*/
let mut grid: TorusGrid<bool> = TorusGrid::new(false, g_rez); let mut grid: TorusGrid<bool> = TorusGrid::new(false, g_rez);
init_life(&mut grid, (g_rez.0 as i32, g_rez.1 as i32)); init_life(&mut grid, (g_rez.0 as i32, g_rez.1 as i32));
loop { loop {
//R.clear(); //R.clear();
R.frame(&grid); R.frame(&grid);
life(&mut grid, (g_rez.0 as i32, g_rez.1 as i32));
match it { match it {
Input_type::comand => { Input_type::comand => {
R.print("Input your comand: "); R.print("Input your comand: ");
+38
View File
@@ -0,0 +1,38 @@
pub struct Agent{
pos: (i32, i32),
ang: i8,
}
impl Agent {
pub fn new(x: i32, y: i32, a: i8) -> Self{
Agent {
pos: (x, y),
ang: a,
}
}
pub fn wolk_a(&mut self){
match self.ang {
0 => {
self.pos.0 += 1;
}
1 => {
self.pos.1 += 1;
}
2 => {
self.pos.0 -= 1;
}
3 => {
self.pos.1 -= 1;
}
_ => {}
}
}
pub fn rotate(&mut self, a: i8){
self.ang = ((self.ang + a) % 4 + 4) % 4;
}
}
+57
View File
@@ -0,0 +1,57 @@
fn wrap_pos(p: (i32, i32), s: (usize, usize)) -> (usize, usize) {
let x = ((p.0 % s.0 as i32 + s.0 as i32) % s.0 as i32) as usize;
let y = ((p.1 % s.1 as i32 + s.1 as i32) % s.1 as i32) as usize;
(x, y)
}
#[derive(Clone)]
pub struct TorusGrid<T: Clone>{
vec: Vec<T>,
size: (usize, usize),
}
impl<T: Clone> TorusGrid<T> {
pub fn new(value: T, s: (usize, usize)) -> Self{
let v = vec![value; s.0 * s.1];
TorusGrid {
vec: v,
size: s
}
}
fn index(&self, x: usize, y: usize) -> usize {
y * self.size.0 + x
}
pub fn if_in_grid(&self, s: (i32, i32)) -> bool{
if s.0 < 0 || s.0 > self.size.0 as i32 - 1{
return false;
}
if s.1 < 0 || s.1 > self.size.1 as i32 - 1{
return false;
}
true
}
pub fn get(&self, s: (i32, i32)) -> Option<&T> {
if self.if_in_grid(s) {
return Some(&self.vec[self.index(s.0 as usize, s.1 as usize)]);
}
None
}
pub fn get_mut(&mut self, s: (i32, i32)) -> Option<&mut T> {
if self.if_in_grid(s) {
let idx = self.index(s.0 as usize, s.1 as usize);
return Some(&mut self.vec[idx]);
}
None
}
pub fn get_wrap(&self, p: (i32, i32)) -> &T {
let a = wrap_pos(p, self.size);
&self.vec[self.index(a.0, a.1)]
}
pub fn get_mut_wrap(&mut self, p: (i32, i32)) -> &mut T {
let a = wrap_pos(p, self.size);
let idx = self.index(a.0, a.1);
&mut self.vec[idx]
}
}
+5 -50
View File
@@ -1,52 +1,7 @@
fn wrap_pos(p: (i32, i32), s: (usize, usize)) -> (usize, usize) { mod grid;
let x = ((p.0 % s.0 as i32 + s.0 as i32) % s.0 as i32) as usize; pub use grid::TorusGrid;
let y = ((p.1 % s.1 as i32 + s.1 as i32) % s.1 as i32) as usize;
(x, y) mod agent;
} use agent::Agent;
pub struct TorusGrid<T: Clone> { pub struct World {}
vec: Vec<T>,
size: (usize, usize),
}
impl<T: Clone> TorusGrid<T> {
pub fn new(value: T, s: (usize, usize)) -> Self {
let v = vec![value; s.0 * s.1];
TorusGrid { vec: v, size: s }
}
fn index(&self, x: usize, y: usize) -> usize {
y * self.size.0 + x
}
pub fn if_in_grid(&self, s: (i32, i32)) -> bool {
if s.0 < 0 || s.0 > self.size.0 as i32 - 1 {
return false;
}
if s.1 < 0 || s.1 > self.size.1 as i32 - 1 {
return false;
}
true
}
pub fn get(&self, s: (i32, i32)) -> Option<&T> {
if self.if_in_grid(s) {
return Some(&self.vec[self.index(s.0 as usize, s.1 as usize)]);
}
None
}
pub fn get_mut(&mut self, s: (i32, i32)) -> Option<&mut T> {
if self.if_in_grid(s) {
let idx = self.index(s.0 as usize, s.1 as usize);
return Some(&mut self.vec[idx]);
}
None
}
pub fn get_wrap(&self, p: (i32, i32)) -> &T {
let a = wrap_pos(p, self.size);
&self.vec[self.index(a.0, a.1)]
}
pub fn get_mut_wrap(&mut self, p: (i32, i32)) -> &mut T {
let a = wrap_pos(p, self.size);
let idx = self.index(a.0, a.1);
&mut self.vec[idx]
}
}