Решение на Wordle от Берна Ахад
Резултати
- 19 точки от тестове
- 0 бонус точки
- 19 точки общо
- 14 успешни тест(а)
- 1 неуспешни тест(а)
Код
use std::fmt;
#[derive(Debug)]
pub enum GameStatus {
InProgress,
Won,
Lost,
}
#[derive(Debug)]
pub enum GameError {
NotInAlphabet(char),
WrongLength { expected: usize, actual: usize },
GameIsOver(GameStatus),
}
#[derive(Debug)]
pub struct Game {
pub status: GameStatus,
pub attempts: u8,
pub alphabet: String,
pub word: String,
pub history: Vec<Word>,
}
#[derive(Clone, Debug)]
pub enum LetterState {
FullMatch,
PartialMatch,
NoMatch,
}
#[derive(Debug)]
pub struct Word {
pub letters: Vec<char>,
pub letters_states: Vec<LetterState>,
}
impl Game {
pub fn new(alphabet: &str, word: &str) -> Result<Self, GameError> {
for c in word.chars() {
if !alphabet.contains(c) {
return Err(GameError::NotInAlphabet(c));
}
}
Ok(Self {
status: GameStatus::InProgress,
attempts: 0,
alphabet: alphabet.to_string(),
word: word.to_string(),
history: vec![],
})
}
pub fn guess_word(&mut self, guess: &str) -> Result<Word, GameError> {
if matches!(self.status, GameStatus::InProgress) {
if guess.chars().count() != self.word.chars().count() {
return Err(GameError::WrongLength {
expected: self.word.chars().count(),
actual: guess.chars().count(),
});
}
for c in guess.chars() {
if !self.alphabet.contains(c) {
return Err(GameError::NotInAlphabet(c));
}
}
let mut word_result: Word = Word {
letters: vec![],
letters_states: vec![],
};
for (i, c) in guess.chars().enumerate() {
if self.word.contains(c) {
for (j, k) in self.word.chars().enumerate() {
if k == c {
if i == j {
word_result.letters.push(c);
word_result.letters_states.push(LetterState::FullMatch)
} else {
word_result.letters.push(c);
word_result.letters_states.push(LetterState::PartialMatch)
}
break;
}
}
} else {
word_result.letters.push(c);
word_result.letters_states.push(LetterState::NoMatch)
}
}
self.history.push(Word {
letters: word_result.letters.clone(),
letters_states: word_result.letters_states.clone(),
});
self.attempts += 1;
if self.word.contains(guess) {
self.status = GameStatus::Won;
} else if self.attempts >= 5 {
self.status = GameStatus::Lost;
}
return Ok(word_result);
} else if matches!(self.status, GameStatus::Lost) {
return Err(GameError::GameIsOver(GameStatus::Lost));
} else {
return Err(GameError::GameIsOver(GameStatus::Won));
}
}
}
impl fmt::Display for Word {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in 0..self.letters.len() {
if matches!(self.letters_states[i], LetterState::FullMatch) {
write!(f, "[{}]", self.letters[i].to_uppercase())?;
} else if matches!(self.letters_states[i], LetterState::PartialMatch) {
write!(f, "({})", self.letters[i].to_uppercase())?;
} else {
write!(f, ">{}<", self.letters[i].to_uppercase())?;
}
}
Ok(())
}
}
impl fmt::Display for Game {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for _i in self.word.chars().enumerate() {
write!(f, "|_|")?;
}
for word in &self.history {
write!(f, "\n")?;
write!(f, "{}", word)?;
}
Ok(())
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20230111-3772066-17wyq3c/solution) Finished test [unoptimized + debuginfo] target(s) in 0.80s Running tests/solution_test.rs (target/debug/deps/solution_test-0edbea2040daef01) running 15 tests test solution_test::test_game_display ... ok test solution_test::test_game_display_cyrillic ... ok test solution_test::test_game_display_german ... ok test solution_test::test_game_state_1 ... ok test solution_test::test_game_state_2 ... ok test solution_test::test_word_display ... ok test solution_test::test_game_state_3 ... ok test solution_test::test_word_display_bulgarian ... ok test solution_test::test_word_display_german ... ok test solution_test::test_word_not_in_alphabet_on_construction ... ok test solution_test::test_word_display_with_repetitions ... FAILED test solution_test::test_word_not_in_alphabet_on_construction_cyrrilic ... ok test solution_test::test_word_not_in_alphabet_on_guess ... ok test solution_test::test_word_not_in_alphabet_on_guess_cyrillic ... ok test solution_test::test_wrong_length ... ok failures: ---- solution_test::test_word_display_with_repetitions stdout ---- thread 'solution_test::test_word_display_with_repetitions' panicked at 'assertion failed: `(left == right)` left: `"(P)[O](O)(B)"`, right: `"(P)[O][O](B)"`', tests/solution_test.rs:68:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: solution_test::test_word_display_with_repetitions test result: FAILED. 14 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`