Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Last active December 8, 2017 16:08
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 AndrewRadev/f53364f526ada6c8962ee50c49ee3f55 to your computer and use it in GitHub Desktop.
Save AndrewRadev/f53364f526ada6c8962ee50c49ee3f55 to your computer and use it in GitHub Desktop.
extern crate solution;
use solution::*;
use std::io::{self, Write};
fn main() {
let mut game = Game::new("aardvark", 10).unwrap();
let stdin = io::stdin();
println!("Let's play hangman!");
loop {
println!("{}", game);
if game.is_over() {
break;
}
print!("Enter command: ");
io::stdout().flush().unwrap();
let mut input = String::new();
if let Err(_) = stdin.read_line(&mut input) {
println!("Invalid input!");
continue;
};
let command = match input.parse::<Command>() {
Ok(c) => c,
Err(e) => {
println!("{}", e);
continue;
}
};
match command {
Command::Help => {
println!("");
println!("Valid commands:");
println!(" - help => Show these instructions");
println!(" - info => Information about attempted words/letters");
println!(" - quit => Quit the game");
println!(" - try letter <c> => Try this letter");
println!(" - try word <word> => Try an entire word");
println!("");
},
Command::Info => {
println!("Information about the state of the game:");
println!(" - Attempted letters: {:?}", game.attempted_letters);
println!(" - Attempted words: {:?}", game.attempted_words);
},
Command::Quit => break,
Command::TryLetter(c) => {
match game.guess_letter(c) {
Ok(true) => println!("Good guess!"),
Ok(false) => println!("Sorry, no dice"),
Err(e) => println!("Error: {}", e),
}
},
Command::TryWord(word) => {
match game.guess_word(&word) {
Ok(true) => println!("Good guess!"),
Ok(false) => println!("Sorry, no dice"),
Err(e) => println!("Error: {}", e),
}
},
};
println!("");
}
println!("Good game!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment