17 Commits
Author SHA1 Message Date
lemon-cat cac35a4047 test functoin world 2026-05-15 18:12:50 +03:00
lemon-cat 0eab202b00 merge dev-world to lemon-cat for test new functional 2026-05-15 16:52:29 +03:00
vova-rs f9842f635b add fn add_agent(), temporary mutable access privileges in Word 2026-05-15 16:06:03 +03:00
lemon-cat 125fd73625 test grid for life 2026-05-15 15:54:40 +03:00
lemon-cat 66c0bb839e comit for merge 2026-05-15 14:18:37 +03:00
vova-rs 63f2ba31d0 agent staruct, TorusGrid derive Clone 2026-05-15 14:16:14 +03:00
lemon-cat 9c61078414 test functional for render, grid, input. Add some function. Add colot print in render ' pub fn print_c()', add offset for pos in render 'pub fn offset_pos()'. Create generete grid in main.rs as random value and test render this. 2026-05-14 18:04:42 +03:00
lemon-cat d9b301ea04 maerg for test 2026-05-14 15:01:17 +03:00
vova-rs 2359187628 fix: add pub to TorusGrid 2026-05-14 14:50:48 +03:00
lemon-cat 3663c74bd9 Merge branch 'render' into lemon-cat 2026-05-14 14:50:41 +03:00
lemon-cat 6f292cbb21 Merge branch 'dev-world' into lemon-cat 2026-05-14 14:47:11 +03:00
vova-rs 1bca11eb4c TorusGrid: был завершон 2026-05-14 13:25:39 +03:00
lemon-cat 7da7d578e1 pos f32 to pos usize 2026-05-14 12:22:29 +03:00
lemon-cat 6534ed18b4 add, function print err 2026-05-13 13:08:59 +03:00
lemon-cat d0c051c7b1 add base fn for render: new, init, set_rez, set_pos, frame, but this have more code 2026-05-12 20:26:23 +03:00
lemon-cat 3196347936 add lemon-cat 2026-05-11 16:35:12 +03:00
vova-rs 7067e3985c hello 2026-05-11 16:15:42 +03:00
7 changed files with 352 additions and 15 deletions

No files matched your search

+1
View File
@@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
crossterm = "0.29"
rand = "0.8"
+117
View File
@@ -0,0 +1,117 @@
use std::sync::{Arc, Mutex};
use std::{
error,
fmt::format,
io::{Stdout, Write, stdout},
process::Output,
};
use crossterm::{
cursor::MoveTo,
execute, queue,
style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor},
terminal::{Clear, ClearType, enable_raw_mode},
};
use super::Agent;
use super::TorusGrid;
pub enum PosCom {
UP,
DW,
LF,
RH,
}
pub struct Render {
out: Stdout,
rez: (usize, usize),
pos: (i32, i32),
}
impl Render {
pub fn new() -> Self {
Render {
out: stdout(),
rez: (10, 10),
pos: (0, 0),
}
}
pub fn set_rez(&mut self, a: usize, b: usize) -> Result<(), String> {
if a == 0 || b == 0 {
return Err("Invalid resolution\n".to_string());
}
self.rez = (a, b);
Ok(())
}
pub fn set_pos(&mut self, a: i32, b: i32) {
self.pos = (a, b);
}
pub fn offset_pos(&mut self, a: PosCom) {
match a {
PosCom::LF => {
self.pos.1 -= 1;
}
PosCom::UP => {
self.pos.0 -= 1;
}
PosCom::DW => {
self.pos.0 += 1;
}
PosCom::RH => {
self.pos.1 += 1;
}
}
}
pub fn clear(&mut self) {
_ = execute!(self.out, Clear(ClearType::All), MoveTo(0, 0));
}
pub fn print(&mut self, out_str: &str) {
_ = execute!(self.out, Print(out_str));
}
pub fn print_c(&mut self, out_str: &str, c: Color) {
_ = execute!(self.out, SetForegroundColor(c), Print(out_str), ResetColor);
}
pub fn print_err(&mut self, out_err: &str) {
_ = execute!(
self.out,
SetForegroundColor(Color::Red),
Print(out_err),
ResetColor
);
}
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 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(cg),
SetForegroundColor(ca),
Print("#"),
ResetColor
);
}
_ = queue!(out, Print("\r\n"));
}
_ = out.flush();
self.print_c(
&format!("pos is: {} {} \n", self.pos.1, self.pos.0),
Color::Yellow,
);
}
}
+23 -1
View File
@@ -3,7 +3,7 @@ use crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode},
};
use std::io::{self, Write};
use std::io::{self, Write, stdout};
use crate::input;
@@ -29,6 +29,8 @@ pub fn Comand_pars() -> Result<ComandEvent, String> {
}
"key" => {
_ = enable_raw_mode();
let mut out = stdout();
_ = crossterm::execute!(out, crossterm::cursor::Hide);
return Ok(ComandEvent::Key);
}
_ => {}
@@ -42,18 +44,38 @@ pub enum KeyEvent {
None,
Exit,
Comand,
A,
W,
S,
D,
}
pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => {
_ = disable_raw_mode();
let mut out = stdout();
_ = crossterm::execute!(out, crossterm::cursor::Show);
return Ok(Some(KeyEvent::Exit));
}
KeyCode::Char('c') => {
_ = disable_raw_mode();
let mut out = stdout();
_ = crossterm::execute!(out, crossterm::cursor::Show);
return Ok(Some(KeyEvent::Comand));
}
KeyCode::Char('a') => {
return Ok(Some(KeyEvent::A));
}
KeyCode::Char('w') => {
return Ok(Some(KeyEvent::W));
}
KeyCode::Char('s') => {
return Ok(Some(KeyEvent::S));
}
KeyCode::Char('d') => {
return Ok(Some(KeyEvent::D));
}
_ => {}
}
}
+63 -14
View File
@@ -1,47 +1,96 @@
use std::{clone, sync::atomic::AtomicI32};
use rand::Rng;
mod world;
pub use world::*;
mod input;
use input::*;
enum Input_type {
mod console_render;
use console_render::*;
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 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;
}
}
loop {
//R.clear();
R.frame(w.get_grid(), w.get_a_grid());
match it {
Input_type::comand => {
println!("input your comand: ");
InputType::comand => {
R.print("Input your comand: ");
let input = Comand_pars();
if input.is_err() {
println!("ERROR: {}", input.err().unwrap());
R.print_err(&format!("ERROR: {}\n", input.err().unwrap()));
} else {
match input.unwrap() {
ComandEvent::Exit => {
println!("progarm is end");
R.print("progarm is end\n");
return;
}
ComandEvent::Key => {
println!("jamp to keybard");
it = Input_type::keybard
R.print("jamp to keybard\n");
it = InputType::keybard
}
_ => {}
}
}
}
Input_type::keybard => {
InputType::keybard => {
let input = Key_pars();
if input.is_err() {
println!("ERROR: {}", input.err().unwrap())
R.print_err(&format!("ERROR: {}", input.err().unwrap()));
} else {
if let Some(k) = input.unwrap() {
match k {
KeyEvent::Exit => {
println!("progarm is end");
R.print("progarm is end\n");
return;
}
KeyEvent::Comand => {
println!("jamp to comand");
it = Input_type::comand
R.print("jamp to comand");
it = InputType::comand
}
KeyEvent::A => {
R.offset_pos(PosCom::LF);
}
KeyEvent::W => {
R.offset_pos(PosCom::UP);
}
KeyEvent::S => {
R.offset_pos(PosCom::DW);
}
KeyEvent::D => {
R.offset_pos(PosCom::RH);
}
_ => {}
}
+41
View File
@@ -0,0 +1,41 @@
#[derive(Clone)]
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;
}
pub fn get_pos(&self) -> (i32, i32) {
self.pos.clone()
}
}
+57
View File
@@ -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]
}
}
+50
View File
@@ -0,0 +1,50 @@
mod grid;
use std::sync::{Arc, Mutex};
pub use grid::TorusGrid;
mod agent;
pub use agent::Agent;
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
}
}