start homework maze, but this code is duesn't work

This commit is contained in:
lemon-cat committed 2026-05-19 17:24:34 +03:00
1 parent b8a21f579d
commit c85fcb8753
3 files changed
+127 -91

No files matched your search

+115 -17
View File
@@ -1,36 +1,134 @@
use rand::Rng;
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),
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(),
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;
pub fn pos(&self, p: (i32, i32)) -> usize {
let s = p.0 * self.rez.0 as i32 + p.1;
if s < self.size as i32 {
return s as usize;
}
self.grd[pos.1 * self.rez.0 + pos.0] = p;
return 0;
}
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) {
pub fn generete(&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));
let a = self.rez.0 as i32;
let b = self.rez.1 as i32;
let mut i = rng.gen_range(0 + 1..a - 1);
let mut j = rng.gen_range(0 + 1..b - 1);
loop {
if (i > 0 && i < (a - 1)) && (j > 0 && j < (b - 1)) {
println!("me at {} {}", i, j);
let mut route: Vec<(i32, i32)> = Vec::new();
println!("rout len is: {}", route.len());
let mut r = self.maze[self.pos((i + 1, j))].who_in('t').unwrap_or(false);
if r == false {
println!("up is free");
route.push((1, 0));
}
r = self.maze[self.pos((i - 1, j))].who_in('t').unwrap_or(false);
if r == false {
println!("down is free");
route.push((-1, 0));
}
r = self.maze[self.pos((i, j + 1))].who_in('t').unwrap_or(false);
if r == false {
println!("right is free");
route.push((0, 1));
}
r = self.maze[self.pos((i, j - 1))].who_in('t').unwrap_or(false);
if r == false {
println!("left is free");
route.push((0, -1));
}
if route.len() > 0 {
let index = rng.gen_range(0..route.len());
println!("index is: {}", index);
let mut p = self.pos((i, j));
self.maze[p].push_in('t', true);
i += route[index].0;
j += route[index].1;
p = self.pos((i, j));
self.maze[p].push_in('d', false);
self.his.push(route[index]);
} else {
if self.his.len() == 0 {
println!("Out of bounds");
return;
} else {
//println!("STEP TO BAKE");
i = self.his[self.his.len() - 1].0;
j = self.his[self.his.len() - 1].1;
self.his.pop();
}
}
}
if self.his.len() == 0 {
return;
}
//println!();
}
}
pub fn who_in(&self, p: (usize, usize)) -> &Status {
&self.maze[self.pos((p.0 as i32, p.1 as i32))]
}
}