-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bea5715
commit 9153761
Showing
6 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
}; | ||
} |