3 Commits
4 changed files with 96 additions and 53 deletions

No files matched your search

+19 -5
View File
@@ -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
View File
@@ -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);
+4 -1
View File
@@ -1,7 +1,7 @@
#[derive(Clone)]
pub struct Agent{
pos: (i32, i32),
ang: i8,
@@ -35,4 +35,7 @@ impl Agent {
pub fn rotate(&mut self, a: i8){
self.ang = ((self.ang + a) % 4 + 4) % 4;
}
pub fn get_pos(&self) -> (i32, i32) {
self.pos.clone()
}
}
+45 -2
View File
@@ -1,7 +1,50 @@
mod grid;
use std::sync::{Arc, Mutex};
pub use grid::TorusGrid;
mod agent;
use agent::Agent;
pub use agent::Agent;
pub struct World {}
pub struct World {
grid: TorusGrid<bool>,
agents: Vec<Arc<Mutex<Agent>>>,
a_grid: TorusGrid<Option<Arc<Mutex<Agent>>>>,
size: (usize, usize),
}
impl World {
pub fn new(s: (usize, usize)) -> Self {
World {
grid: TorusGrid::new(false, s),
agents: Vec::new(),
a_grid: TorusGrid::new(None, s),
size: s,
}
}
pub fn get_size(&self) -> (usize, usize) {
self.size.clone()
}
pub fn add_agent(&mut self, ag: Agent) -> Result<(), &str> {
let pos = ag.get_pos();
let p = self.a_grid.get_mut_wrap(pos);
// if *p != None {
// return Err("w");
// }
if p.is_some() {
return Err("ERROR: pos занято");
}
self.agents.push(Arc::new(Mutex::new(ag)));
*p = Some(Arc::clone(&self.agents[self.agents.len() - 1]));
Ok(())
}
pub fn get_grid(&self) -> &TorusGrid<bool> {
&self.grid
}
pub fn get_mut_grid(&mut self) -> &mut TorusGrid<bool> {
&mut self.grid
}
pub fn get_a_grid(&self) -> &TorusGrid<Option<Arc<Mutex<Agent>>>> {
&self.a_grid
}
}