Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ffbfbb7bf | ||
|
|
1e9529733f |
No files matched your search
+11
@@ -0,0 +1,11 @@
|
|||||||
|
# бинарники
|
||||||
|
/target/
|
||||||
|
|
||||||
|
# Логи и временные файлы Rust
|
||||||
|
**/*.rs.bk
|
||||||
|
Cargo.lock
|
||||||
|
|
||||||
|
# асеты
|
||||||
|
assets/**/*
|
||||||
|
!assets/shaders/
|
||||||
|
!assets/shaders/**/*
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "homework_sand_rs"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
crossterm = "0.29"
|
||||||
|
rand = "0.8"
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
use std::clone;
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Grid {
|
||||||
|
grd: Vec<bool>,
|
||||||
|
rez: (usize, usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Grid {
|
||||||
|
pub fn init(r: &(usize, usize)) -> Self {
|
||||||
|
Grid {
|
||||||
|
grd: vec![false; r.0 * r.1],
|
||||||
|
rez: r.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn push_in(&mut self, p: bool, pos: &(usize, usize)) {
|
||||||
|
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 rand(&mut self) {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
for i in 0..self.rez.0 {
|
||||||
|
for j in 0..self.rez.1 {
|
||||||
|
let mut r = rng.gen_range(0..=1) == 0;
|
||||||
|
self.push_in(r, &(i, j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
use crossterm::{
|
||||||
|
event::{self, Event, KeyCode},
|
||||||
|
terminal::{disable_raw_mode, enable_raw_mode},
|
||||||
|
};
|
||||||
|
use std::io::{self, Write, stdout};
|
||||||
|
|
||||||
|
pub enum ComandEvent {
|
||||||
|
//who is this
|
||||||
|
None,
|
||||||
|
Exit,
|
||||||
|
Key,
|
||||||
|
}
|
||||||
|
pub fn Comand_pars() -> Result<ComandEvent, String> {
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut input)
|
||||||
|
.expect("error input scan\n");
|
||||||
|
let mut words: Vec<&str> = input.trim().split(" ").collect();
|
||||||
|
if words.len() == 0 {
|
||||||
|
return Err(String::from("Zerro lenth comand\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
match words[0] {
|
||||||
|
"q" => {
|
||||||
|
return Ok(ComandEvent::Exit);
|
||||||
|
}
|
||||||
|
"key" => {
|
||||||
|
_ = enable_raw_mode();
|
||||||
|
let mut out = stdout();
|
||||||
|
_ = crossterm::execute!(out, crossterm::cursor::Hide);
|
||||||
|
return Ok(ComandEvent::Key);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(ComandEvent::None)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum KeyEvent {
|
||||||
|
//KeuEvent for your input
|
||||||
|
None,
|
||||||
|
Exit,
|
||||||
|
Comand,
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Some(KeyEvent::None))
|
||||||
|
}
|
||||||
+85
@@ -0,0 +1,85 @@
|
|||||||
|
mod grid;
|
||||||
|
mod input;
|
||||||
|
mod render;
|
||||||
|
|
||||||
|
use grid::*;
|
||||||
|
use input::*;
|
||||||
|
use render::*;
|
||||||
|
//use std::clone;
|
||||||
|
use std::{clone, sync::atomic::AtomicI32};
|
||||||
|
fn step(g: &mut Grid, rez: &(usize, usize)) {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) = (40, 80);
|
||||||
|
_ = r.resize(&rez);
|
||||||
|
let mut space = Grid::init(&rez);
|
||||||
|
space.rand();
|
||||||
|
loop {
|
||||||
|
_ = r.frame(&space);
|
||||||
|
step(&mut space, &rez);
|
||||||
|
match it {
|
||||||
|
InputType::comand => {
|
||||||
|
_ = r.print("print 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;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
use std::io::{Stdout, Write, stdout};
|
||||||
|
|
||||||
|
use crossterm::{
|
||||||
|
cursor::MoveTo,
|
||||||
|
queue,
|
||||||
|
style::Print,
|
||||||
|
terminal::{Clear, ClearType},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::grid::Grid;
|
||||||
|
|
||||||
|
pub struct Render {
|
||||||
|
out: Stdout,
|
||||||
|
rez: (usize, usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render {
|
||||||
|
// add code here
|
||||||
|
pub fn init() -> Self {
|
||||||
|
Render {
|
||||||
|
out: stdout(),
|
||||||
|
rez: (40, 80),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn resize(&mut self, r: &(usize, usize)) -> Result<(), std::io::Error> {
|
||||||
|
self.rez = r.clone();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn clear(&mut self) -> Result<(), std::io::Error> {
|
||||||
|
queue!(self.out, Clear(ClearType::All), MoveTo(0, 0))?;
|
||||||
|
self.out.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn print(&mut self, out: &str) -> Result<(), std::io::Error> {
|
||||||
|
queue!(self.out, Print(out), Print("\r\n"))?;
|
||||||
|
self.out.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn frame(&mut self, g: &Grid) -> Result<(), std::io::Error> {
|
||||||
|
for i in 0..self.rez.0 {
|
||||||
|
for j in 0..self.rez.1 {
|
||||||
|
let r = g.who_in(&(i, j));
|
||||||
|
match r {
|
||||||
|
true => {
|
||||||
|
queue!(self.out, Print("፨"))?;
|
||||||
|
}
|
||||||
|
false => {
|
||||||
|
queue!(self.out, Print(" "))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
queue!(self.out, Print("\r\n"))?;
|
||||||
|
}
|
||||||
|
self.out.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user