homework is completed

This commit is contained in:
lemon-cat committed 2026-05-18 15:54:41 +03:00
1 parent 6ffbfbb7bf
commit f235fdcea6
3 files changed
+68 -20

No files matched your search

+8 -4
View File
@@ -1,5 +1,3 @@
use std::clone;
use rand::Rng;
#[derive(Clone)]
pub struct Grid {
@@ -15,10 +13,16 @@ impl Grid {
}
}
pub fn push_in(&mut self, p: bool, pos: &(usize, usize)) {
if (pos.1 * self.rez.0 + pos.0) > (self.rez.0 * self.rez.1) {
return;
}
self.grd[pos.1 * self.rez.0 + pos.0] = p;
}
pub fn who_in(&self, pos: &(usize, usize)) -> bool {
return self.grd[pos.1 * self.rez.0 + pos.0] == true;
pub fn who_in(&self, pos: &(usize, usize)) -> Option<bool> {
if (pos.1 * self.rez.0 + pos.0) > (self.rez.0 * self.rez.1) {
return None;
}
return Some(self.grd[pos.1 * self.rez.0 + pos.0] == true);
}
pub fn rand(&mut self) {
let mut rng = rand::thread_rng();
+56 -14
View File
@@ -4,22 +4,63 @@ mod render;
use grid::*;
use input::*;
use rand::Rng;
use render::*;
//use std::clone;
use std::{clone, sync::atomic::AtomicI32};
fn step(g: &mut Grid, rez: &(usize, usize)) {
let mut rng = rand::thread_rng();
let a = g.clone();
for i in 0..rez.0 as i32 {
for j in 0..(rez.1 - 1) as i32 {
if !a.who_in(&(i as usize, (j + 1) as usize)) {
g.push_in(false, &(i as usize, j as usize));
g.push_in(true, &(i as usize, (j + 1) as usize));
} else if !a.who_in(&((i + 1) as usize, (j + 1) as usize)) && i < rez.1 as i32 {
g.push_in(false, &(i as usize, j as usize));
g.push_in(true, &((i + 1) as usize, (j + 1) as usize));
} else if !a.who_in(&((i - 1) as usize, (j + 1) as usize)) && i > 0 {
g.push_in(false, &(i as usize, j as usize));
g.push_in(true, &((i - 1) as usize, (j + 1) as usize));
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 v = rng.gen_range(0..=3);
match v {
0 => {
if let Some(k) = a.who_in(&(i, j + 1)) {
match k {
false => {
g.push_in(false, &(i, j));
g.push_in(true, &(i, j + 1));
}
true => {}
}
}
}
1 => {
if (i + 1) < rez.0 {
if let Some(k) = a.who_in(&(i + 1, j + 1)) {
match k {
false => {
g.push_in(false, &(i, j));
g.push_in(true, &(i + 1, j + 1));
}
true => {}
}
}
}
}
3 => {
if i != 0 {
if let Some(k) = a.who_in(&(i - 1, j + 1)) {
match k {
false => {
g.push_in(false, &(i, j));
g.push_in(true, &(i - 1, j + 1));
}
true => {}
}
}
}
}
_ => {}
}
}
false => {}
}
}
}
}
@@ -38,10 +79,11 @@ fn main() {
let rez: (usize, usize) = (40, 80);
_ = r.resize(&rez);
let mut space = Grid::init(&rez);
space.rand();
//space.rand();
loop {
_ = r.frame(&space);
step(&mut space, &rez);
space.push_in(true, &(rez.0 / 2, 0));
match it {
InputType::comand => {
_ = r.print("print comand:");
+4 -2
View File
@@ -37,16 +37,18 @@ impl Render {
Ok(())
}
pub fn frame(&mut self, g: &Grid) -> Result<(), std::io::Error> {
queue!(self.out, MoveTo(0, 0))?;
for i in 0..self.rez.0 {
for j in 0..self.rez.1 {
let r = g.who_in(&(i, j));
match r {
true => {
Some(true) => {
queue!(self.out, Print("፨"))?;
}
false => {
Some(false) => {
queue!(self.out, Print(" "))?;
}
_ => {}
}
}
queue!(self.out, Print("\r\n"))?;