Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.