QAQOFRFCZFXEIQZLARMADFRDBOLLB7JDSMH7KT66DVRNAZPKCQUAC
InvalidLetter
InvalidLetter,
AlreadyGuessed,
GameOver,
}
impl From<HangmanError> for JsValue {
fn from(he: HangmanError) -> Self {
JsValue::from(match he {
HangmanError::InvalidLetter => "Invalid Letter!",
HangmanError::AlreadyGuessed => "Already Guessed!",
HangmanError::GameOver => "Game is over!",
})
}
}
struct WordList {}
impl WordList {
pub fn select_random_word() -> Word {
Word(
"EGGPLANT"
.chars()
.map(|c| {
let letter =
InternalLetter::try_from(c).expect("WordList always uses valid chars");
WordLetter {
letter,
state: LetterGuessedState::Unguessed,
}
})
.collect(),
)
}
}
#[wasm_bindgen]
pub struct Word(Vec<WordLetter>);
impl Deref for Word {
type Target = Vec<WordLetter>;
fn deref(&self) -> &Self::Target {
&self.0
}
impl DerefMut for Word {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Word {
pub fn guess(&mut self, guess: &InternalLetter) -> GuessResult {
let mut guess_result: GuessResult = GuessResult::Incorrect;
self.iter_mut().for_each(|f| {
if f.letter == *guess {
f.state = LetterGuessedState::CorrectGuess;
guess_result = GuessResult::Correct;
}
});
guess_result
}
pub fn completely_guessed(&self) -> bool {
self.0.iter().all(WordLetter::guessed)
}
}
}
}
#[wasm_bindgen]
pub fn guess_letter(&mut self, letter: char) -> Result<GameState, HangmanError> {
let guess = InternalLetter::try_from(letter).or(Err(HangmanError::InvalidLetter))?;
if self.guesses.get(&guess).is_some() {
return Err(HangmanError::AlreadyGuessed);
} else if self.state() != GameState::InProgress {
return Err(HangmanError::GameOver);
};
let result = self.current_word.guess(&guess);
match result {
GuessResult::Incorrect => {
self.guesses_left -= 1;
}
GuessResult::Correct => {}
WordLetter {letter, state: LetterGuessedState::CorrectGuess } => (u8::from(*letter)),
WordLetter {state: LetterGuessedState::Unguessed, .. } => b"_"[0],
};
WordLetter {
letter,
state: LetterGuessedState::CorrectGuess,
} => (u8::from(*letter)),
WordLetter {
state: LetterGuessedState::Unguessed,
..
} => b"_"[0],
};
.filter_map(|g| {
if let GuessLetter{ letter, state: GuessResult::Correct } = g {
let s : String = (*letter).into();
let j : JsValue = (*s).into();
.filter_map(|(letter, guess)| {
if let GuessResult::Correct = guess {
let s: String = (*letter).into();
let j: JsValue = (*s).into();
.filter_map(|g| {
if let GuessLetter{ letter, state: GuessResult::Incorrect } = g {
let s : String = (*letter).into();
let j : JsValue = (*s).into();
.filter_map(|(letter, guess)| {
if let GuessResult::Incorrect = guess {
let s: String = (*letter).into();
let j: JsValue = (*s).into();
pkg