Last active
December 8, 2017 16:08
-
-
Save AndrewRadev/f53364f526ada6c8962ee50c49ee3f55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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