From 7ab91a0e9b23214a8fda4c4745610d8543d78da6 Mon Sep 17 00:00:00 2001 From: mur4ik18 Date: Mon, 14 Feb 2022 19:22:51 +0200 Subject: [PATCH 1/3] :rocket: 41_guess add rust --- 41_Guess/rust/Cargo.toml | 9 ++++ 41_Guess/rust/README.md | 3 ++ 41_Guess/rust/src/main.rs | 86 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 41_Guess/rust/Cargo.toml create mode 100644 41_Guess/rust/README.md create mode 100644 41_Guess/rust/src/main.rs diff --git a/41_Guess/rust/Cargo.toml b/41_Guess/rust/Cargo.toml new file mode 100644 index 00000000..66ea57a5 --- /dev/null +++ b/41_Guess/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guess" +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.4" \ No newline at end of file diff --git a/41_Guess/rust/README.md b/41_Guess/rust/README.md new file mode 100644 index 00000000..a2d10996 --- /dev/null +++ b/41_Guess/rust/README.md @@ -0,0 +1,3 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) + +Conversion to [Python](https://www.rust-lang.org/) diff --git a/41_Guess/rust/src/main.rs b/41_Guess/rust/src/main.rs new file mode 100644 index 00000000..442e348b --- /dev/null +++ b/41_Guess/rust/src/main.rs @@ -0,0 +1,86 @@ +use rand::Rng; +use std::io; +use std::cmp::Ordering; +// Rust haven't log2 in the standard library so I added fn log_2 +const fn num_bits() -> usize { std::mem::size_of::() * 8 } + +fn main() { + + let mut rng = rand::thread_rng(); + let mut still_guessing = true; + let limit = set_limit(); + let limit_goal = 1+(log_2(limit.try_into().unwrap())/log_2(2)) ; + loop{ + + let mut won = false; + let mut guess_count = 1; + let my_guess = rng.gen_range(1..limit); + + println!("I'm thinking of a number between 1 and {}",limit); + println!("Now you try to guess what it is."); + + while still_guessing { + let inp = get_input() + .trim() + .parse::().unwrap(); + println!("\n\n\n"); + if inp < my_guess { + println!("Too low. Try a bigger answer"); + guess_count+=1; + } + else if inp > my_guess { + println!("Too high. Try a smaller answer"); + guess_count+=1; + } + else { + println!("That's it! You got it in {} tries", guess_count); + won = true; + still_guessing = false; + } + } + if won { + match guess_count.cmp(&limit_goal) { + Ordering::Less => println!("Very good."), + Ordering::Equal => println!("Good."), + Ordering::Greater => println!("You should have been able to get it in only {}", limit_goal), + } + + println!("\n\n\n"); + still_guessing = true; + } else { + println!("\n\n\n"); + } + } +} + +fn log_2(x:i32) -> u32 { + assert!(x > 0); + num_bits::() as u32 - x.leading_zeros() - 1 +} + +fn set_limit() -> i64 { + + println!(" Guess"); + println!("\n\n\n"); + println!("This is a number guessing game. I'll think"); + println!("of a number between 1 and any limit you want.\n"); + println!("Then you have to guess what it is\n"); + println!("What limit do you want?"); + + let inp = get_input().trim().parse::().unwrap(); + + if inp >= 2 { + inp + } + else { + set_limit() + } +} + +fn get_input() -> String { + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Your input is not correct"); + input +} From 971e75d066a70fe610eb349ccb99652bba54827d Mon Sep 17 00:00:00 2001 From: Alex Kotov <79057640+mur4ik18@users.noreply.github.com> Date: Mon, 14 Feb 2022 19:24:34 +0200 Subject: [PATCH 2/3] Update README.md --- 41_Guess/rust/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/41_Guess/rust/README.md b/41_Guess/rust/README.md index a2d10996..fc6468b9 100644 --- a/41_Guess/rust/README.md +++ b/41_Guess/rust/README.md @@ -1,3 +1,3 @@ Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) -Conversion to [Python](https://www.rust-lang.org/) +Conversion to [Rust](https://www.rust-lang.org/) From b79200166eb927898efdf5723dd6205d218e8807 Mon Sep 17 00:00:00 2001 From: Alex Kotov <79057640+mur4ik18@users.noreply.github.com> Date: Mon, 14 Feb 2022 19:27:44 +0200 Subject: [PATCH 3/3] Update main.rs --- 41_Guess/rust/src/main.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/41_Guess/rust/src/main.rs b/41_Guess/rust/src/main.rs index 442e348b..da175038 100644 --- a/41_Guess/rust/src/main.rs +++ b/41_Guess/rust/src/main.rs @@ -1,3 +1,26 @@ +//####################################################### +// +// Guess +// +// From: Basic Computer Games (1978) +// +// "In program Guess, the computer chooses a random +// integer between 0 and any limit and any limit you +// set. You must then try to guess the number the +// computer has choosen using the clues provideed by +// the computer. +// You should be able to guess the number in one less +// than the number of digits needed to represent the +// number in binary notation - i.e. in base 2. This ought +// to give you a clue as to the optimum search technique. +// Guess converted from the original program in FOCAL +// which appeared in the book "Computers in the Classroom" +// by Walt Koetke of Lexington High School, Lexington, +// Massaschusetts. +// +//####################################################### + + use rand::Rng; use std::io; use std::cmp::Ordering;