From 21d3a297ec9af26b92c77c6c88e1d3ac75c6cf89 Mon Sep 17 00:00:00 2001 From: rilwanubala Date: Tue, 1 Sep 2026 22:51:40 +0100 Subject: [PATCH] Add guessing_game project --- .gitignore | 1 + Cargo.lock | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 +++ src/main.rs | 73 ++++++++++++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs 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..53e05bb --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,133 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.189" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +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 = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "zerocopy" +version = "0.8.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "556764e583adb45a9f8d413c2a147fa7e8d821e48e12b14fd560b607998b75eb" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ab42fc20575779bd240faa45f94a74256f755c0fa9e89f0ede20d91d0cdfc1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9f9c4ac --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2024" + +[dependencies] +rand = "0.8.5" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..47b5752 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,73 @@ +use std::cmp::Ordering; +use std::io; + +use rand::Rng; + +fn main() { + println!("I have chosen a number between 1 and 100."); + println!("You have 7 attempts to guess it."); +// Generate a random number between 1 and 100 + let secret_number = rand::thread_rng().gen_range(1..=100); + + //create a loop that allows the user to guess up to 7 times + for attempt in 1..=7 { + println!(); + println!("Attempt {attempt} of 7"); + println!("Guess a number:"); + + //create a mutable variable to store the user's guess + + let mut guess = String::new(); + + //read the user's guess from standard input + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + //convert the user's guess to a number and handle any errors that may occur + + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => { + println!("Invalid guess! Please guess a number."); + continue; + } + }; + + //print the user's guess to the console + + println!("You guessed: {guess}"); + + //compare the user's guess to the secret number and print a message indicating whether the guess was too low, too high, or correct + + match guess.cmp(&secret_number) { + Ordering::Less => { + println!("Too small!"); + } + + // Handle the case where the guess is greater than the secret number + + Ordering::Greater => { + println!("Too big!"); + } + + // Handle the case where the guess is equal to the secret number + + Ordering::Equal => { + println!(); + println!("Congratulations!"); + println!("You guessed the correct number!"); + println!("You guessed the number in {attempt} attempt(s)."); + return; + } + } + } + + // If the user has used all 7 attempts, print a message indicating that the game is over and reveal the secret number + + println!(); + println!("Your guessing is over!"); + println!("You used all 7 attempts."); + println!("The secret number was: {secret_number}"); +}