Skip to content

Commit

Permalink
Adding maze-1d
Browse files Browse the repository at this point in the history
  • Loading branch information
simsekgokhan committed May 7, 2024
1 parent bea5715 commit 9153761
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "rust-book-minigrep"
name = "my_rust_lib"
version = "0.1.0"
authors = ["Gokhan Simsek <[email protected]>"]
edition = "2021"
Expand Down
8 changes: 8 additions & 0 deletions examples/maze-1d-with-cargo-toml/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "maze-1d"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
my_rust_lib = { path = "../../my_rust_lib" }
8 changes: 8 additions & 0 deletions examples/maze-1d-with-cargo-toml/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

use my_rust_lib::maze_1d::*;

fn main() {
let start_pos = 2;
let start_dir = Dir::L;
goto(&start_dir, start_pos, &mut steps, &mut all_steps);
}
9 changes: 9 additions & 0 deletions examples/maze_1d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#[path = "../src/maze_1d.rs"] mod maze_1d;
use maze_1d::*;

fn main() {
let start_pos = 2;
let start_dir = Dir::L;
start_game(&start_dir, start_pos);
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#[cfg(test)]
mod integration_tests;

pub mod maze_1d; // pub because we this from examples
mod async_;
mod averaged_collection;
mod cfg;
Expand Down
73 changes: 73 additions & 0 deletions src/maze_1d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

#[derive(Clone, Debug, PartialEq)]
pub enum Dir { L, R } // Left, Right

// 1D world
// 1 is open, 0 is closed box
const W: [usize; 5] = [1, 0, 0, 1, 1];

pub fn start_game(
dir: &Dir,
pos: usize,
) {
print_info(dir, pos);

let mut all_steps: Vec<Dir> = vec![];
let mut steps: Vec<Dir> = vec![];
goto(dir, pos, &mut steps, &mut all_steps);

println!("\n##### Game ends: You win! #####");
println!("steps: {:?}", steps);
println!("all_steps: {:?}\n", all_steps);
}

pub fn print_info(start_dir: &Dir, start_pos: usize) {
println!("");
println!("--INFO-------------------------");
println!("World: {:?}", W);
println!("Start pos: {start_pos}");
println!("Start dir: {:?}", start_dir);
println!("starting game...");
println!("-------------------------------");
}

pub fn goto(
dir: &Dir,
pos: usize,
steps: &mut Vec<Dir>,
all_steps: &mut Vec<Dir>
) {
// println!("-- going from {pos}");
if pos == 0 { return }
if pos == W.len()-1 { return }
match dir {
Dir::L => {
all_steps.push(dir.clone());
if W[pos-1] == 1 { // Open cell
// println!("L");
steps.push(dir.clone());
goto(dir, pos-1, steps, all_steps);
} else {
if W[pos+1] == 0 { // Closed cell
panic!("All ways are closed. pos: {pos}");
} else {
goto(&Dir::R, pos, steps, all_steps);
}
}
}
Dir::R => {
all_steps.push(dir.clone());
if W[pos+1] == 1 {
// println!("R");
steps.push(dir.clone());
goto(dir, pos+1, steps, all_steps)
} else {
if W[pos-1] == 0 {
panic!("All ways are closed. pos: {pos}");
} else {
goto(&Dir::L, pos, steps, all_steps);
}
}
}
};
}

0 comments on commit 9153761

Please sign in to comment.