Compare commits

..
4 Commits
Author SHA1 Message Date
lemon-cat 29e390be76 fix destroy walls and add vin 2026-05-22 12:11:43 +03:00
lemon-cat 716c374f1d rand gen Maze is relise 2026-05-21 09:16:42 +03:00
lemon-cat 05a039d88f relise destroy walls 2026-05-19 20:45:18 +03:00
lemon-cat c85fcb8753 start homework maze, but this code is duesn't work 2026-05-19 17:24:34 +03:00
4 changed files with 328 additions and 98 deletions

No files matched your search

+201 -19
View File
@@ -1,36 +1,218 @@
use rand::Rng;
use rand::{Rng, rngs, seq::index};
use crate::input;
#[derive(Clone)]
pub struct Status {
track: bool,
r_wall: bool,
d_wall: bool,
}
impl Status {
pub fn init(t: bool, r: bool, d: bool) -> Self {
Status {
track: t,
r_wall: r,
d_wall: d,
}
}
pub fn who_in(&self, a: char) -> Option<bool> {
match a {
't' => {
return Some(self.track.clone());
}
'r' => {
return Some(self.r_wall.clone());
}
'd' => {
return Some(self.d_wall.clone());
}
_ => {
return None;
}
}
}
pub fn push_in(&mut self, a: char, v: bool) {
match a {
't' => {
self.track = v;
}
'r' => {
self.r_wall = v;
}
'd' => {
self.d_wall = v;
}
_ => {}
}
}
}
pub struct Grid {
grd: Vec<bool>,
maze: Vec<Status>,
rez: (usize, usize),
pos: (i32, i32),
vin_pos: (i32, i32),
size: usize,
his: Vec<(i32, i32)>,
}
impl Grid {
pub fn init(r: &(usize, usize)) -> Self {
Grid {
grd: vec![false; r.0 * r.1],
maze: vec![Status::init(false, true, true); r.0 * r.1],
rez: r.clone(),
pos: (1, 1),
vin_pos: (r.0 as i32 - 1, r.1 as i32 - 1),
size: r.0 * r.1,
his: Vec::new(),
}
}
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;
fn is_pos(&self, pos: &(i32, i32)) -> bool {
let x = self.pos.0 + pos.0;
let y = self.pos.1 + pos.1;
(x >= 0 && x < self.rez.0 as i32) && (y >= 0 && y < self.rez.1 as i32)
}
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 is_vin(&self) -> bool {
self.pos.0 == self.vin_pos.0 && self.pos.1 == self.vin_pos.1
}
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));
fn index(&self) -> usize {
(self.pos.1 * self.rez.0 as i32 + self.pos.0) as usize
}
fn get_index(&self, pos: &(usize, usize)) -> usize {
let s = pos.1 * self.rez.0 + pos.0;
if s < self.size {
s
} else {
println!("index error 404");
0
}
}
pub fn get_cursor(&self) -> (i32, i32) {
self.pos.clone()
}
pub fn get_vin(&self) -> (i32, i32) {
self.vin_pos.clone()
}
fn mut_pos(&mut self, pos: &(i32, i32)) -> bool {
if self.is_pos(pos) {
self.pos.0 += pos.0.clone();
self.pos.1 += pos.1.clone();
return true;
}
return false;
}
pub fn game_pace(&mut self, pos: &(i32, i32)) {
if self.is_pos(pos) {
match pos {
(1, 0) => {
let p = self.index();
if !self.maze[p].r_wall {
self.mut_pos(pos);
}
}
(-1, 0) => {
let p = self.get_index(&((self.pos.0 - 1) as usize, self.pos.1 as usize));
if !self.maze[p].r_wall {
self.mut_pos(pos);
}
}
(0, 1) => {
let p = self.index();
if !self.maze[p].d_wall {
self.mut_pos(pos);
}
}
(0, -1) => {
let p = self.get_index(&(self.pos.0 as usize, (self.pos.1 - 1) as usize));
if !self.maze[p].d_wall {
self.mut_pos(pos);
}
}
_ => {}
}
}
}
pub fn pace(&mut self, pos: &(i32, i32)) {
if self.is_pos(pos) {
let mut p = self.index();
self.maze[p].push_in('t', true);
match pos {
(1, 0) => {
// right
self.maze[p].push_in('r', false);
self.mut_pos(pos);
}
(-1, 0) => {
//left
self.mut_pos(pos);
p = self.index();
self.maze[p].push_in('r', false);
}
(0, 1) => {
//down
self.maze[p].push_in('d', false);
self.mut_pos(pos);
}
(0, -1) => {
//up
self.mut_pos(pos);
p = self.index();
self.maze[p].push_in('d', false);
}
_ => {}
}
} else {
println!("STOP THIS!");
}
}
pub fn rand_pace(&mut self) -> Option<bool> {
let mut rng = rand::thread_rng();
let mut route = vec![(1, 0), (-1, 0), (0, 1), (0, -1)];
route.retain(|dir| {
if !self.is_pos(dir) {
return false;
}
let p = self.get_index(&((self.pos.0 + dir.0) as usize, (self.pos.1 + dir.1) as usize));
!self.maze[p].track
});
if route.is_empty() {
println!("rand pace is false");
if !self.his.is_empty() {
let index = self.index();
self.maze[index].track = true;
self.pos.0 = self.his[self.his.len() - 1].0;
self.pos.1 = self.his[self.his.len() - 1].1;
if self.his.pop().is_some() {
return Some(true);
} else {
return None;
}
} else {
return None;
}
} else {
self.his.push(self.pos.clone());
let dir = route[rng.gen_range(0..route.len())];
self.pace(&dir);
return Some(true);
}
}
pub fn rand_gen(&mut self) {
let mut rng = rand::thread_rng();
let x = rng.gen_range(0..self.rez.0);
let y = rng.gen_range(0..self.rez.1);
self.mut_pos(&(x as i32, y as i32));
let mut i = true;
while i {
i = self.rand_pace().is_some();
}
}
pub fn who_in(&self, pos: &(usize, usize)) -> &Status {
&self.maze[self.get_index(pos)]
}
}
+21
View File
@@ -1,4 +1,5 @@
use crossterm::{
cursor::Show,
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode},
};
@@ -41,6 +42,11 @@ pub enum KeyEvent {
None,
Exit,
Comand,
A,
W,
S,
D,
R,
}
pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
if let Event::Key(key) = event::read()? {
@@ -57,6 +63,21 @@ pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
_ = 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));
}
KeyCode::Char('r') => {
return Ok(Some(KeyEvent::R));
}
_ => {}
}
}
+37 -65
View File
@@ -2,69 +2,13 @@ mod grid;
mod input;
mod render;
use std::io::Stdout;
use crossterm::{cursor::Show, terminal::disable_raw_mode};
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,
@@ -73,16 +17,25 @@ 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);
let rez: (usize, usize) = (40, 20);
_ = r.resize(&rez);
let mut space = Grid::init(&rez);
//space.rand();
space.rand_gen();
loop {
if space.is_vin() {
_ = r.print(&"You is vin!");
match it {
InputType::keyboard => {
let mut out = std::io::stdout();
_ = crossterm::execute!(out, crossterm::cursor::Show);
_ = disable_raw_mode();
}
_ => {}
}
return;
}
_ = r.clear();
_ = r.frame(&space);
step(&mut space, &rez);
space.push_in(true, &(rez.0 / 2, 0));
match it {
InputType::comand => {
_ = r.print("input comand:");
@@ -116,6 +69,25 @@ fn main() {
KeyEvent::Comand => {
it = InputType::comand;
}
KeyEvent::A => {
space.game_pace(&(-1, 0));
}
KeyEvent::W => {
space.game_pace(&(0, -1));
}
KeyEvent::S => {
space.game_pace(&(0, 1));
}
KeyEvent::D => {
space.game_pace(&(1, 0));
}
KeyEvent::R => {
if let Some(k) = space.rand_pace() {
r.print(&format!("rand_pace is {}", k));
} else {
r.print(&"rand_pace is None");
}
}
_ => {}
}
}
+69 -14
View File
@@ -3,7 +3,7 @@ use std::io::{Stdout, Write, stdout};
use crossterm::{
cursor::MoveTo,
queue,
style::Print,
style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor},
terminal::{Clear, ClearType},
};
@@ -37,21 +37,76 @@ 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.1 {
for j in 0..self.rez.0 {
let r = g.who_in(&(j, i));
match r {
Some(true) => {
queue!(self.out, Print("፨"))?;
}
Some(false) => {
queue!(self.out, Print(" "))?;
}
_ => {}
queue!(
self.out,
SetBackgroundColor(Color::Grey),
SetForegroundColor(Color::Black)
)?;
for j in 0..self.rez.0 * 2 + 1 {
match (j % 2) == 0 {
true => {
queue!(self.out, Print("╬"))?;
}
false => {
queue!(self.out, Print("═"))?;
}
}
queue!(self.out, Print("\r\n"))?;
}
queue!(self.out, ResetColor, Print("\r\n"))?;
for i in 0..self.rez.1 {
queue!(
self.out,
SetBackgroundColor(Color::Grey),
SetForegroundColor(Color::Black)
)?;
queue!(self.out, Print("║"))?;
for j in 0..self.rez.0 {
let r = g.who_in(&(j, i));
let c = g.get_cursor();
let v = g.get_vin();
if c.0 as usize == j && c.1 as usize == i {
queue!(self.out, SetBackgroundColor(Color::Magenta))?;
queue!(self.out, Print(" "), ResetColor)?;
queue!(
self.out,
SetBackgroundColor(Color::Grey),
SetForegroundColor(Color::Black)
)?;
} else if v.0 as usize == j && v.1 as usize == i {
queue!(self.out, SetBackgroundColor(Color::Green))?;
queue!(self.out, Print(" "), ResetColor)?;
queue!(
self.out,
SetBackgroundColor(Color::Grey),
SetForegroundColor(Color::Black)
)?;
} else {
queue!(self.out, Print(" "))?;
}
if r.who_in('r').unwrap_or(false) {
queue!(self.out, Print("║"))?;
} else {
queue!(self.out, Print(" "))?;
}
}
queue!(self.out, ResetColor, Print("\r\n"))?;
queue!(
self.out,
SetBackgroundColor(Color::Grey),
SetForegroundColor(Color::Black)
)?;
queue!(self.out, Print("╬"))?;
for j in 0..self.rez.0 {
let r = g.who_in(&(j, i));
if r.who_in('d').unwrap_or(false) {
queue!(self.out, Print("═"))?;
} else {
queue!(self.out, Print(" "))?;
}
queue!(self.out, Print("╬"))?;
}
queue!(self.out, ResetColor, Print("\r\n"))?;
}
self.out.flush()?;
Ok(())