test functoin world
This commit is contained in:
1 parent
0eab202b00
commit
cac35a4047
2 files changed
+47
-50
No files matched your search
@@ -1,3 +1,5 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use std::{
|
||||
error,
|
||||
fmt::format,
|
||||
@@ -12,6 +14,7 @@ use crossterm::{
|
||||
terminal::{Clear, ClearType, enable_raw_mode},
|
||||
};
|
||||
|
||||
use super::Agent;
|
||||
use super::TorusGrid;
|
||||
|
||||
pub enum PosCom {
|
||||
@@ -78,17 +81,28 @@ impl Render {
|
||||
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();
|
||||
_ = queue!(out, MoveTo(0, 0));
|
||||
for i in 0..self.rez.0 {
|
||||
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 b = *a as u8 * 200;
|
||||
let ca: Color;
|
||||
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!(
|
||||
out,
|
||||
SetBackgroundColor(Color::Rgb { r: b, g: b, b: b }),
|
||||
Print(" "),
|
||||
SetBackgroundColor(cg),
|
||||
SetForegroundColor(ca),
|
||||
Print("#"),
|
||||
ResetColor
|
||||
);
|
||||
}
|
||||
|
||||
+28
-45
@@ -10,60 +10,43 @@ use input::*;
|
||||
mod console_render;
|
||||
use console_render::*;
|
||||
|
||||
fn init_life(g: &mut TorusGrid<bool>, size: (i32, i32)) -> &mut TorusGrid<bool> {
|
||||
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 {
|
||||
enum InputType {
|
||||
comand,
|
||||
keybard,
|
||||
}
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let mut it = Input_type::comand;
|
||||
let mut rnd = rand::thread_rng();
|
||||
let mut it = InputType::comand;
|
||||
let mut R = Render::new();
|
||||
R.clear();
|
||||
_ = 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 {
|
||||
//R.clear();
|
||||
R.frame(&grid);
|
||||
life(&mut grid, (g_rez.0 as i32, g_rez.1 as i32));
|
||||
R.frame(w.get_grid(), w.get_a_grid());
|
||||
match it {
|
||||
Input_type::comand => {
|
||||
InputType::comand => {
|
||||
R.print("Input your comand: ");
|
||||
let input = Comand_pars();
|
||||
if input.is_err() {
|
||||
@@ -76,13 +59,13 @@ fn main() {
|
||||
}
|
||||
ComandEvent::Key => {
|
||||
R.print("jamp to keybard\n");
|
||||
it = Input_type::keybard
|
||||
it = InputType::keybard
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Input_type::keybard => {
|
||||
InputType::keybard => {
|
||||
let input = Key_pars();
|
||||
if input.is_err() {
|
||||
R.print_err(&format!("ERROR: {}", input.err().unwrap()));
|
||||
@@ -95,7 +78,7 @@ fn main() {
|
||||
}
|
||||
KeyEvent::Comand => {
|
||||
R.print("jamp to comand");
|
||||
it = Input_type::comand
|
||||
it = InputType::comand
|
||||
}
|
||||
KeyEvent::A => {
|
||||
R.offset_pos(PosCom::LF);
|
||||
|
||||
Reference in new issue
Block a user