test functoin world

This commit is contained in:
lemon-cat committed 2026-05-15 18:12:50 +03:00
1 parent 0eab202b00
commit cac35a4047
2 files changed
+47 -50

No files matched your search

+19 -5
View File
@@ -1,3 +1,5 @@
use std::sync::{Arc, Mutex};
use std::{ use std::{
error, error,
fmt::format, fmt::format,
@@ -12,6 +14,7 @@ use crossterm::{
terminal::{Clear, ClearType, enable_raw_mode}, terminal::{Clear, ClearType, enable_raw_mode},
}; };
use super::Agent;
use super::TorusGrid; use super::TorusGrid;
pub enum PosCom { pub enum PosCom {
@@ -78,17 +81,28 @@ impl Render {
ResetColor ResetColor
); );
} }
pub fn frame(&mut self, grid: &TorusGrid<bool>) { pub fn frame(&mut self, grid: &TorusGrid<bool>, a_grid: &TorusGrid<Option<Arc<Mutex<Agent>>>>) {
let mut out = self.out.lock(); let mut out = self.out.lock();
_ = queue!(out, MoveTo(0, 0)); _ = queue!(out, MoveTo(0, 0));
for i in 0..self.rez.0 { for i in 0..self.rez.0 {
for j in 0..self.rez.1 { for j in 0..self.rez.1 {
let a = grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1)); let ca: Color;
let b = *a as u8 * 200; let cg: Color;
let a = a_grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
let g = grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
let c = *g as u8 * 200;
cg = Color::Rgb { r: c, g: c, b: c };
if a.is_some() {
//c = Color::Red;
ca = Color::Red;
} else {
ca = cg;
}
_ = queue!( _ = queue!(
out, out,
SetBackgroundColor(Color::Rgb { r: b, g: b, b: b }), SetBackgroundColor(cg),
Print(" "), SetForegroundColor(ca),
Print("#"),
ResetColor ResetColor
); );
} }
+28 -45
View File
@@ -10,60 +10,43 @@ use input::*;
mod console_render; mod console_render;
use console_render::*; use console_render::*;
fn init_life(g: &mut TorusGrid<bool>, size: (i32, i32)) -> &mut TorusGrid<bool> { enum InputType {
let mut rng = rand::thread_rng();
for i in 0..size.0 {
for j in 0..size.1 {
if let Some(a) = g.get_mut((i, j)) {
*a = rng.gen_range(0..=1) == 0;
}
}
}
g
}
fn life(g: &mut TorusGrid<bool>, size: (i32, i32)) -> &mut TorusGrid<bool> {
let a = g.clone();
for i in 0..size.0 {
for j in 0..size.1 {
let mut s: i32 = 0;
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;
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);
}
}
g
}
enum Input_type {
comand, comand,
keybard, keybard,
} }
fn main() { fn main() {
println!("Hello, world!"); let mut rnd = rand::thread_rng();
let mut it = Input_type::comand; let mut it = InputType::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 rez: (usize, usize) = (100, 100);
let mut w = world::World::new(rez);
for i in 0..rez.0 {
for j in 0..rez.1 {
if let Some(a) = w.get_mut_grid().get_mut((i as i32, j as i32)) {
*a = rnd.gen_range(0..=1) == 1;
}
}
}
for mut i in 0..20 {
let x = rnd.gen_range(0..=rez.0) as i32;
let y = rnd.gen_range(0..=rez.1) as i32;
let a = w.add_agent(world::Agent::new(x, y, 0));
if a.is_err() {
R.print_err(a.err().unwrap());
i -= 1;
}
}
let mut grid: TorusGrid<bool> = TorusGrid::new(false, g_rez);
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(w.get_grid(), w.get_a_grid());
life(&mut grid, (g_rez.0 as i32, g_rez.1 as i32));
match it { match it {
Input_type::comand => { InputType::comand => {
R.print("Input your comand: "); R.print("Input your comand: ");
let input = Comand_pars(); let input = Comand_pars();
if input.is_err() { if input.is_err() {
@@ -76,13 +59,13 @@ fn main() {
} }
ComandEvent::Key => { ComandEvent::Key => {
R.print("jamp to keybard\n"); R.print("jamp to keybard\n");
it = Input_type::keybard it = InputType::keybard
} }
_ => {} _ => {}
} }
} }
} }
Input_type::keybard => { InputType::keybard => {
let input = Key_pars(); let input = Key_pars();
if input.is_err() { if input.is_err() {
R.print_err(&format!("ERROR: {}", input.err().unwrap())); R.print_err(&format!("ERROR: {}", input.err().unwrap()));
@@ -95,7 +78,7 @@ fn main() {
} }
KeyEvent::Comand => { KeyEvent::Comand => {
R.print("jamp to comand"); R.print("jamp to comand");
it = Input_type::comand it = InputType::comand
} }
KeyEvent::A => { KeyEvent::A => {
R.offset_pos(PosCom::LF); R.offset_pos(PosCom::LF);