Решение на Wordle от Мария Бошикьова

Обратно към всички решения

Към профила на Мария Бошикьова

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 6 успешни тест(а)
  • 9 неуспешни тест(а)

Код

#[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 word: Word,
pub history: Vec<String>,
}
#[derive(Debug)]
pub struct Word {
pub guess: String,
pub the_word: String,
pub alphabet: String,
}
impl Game {
pub fn new(alphabet: &str, word: &str) -> Result<Self, GameError> {
let mut result = String::new();
for c in word.chars(){
if !alphabet.contains(c){
return Err(GameError::NotInAlphabet(c));
}
result.push_str("|_|");
}
Ok(Self{ word: Word {guess: String::from(""),
alphabet: String::from(alphabet),
the_word: String::from(word)},
status: GameStatus::InProgress,
attempts: 0,
history: vec![result],
})
}
pub fn guess_word(&mut self, guess: &str) -> Result<Word, GameError> {
let word: Vec<char> = self.word.the_word.chars().collect();
self.attempts = self.attempts + 1;
if guess.chars().count() != word.len(){
return Err(GameError::WrongLength{expected: word.len(), actual: guess.chars().count()});
}
let mut counter = 0;
let mut guessed_letter = 0;
let mut result = String::new();
for c in guess.chars() {
if !self.word.alphabet.contains(c){
return Err(GameError::NotInAlphabet(c));
}
if c == word[counter]{
result.push_str(&format!("[{}]", c.to_uppercase()));
guessed_letter = guessed_letter + 1;
} else if self.word.the_word.contains(c) {
result.push_str(&format!("({})", c.to_uppercase()));
} else {
result.push_str(&format!(">{}<", c.to_uppercase()));
}
counter = counter + 1;
}
self.word.guess = guess.to_string();
self.history.push(result);
if guessed_letter == counter {
return Err(GameError::GameIsOver(GameStatus::Won));
}
if self.attempts == 5 {
return Err(GameError::GameIsOver(GameStatus::Lost));
}
Ok(Word{guess: String::from(guess), alphabet: self.word.alphabet.clone(), the_word: self.word.the_word.clone() })
}
}
use std::fmt;
impl fmt::Display for Game {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let length = self.history.len();
let mut counter = 0;
for word in self.history.iter() {
counter = counter + 1;
if counter == length {
write!(f, "{}", word);
} else {
writeln!(f, "{}", word);
}
}
write!(f, "")
}
}
impl fmt::Display for Word {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let word: Vec<char> = self.the_word.chars().collect();
let mut result = String::new();
let mut counter = 0;
for c in self.guess.chars() {
if c == word[counter]{
result.push_str(&format!("[{}]", c.to_uppercase()));
} else if self.the_word.contains(c) {
result.push_str(&format!("({})", c.to_uppercase()));
} else {
result.push_str(&format!(">{}<", c.to_uppercase()));
}
counter = counter + 1;
}
writeln!(f, "{}", result)
}
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20230111-3772066-ag72cs/solution)
warning: unused `Result` that must be used
   --> src/lib.rs:103:17
    |
103 |                 write!(f, "{}", word);
    |                 ^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this `Result` may be an `Err` variant, which should be handled
    = note: `#[warn(unused_must_use)]` on by default
    = note: this warning originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: unused `Result` that must be used
   --> src/lib.rs:105:17
    |
105 |                 writeln!(f, "{}", word);
    |                 ^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this `Result` may be an `Err` variant, which should be handled
    = note: this warning originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `solution` (lib) generated 2 warnings
    Finished test [unoptimized + debuginfo] target(s) in 0.79s
     Running tests/solution_test.rs (target/debug/deps/solution_test-0edbea2040daef01)

running 15 tests
test solution_test::test_game_display ... FAILED
test solution_test::test_game_display_cyrillic ... FAILED
test solution_test::test_game_display_german ... ok
test solution_test::test_game_state_1 ... FAILED
test solution_test::test_game_state_2 ... FAILED
test solution_test::test_game_state_3 ... FAILED
test solution_test::test_word_display ... FAILED
test solution_test::test_word_display_bulgarian ... FAILED
test solution_test::test_word_display_german ... FAILED
test solution_test::test_word_not_in_alphabet_on_construction ... ok
test solution_test::test_word_not_in_alphabet_on_construction_cyrrilic ... ok
test solution_test::test_word_display_with_repetitions ... FAILED
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_game_display stdout ----
thread 'solution_test::test_game_display' panicked at 'called `Result::unwrap()` on an `Err` value: GameIsOver(Won)', tests/solution_test.rs:110:29

---- solution_test::test_game_display_cyrillic stdout ----
thread 'solution_test::test_game_display_cyrillic' panicked at 'called `Result::unwrap()` on an `Err` value: GameIsOver(Won)', tests/solution_test.rs:124:28
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::test_game_state_1 stdout ----
thread 'solution_test::test_game_state_1' panicked at 'called `Result::unwrap()` on an `Err` value: GameIsOver(Won)', tests/solution_test.rs:144:28

---- solution_test::test_game_state_2 stdout ----
thread 'solution_test::test_game_state_2' panicked at 'called `Result::unwrap()` on an `Err` value: GameIsOver(Lost)', tests/solution_test.rs:157:32

---- solution_test::test_game_state_3 stdout ----
thread 'solution_test::test_game_state_3' panicked at 'called `Result::unwrap()` on an `Err` value: GameIsOver(Lost)', tests/solution_test.rs:171:32

---- solution_test::test_word_display stdout ----
thread 'solution_test::test_word_display' panicked at 'assertion failed: `(left == right)`
  left: `"(O)(P)>S<\n"`,
 right: `"(O)(P)>S<"`', tests/solution_test.rs:57:5

---- solution_test::test_word_display_bulgarian stdout ----
thread 'solution_test::test_word_display_bulgarian' panicked at 'assertion failed: `(left == right)`
  left: `"(Л)>А<(Л)>Е<\n"`,
 right: `"(Л)>А<(Л)>Е<"`', tests/solution_test.rs:77:5

---- solution_test::test_word_display_german stdout ----
thread 'solution_test::test_word_display_german' panicked at 'assertion failed: `(left == right)`
  left: `">F<[Ü][SS]\n"`,
 right: `">F<[Ü][SS]"`', tests/solution_test.rs:88:5

---- solution_test::test_word_display_with_repetitions stdout ----
thread 'solution_test::test_word_display_with_repetitions' panicked at 'assertion failed: `(left == right)`
  left: `"(O)[O](P)>S<\n"`,
 right: `"(O)[O](P)>S<"`', tests/solution_test.rs:67:5


failures:
    solution_test::test_game_display
    solution_test::test_game_display_cyrillic
    solution_test::test_game_state_1
    solution_test::test_game_state_2
    solution_test::test_game_state_3
    solution_test::test_word_display
    solution_test::test_word_display_bulgarian
    solution_test::test_word_display_german
    solution_test::test_word_display_with_repetitions

test result: FAILED. 6 passed; 9 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test solution_test`

История (1 версия и 0 коментара)

Мария качи първо решение на 24.11.2022 16:57 (преди 6 месеца)