Files
homework_sand_rs/src/main.rs
T
2026-05-18 16:12:43 +03:00

127 lines
4.0 KiB
Rust

mod grid;
mod input;
mod render;
use grid::*;
use input::*;
use rand::Rng;
use render::*;
fn step(g: &mut Grid, rez: &(usize, usize)) {
let mut rng = rand::thread_rng();
let a = g.clone();
for i in 0..rez.0 {
for j in 0..(rez.1 - 1) {
if let Some(k) = a.who_in(&(i, j)) {
match k {
true => {
let mut v = true;
if let Some(k) = a.who_in(&(i, j + 1))
&& v
{
match k {
false => {
g.push_in(false, &(i, j));
g.push_in(true, &(i, j + 1));
v = false;
}
true => {}
}
}
if (i + 1) < rez.0 {
if let Some(k) = a.who_in(&(i + 1, j + 1))
&& v
{
match k {
false => {
g.push_in(false, &(i, j));
g.push_in(true, &(i + 1, j + 1));
v = false;
}
true => {}
}
}
}
if i != 0 {
if let Some(k) = a.who_in(&(i - 1, j + 1))
&& v
{
match k {
false => {
g.push_in(false, &(i, j));
g.push_in(true, &(i - 1, j + 1));
}
true => {}
}
}
}
}
false => {}
}
}
}
}
}
enum InputType {
keyboard,
comand,
}
fn main() {
let mut it = InputType::comand;
let mut r = Render::init();
_ = r.clear();
_ = r.print("Hello sand!");
//_ = r.frame();
let rez: (usize, usize) = (80, 40);
_ = r.resize(&rez);
let mut space = Grid::init(&rez);
//space.rand();
loop {
_ = r.frame(&space);
step(&mut space, &rez);
space.push_in(true, &(rez.0 / 2, 0));
match it {
InputType::comand => {
_ = r.print("input comand:");
let input = Comand_pars();
if input.is_err() {
_ = r.print(&input.err().unwrap());
} else {
match input.unwrap() {
ComandEvent::Exit => {
_ = r.print("end program\n");
return;
}
ComandEvent::Key => {
it = InputType::keyboard;
}
_ => {}
}
}
}
InputType::keyboard => {
let input = Key_pars();
if input.is_err() {
_ = r.print(&format!("{}", input.err().unwrap()));
} else {
if let Some(k) = input.unwrap() {
match k {
KeyEvent::Exit => {
_ = r.print("end program\n");
return;
}
KeyEvent::Comand => {
it = InputType::comand;
}
_ => {}
}
}
}
}
}
}
}