Skip to content

Instantly share code, notes, and snippets.

@grymmjack
Created November 30, 2023 16:09
Show Gist options
  • Save grymmjack/231c335f3861172fd8f18b107d679515 to your computer and use it in GitHub Desktop.
Save grymmjack/231c335f3861172fd8f18b107d679515 to your computer and use it in GitHub Desktop.
guessing-game-rick.rs
/*
Guess the Number
----------------
From the Rust Programming Language Book
with modifications by Rick Christy ([email protected])
*/
use rand::Rng;
use std::env;
use std::cmp::Ordering;
use std::io::{self, Write};
use std::process::Command;
const CLR:&str = "\x1b[2J";
// const C00:&str = "\x1b[0;30m";
// const C01:&str = "\x1b[0;31m";
// const C02:&str = "\x1b[0;32m";
// const C03:&str = "\x1b[0;33m";
const C04:&str = "\x1b[0;34m";
// const C05:&str = "\x1b[0;35m";
// const C06:&str = "\x1b[0;36m";
const C07:&str = "\x1b[0;37m";
// const C08:&str = "\x1b[1;30m";
const C09:&str = "\x1b[1;31m";
// const C10:&str = "\x1b[1;32m";
const C11:&str = "\x1b[1;33m";
const C12:&str = "\x1b[1;34m";
const C13:&str = "\x1b[1;35m";
const C14:&str = "\x1b[1;36m";
const C15:&str = "\x1b[1;37m";
fn main() {
let args: Vec<String> = env::args().collect();
let mut total_chances:u32 = 5;
if args.len() > 1 { // args 1 = command itself so len test > 1 = any args
if args[1] == "--help" || args[1] == "-h" {
println!("Syntax: {} <n>\nWhere n is number of chances to guess.\n", args[0]);
return // this does work - since fn main is just a function return to end the program.
}
total_chances = match args[1].parse() {
Ok(num) => num,
Err(_) => 5
};
}
let mut chances: u32 = total_chances;
let secret_number = rand::thread_rng().gen_range(1..=100);
banner();
println!("I'm thinking of a number between {C14}1 and 100{C07}! Guess the number!");
println!("You have {C13}{chances}{C07} chances to guess! Good luck!");
loop {
println!(
"\nPlease input your guess, or type {C11}\"quit\"{C07} to end the game."
);
print!("{C09}>> {C14}");
io::stdout().flush().unwrap();
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
println!("{C07}");
match guess.trim() {
"quit" => {
println!("You gave up!");
println!(
"You had {C13}{chances}{C07} {} left!",
if chances > 1 { "chances" } else { "chance" }
);
println!("\nGoodbye, hope you had fun!\n");
break;
}
"show" => println!("\nThe secret number is: {C15}{secret_number}{C07}"),
&_ => {}
};
let guess: i32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
if guess <= 0 || guess > 100 {
println!("\n{C13}Enter a number between 1 and 100!{C07}n");
continue;
}
print!("You guessed {C15}{guess}{C07}");
chances -= 1;
match guess.cmp(&secret_number) {
Ordering::Less => println!(" but that is {C15}too small{C07}!"),
Ordering::Greater => println!(" but that is {C15}too big{C07}!"),
Ordering::Equal => {
println!("! That was it! You {C13}win{C07}!!!");
println!(
"You guessed the number in {C12}{}{C07} {}!",
total_chances - chances,
if total_chances - chances > 1 {
"tries"
} else {
"try"
}
);
println!("Thanks for playing!\n");
break;
}
}
if chances == 0 {
println!("\n{C04}Sorry! You didn't guess the number...");
println!("You have run out of chances!");
println!("The number was: {C15}{secret_number}{C07}!\n");
break;
} else {
println!(
"You have {C13}{chances}{C07} {} left.",
if chances > 1 { "chances" } else { "chance" }
);
}
}
}
// Print the banner for the game using figlet if available
fn banner() {
screen_reset();
match Command::new("figlet").output() {
Ok(_) => {
let output = Command::new("figlet")
.arg("-f")
.arg("smslant")
.arg("Guess the Number!")
.output()
.expect("failed to execute process");
io::stdout().write_all(&output.stdout).unwrap();
}
Err(_) => {
println!("GUESS THE NUMBER!");
}
}
io::stdout().flush().unwrap();
print!("{C07}");
}
// Reset screen using ANSI codes
fn screen_reset() {
print!("{CLR}"); //clear screen
print!("{C15}"); //bright white
io::stdout().flush().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment