diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Cargo.lock | 75 | ||||
| -rw-r--r-- | Cargo.toml | 9 | ||||
| -rw-r--r-- | src/main.rs | 75 |
4 files changed, 160 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..2387701 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,75 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "monty-hall-problem" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..1887ff6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "monty-hall-problem" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..dc0d873 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,75 @@ +// Monty Hall Problem +// https://en.wikipedia.org/wiki/Monty_Hall_problem + +// Step 1: Choose a winning door +// Step 2: User chooses a door +// Step 3: Open an incorrect door +// Step 4: Ask for a switch +// Step 5: Reveal the result + +// Step 6: Record the result (win or loss) +// Step 7: Calculate the win rate (win / total * 100) +// Step 8: Loop + +use rand::Rng; + +fn main() { + println!("Monty Hall Problem"); + println!("by Kaorai\n"); + + let mut total_plays: i32 = 0; + let mut wins: i32 = 0; + + loop { + let winning_door: i8 = rand::thread_rng().gen_range(1..4); + + let mut user_choice: i8 = 0; + + while user_choice > 3 || user_choice < 1 { + println!("Choose a door (1, 2, 3) or type 'exit' to quit: "); + + let mut user_input: String = String::new(); + std::io::stdin().read_line(&mut user_input).expect("Failed to read line"); + user_input = user_input.trim().to_string(); + + if user_input == "exit" { + return; + } + + user_choice = user_input.parse().expect("Please enter a number"); + } + + let mut incorrect_door: i8 = user_choice; + if user_choice == winning_door { + while incorrect_door == winning_door || incorrect_door == user_choice { + incorrect_door = rand::thread_rng().gen_range(1..4); + } + } else { + incorrect_door = 6 - user_choice - winning_door; + } + + println!("\nDoor {} is incorrect", incorrect_door); + println!("Would you like to switch doors? (y or n)"); + + let mut user_input: String = String::new(); + std::io::stdin().read_line(&mut user_input).expect("Failed to read line"); + user_input = user_input.trim().to_string(); + + if user_input == "y" { + user_choice = 6 - user_choice - incorrect_door; + } + + println!("\nUser choice: {}", user_choice); + println!("Winning door: {}", winning_door); + if user_choice == winning_door { + println!("You win!"); + wins += 1; + } else { + println!("You lose!"); + } + + total_plays += 1; + + println!("Win rate: {:.2}%\n", (wins as f32 / total_plays as f32) * 100.0); + } +} |
