Skip to content

Instantly share code, notes, and snippets.

@jeffque
Last active March 8, 2022 13:26
Show Gist options
  • Save jeffque/ea55d5e37d550007b038759c1594fc8e to your computer and use it in GitHub Desktop.
Save jeffque/ea55d5e37d550007b038759c1594fc8e to your computer and use it in GitHub Desktop.
// based on https://github.com/vit0rr/jogo-da-adivinhacao
//extern crate rand;
//use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn read_guess() -> u32 {
return loop {
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Error to read entry");
match guess.trim().parse() {
Ok(num) => break num,
Err(_) => /* continue */break 13,
};
}
}
fn divination_game(attempts: u32, secret_number: u32) {
println!("attempt: {:?}", attempts);
println!("Type your guess!");
let guess: u32 = read_guess();
println!("You type: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Less!"),
Ordering::Greater => println!("Greater!"),
Ordering::Equal => {
println!("Right! You played {} times", attempts);
return;
}
}
if attempts > 3 {
return;
} else {
divination_game(attempts + 1, secret_number);
}
}
fn main() {
println!("guess the number!");
let secret_number = 42;//rand::thread_rng().gen_range(1, 101);
divination_game(1, secret_number);
}
// based on https://github.com/vit0rr/jogo-da-adivinhacao
extern crate rand;
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn read_guess() -> u32 {
return loop {
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Error to read entry");
match guess.trim().parse() {
Ok(num) => break num,
Err(_) => continue,
};
}
}
fn divination_game(attempts: u32, secret_number: u32) {
println!("attempt: {:?}", attempts);
println!("Type your guess!");
let guess: u32 = read_guess();
println!("You type: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Less!"),
Ordering::Greater => println!("Greater!"),
Ordering::Equal => {
println!("Right! You played {} times", attempts);
return;
}
}
divination_game(attempts + 1, secret_number);
}
fn main() {
println!("guess the number!");
let secret_number = 42;//rand::thread_rng().gen_range(1, 101);
divination_game(1, secret_number);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment