From 4a6d166a5a6e389c560082b2d4226ac73bc0e218 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Wed, 16 Apr 2025 00:45:21 -0500 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 8 ++++ src/main.rs | 84 +++++++++++++++++++++++++++++++++ 4 files changed, 226 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..84c5473 --- /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.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 = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.172" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" + +[[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.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[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 = "syn" +version = "2.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "zerocopy" +version = "0.8.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2586fea28e186957ef732a5f8b3be2da217d65c5969d4b1e17f973ebbe876879" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a996a8f63c5c4448cd959ac1bab0aaa3306ccfd060472f85943ee0750f0169be" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..796af3c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[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..8f56607 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,84 @@ +use std::io; +use std::io::Write; +use std::cmp::Ordering; +use rand::Rng; + +// Floor and ceiling for guessing range +const MIN: u8 = 1; +const MAX: u8 = 100; + +// Colours for use in print (all bold by default) +const RED: &str = "\x1b[1;31m"; +const GRN: &str = "\x1b[1;32m"; +const YLW: &str = "\x1b[1;33m"; +// Bold +const BLD: &str = "\x1b[1m"; +// Reset +const RST: &str = "\x1b[0m"; + +fn wr_prompt(gcount: u8) { + print!("GUESS {}> ", gcount); + io::stdout().flush().expect("Flush failed."); +} + +fn main() { + println!("\n{BLD}Guess the number!{RST}\n"); + + // Guess counter + let mut gcount: u8 = 0; + + let secret_number: u8 = rand::thread_rng().gen_range(MIN..=MAX); + println!("Please type in your guess ({}-{}):", MIN, MAX); + + loop { + wr_prompt(gcount); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + match guess.trim().to_lowercase().as_str() { + "exit" | "quit" | "q" => { + println!("Exiting!"); + std::process::exit(0); + }, + "1337" => { + println!("The answer is {BLD}{}{RST}", secret_number.to_string()); + continue; + }, + _ => {}, + } + + let guess: u8 = match guess.trim().parse() { + Ok(num) if num >= MIN && num <= MAX => { + if gcount < 255 { + gcount += 1; + } else { + println!("{BLD}GAME OVER!\n{RED}You've run out of guesses!{RST}"); + std::process::exit(13); + } + num + }, + _ => { + println!("Invalid input! Guess a number between {BLD}{}{RST} and {BLD}{}{RST}.", MIN, MAX); + continue; + }, + }; + + //println!("You guessed: {}", guess); + if guess == 69 { + println!("{BLD}Nice!{RST}"); + } + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too {YLW}small{RST}!"), + Ordering::Greater => println!("Too {RED}big{RST}!"), + Ordering::Equal => { + println!("{GRN}You've won!{RST}\nYou used {BLD}{}{RST} guesses!", gcount); + break; + } + } + } +}