Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cac35a4047 | ||
|
|
0eab202b00 | ||
|
|
f9842f635b |
No files matched your search
@@ -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
@@ -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);
|
||||||
|
|||||||
+4
-1
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Agent{
|
pub struct Agent{
|
||||||
pos: (i32, i32),
|
pos: (i32, i32),
|
||||||
ang: i8,
|
ang: i8,
|
||||||
@@ -35,4 +35,7 @@ impl Agent {
|
|||||||
pub fn rotate(&mut self, a: i8){
|
pub fn rotate(&mut self, a: i8){
|
||||||
self.ang = ((self.ang + a) % 4 + 4) % 4;
|
self.ang = ((self.ang + a) % 4 + 4) % 4;
|
||||||
}
|
}
|
||||||
|
pub fn get_pos(&self) -> (i32, i32) {
|
||||||
|
self.pos.clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+45
-2
@@ -1,7 +1,50 @@
|
|||||||
mod grid;
|
mod grid;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
pub use grid::TorusGrid;
|
pub use grid::TorusGrid;
|
||||||
|
|
||||||
mod agent;
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user