From 915376168a3b0d8d111258525e5303c42715db0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C5=9Eim=C5=9Fek?= Date: Tue, 7 May 2024 10:33:39 +0300 Subject: [PATCH] Adding maze-1d --- Cargo.toml | 2 +- examples/maze-1d-with-cargo-toml/Cargo.toml | 8 +++ examples/maze-1d-with-cargo-toml/main.rs | 8 +++ examples/maze_1d.rs | 9 +++ src/lib.rs | 1 + src/maze_1d.rs | 73 +++++++++++++++++++++ 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 examples/maze-1d-with-cargo-toml/Cargo.toml create mode 100644 examples/maze-1d-with-cargo-toml/main.rs create mode 100644 examples/maze_1d.rs create mode 100644 src/maze_1d.rs diff --git a/Cargo.toml b/Cargo.toml index 915ade5..59fa3f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rust-book-minigrep" +name = "my_rust_lib" version = "0.1.0" authors = ["Gokhan Simsek "] edition = "2021" diff --git a/examples/maze-1d-with-cargo-toml/Cargo.toml b/examples/maze-1d-with-cargo-toml/Cargo.toml new file mode 100644 index 0000000..dff8ac5 --- /dev/null +++ b/examples/maze-1d-with-cargo-toml/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "maze-1d" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +my_rust_lib = { path = "../../my_rust_lib" } \ No newline at end of file diff --git a/examples/maze-1d-with-cargo-toml/main.rs b/examples/maze-1d-with-cargo-toml/main.rs new file mode 100644 index 0000000..2e304ec --- /dev/null +++ b/examples/maze-1d-with-cargo-toml/main.rs @@ -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); +} \ No newline at end of file diff --git a/examples/maze_1d.rs b/examples/maze_1d.rs new file mode 100644 index 0000000..b6947ff --- /dev/null +++ b/examples/maze_1d.rs @@ -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); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index c296440..448d5fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/maze_1d.rs b/src/maze_1d.rs new file mode 100644 index 0000000..4ab503b --- /dev/null +++ b/src/maze_1d.rs @@ -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 = vec![]; + let mut steps: Vec = 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, + all_steps: &mut Vec +) { + // 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); + } + } + } + }; +}