hello,
last time I made a fibonacci series generator in Rust and now I have made something different :)
use std::io;
fn main() {
let mut input: String = String::new();
let stdin = io::stdin();
let x = rand::random::<u32>() % 101;
let mut attempts = 0;
loop {
println!("Guess a number from 0 to 100:");
stdin.read_line(&mut input);
input = input.to_string().replace("\n", ""); // removing the \n
let user_input: u32 = input.parse::<u32>().unwrap();
if x == user_input {
println!("You won! attempts: {attempts}");
break;
}
else if x < user_input {
println!("too big");
attempts += 1;
}
else {
println!("too small");
attempts += 1;
}
input.clear()
}
}
feel free to give me suggestion :)
@kionite231
You could save the inputs done by the user and ask if the guesses should be listed after you did enter the correct number.
done :D
use std::io; fn main() { let mut input: String = String::new(); let stdin = io::stdin(); let x = rand::random::<u32>() % 101; let mut attempts = 0; let mut user_inputs: Vec<u32> = Vec::new(); loop { println!("Guess a number from 0 to 100:"); stdin.read_line(&mut input); input = input.to_string().replace("\n", ""); // removing the \n let user_input: u32 = input.parse::<u32>().unwrap(); user_inputs.push(user_input); if x == user_input { println!("You won! attempts: {attempts}"); println!("Your inputs:"); for input in user_inputs { print!("{input} "); } println!(""); break; } else if x < user_input { println!("too big"); attempts += 1; } else { println!("too small"); attempts += 1; } input.clear() } }
@kionite231
You could also color the inputs in different colors
Maybe red if it was wrong and green if the guess was right.
You could also keep that color coding if the list of inputs is getting shown at the end.
but to do that I have to use external deps? which I am not comfortable doing. (I am newbie :) )
Nice work and congrats on your progress! Being new and uncomfortable with dependencies, this project might be a good opportunity to read and apply chapter 7 of the rust book if you haven’t already. Specifically 7.2 defining modules … and the sections that follow.
As your projects increase complexity it’s really useful to shift chunks around (within main.rs and into other files/directories). Doing it now will make it easier to see the individual parts (vs waiting till a project has a lot going on). It might make dependencies feel less unfamiliar.
Coloured text does not require a dep. It is just about printing the right colour codes like:
const BLACK: &'static str = "\u{001b}[30m"; const RED: &'static str = "\u{001b}[31m"; const GREEN: &'static str = "\u{001b}[32m"; const YELLOW: &'static str = "\u{001b}[33m"; const BLUE: &'static str = "\u{001b}[34m"; const MAGENTA: &'static str = "\u{001b}[35m"; const CYAN: &'static str = "\u{001b}[36m"; const WHITE: &'static str = "\u{001b}[37m"; const CLEAR: &'static str = "\u{001b}[0m"; fn main() { println!("{RED}red! {BLUE}blue!{CLEAR}"); }
The libraries just make it easier so you don’t need to remember or know what those codes mean.
I thought, colour codes are platform dependent, will it work on windows
I usually run things on Linux or macOS, but using a library (crate) may add portability, imo
🤔 I think the vt100 protocols (where the escape code come from) predate windows and I think all modern terminals still use those as the base. So I think they are cross platform. From a quick search it looks like they are the same codes on windows. So I dont think the libraries are doing anything special for cross platform support.
I see, so I was wrong then
Maybe I should try colour codes on windows when I get to it 😅 thanks for the info
@kionite231
Okay I do understand that maybe I will get another idea if so I will let you know.