Skip to content

Instantly share code, notes, and snippets.

@coyotte508
Created October 10, 2020 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coyotte508/f6ab07e32fe7bc558d72171e5235ef83 to your computer and use it in GitHub Desktop.
Save coyotte508/f6ab07e32fe7bc558d72171e5235ef83 to your computer and use it in GitHub Desktop.
use std::io::{stdin, stdout, Write};
fn main() {
let stdin = stdin();
let mut stdout = stdout();
let to_guess = String::from("BENEDICT");
let mut letters: u128 = 0;
loop {
let a = to_guess.chars().map(|x| if x <= 127 as char && letters & (1 << x as u8) > 0 {x} else {'_'}).collect::<String>();
let a = a.chars().map(|x| x.to_string() + &' '.to_string()).collect::<Vec<String>>().join(" ");
let a = a.trim_end();
println!("Current word: {}", a);
if !a.contains('_') {
println!("Congratulations!");
break;
}
print!("Enter new letter: ");
stdout.flush().unwrap();
let mut letter = String::new();
stdin.read_line(& mut letter).expect("Invalid letter");
if letter.trim().len() != 1 {
println!("Use only one letter");
continue;
}
let letter = letter.to_uppercase().chars().next().unwrap();
letters = letters | (1 << letter as u8)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment