Compare commits
4
Commits
9c61078414
...
125fd73625
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
125fd73625 | ||
|
|
66c0bb839e | ||
|
|
63f2ba31d0 | ||
|
|
2359187628 |
No files matched your search
@@ -78,13 +78,13 @@ impl Render {
|
|||||||
ResetColor
|
ResetColor
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
pub fn frame(&mut self, grid: &TorusGrid<i32>) {
|
pub fn frame(&mut self, grid: &TorusGrid<bool>) {
|
||||||
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 a = grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
|
||||||
let b = *a as u8;
|
let b = *a as u8 * 200;
|
||||||
_ = queue!(
|
_ = queue!(
|
||||||
out,
|
out,
|
||||||
SetBackgroundColor(Color::Rgb { r: b, g: b, b: b }),
|
SetBackgroundColor(Color::Rgb { r: b, g: b, b: b }),
|
||||||
|
|||||||
+39
-10
@@ -1,3 +1,5 @@
|
|||||||
|
use std::{clone, sync::atomic::AtomicI32};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
mod world;
|
mod world;
|
||||||
pub use world::*;
|
pub use world::*;
|
||||||
@@ -8,31 +10,58 @@ 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> {
|
||||||
|
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 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 {
|
let mut grid: TorusGrid<bool> = TorusGrid::new(false, g_rez);
|
||||||
for j in 0..g_rez.1 {
|
init_life(&mut grid, (g_rez.0 as i32, g_rez.1 as i32));
|
||||||
if let Some(a) = grid.get_mut((i as i32, j as i32)) {
|
|
||||||
*a = rng.gen_range(0..=255) 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: ");
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
@@ -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]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user