Решение на Wordle от Даниел Величков
Към профила на Даниел Величков
Резултати
- 13 точки от тестове
- 0 бонус точки
- 13 точки общо
- 10 успешни тест(а)
- 5 неуспешни тест(а)
Код
use std::{fmt, ops::Index};
#[derive(Debug, PartialEq)]
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 winning_word: String,
pub attempt_array: Vec <Word>
}
#[derive(Debug)]
pub struct Word {
pub letters: String,
pub matchings: Vec <u8>
}
impl Game {
pub fn new(alphabet: &str, word: &str) -> Result<Self, GameError> {
let mut symbol:char = '.';
let mut flag = false;
for i in word.chars() {
if !alphabet.contains(i) {
symbol = i;
flag = true;
break;
}
}
if flag {
return Err(GameError::NotInAlphabet(symbol))
}
let game = Game {
status: GameStatus::InProgress,
attempts: 0,
alphabet: alphabet.to_string(),
winning_word: word.to_string(),
attempt_array: Vec::new()
};
return Ok(game);
}
pub fn guess_word(&mut self, guess: &str) -> Result<Word, GameError> {
if self.status == GameStatus::Won {
return Err(GameError::GameIsOver(GameStatus::Won));
}
if self.status == GameStatus::Lost {
return Err(GameError::GameIsOver(GameStatus::Lost));
}
self.attempts += 1;
let guess_str = guess.to_string();
let exp = guess_str.len();
let act = self.winning_word.len();
if exp != act {
return Err(GameError::WrongLength { expected: (exp), actual: (act) })
}
let mut symbol = '.';
let mut flag = false;
for i in guess.chars() {
if !self.alphabet.contains(i) {
symbol = i;
flag = true;
break;
}
}
let mut word = Word {
letters: guess_str,
matchings: Vec::new()
};
if flag {
return Err(GameError::NotInAlphabet(symbol));
}
// start the actual guessing
if guess.to_string() == self.winning_word {
self.status = GameStatus::Won;
}
if self.attempts >= 5 {
self.status = GameStatus::Lost;
}
let mut win_word_vec = Vec::new();
let mut guess_word_vec = Vec::new();
for i in self.winning_word.as_str().chars() {
win_word_vec.push(i);
}
for i in guess.chars() {
guess_word_vec.push(i);
}
let mut word_to_save_as_attempt = Word {
letters: guess.to_string(),
matchings: Vec::new()
};
let mut i = 0;
while i < guess_word_vec.len() {
if win_word_vec[i] == guess_word_vec[i] {
word.matchings.push(2);
} else if self.winning_word.contains(guess_word_vec[i]) {
word.matchings.push(1);
} else {
word.matchings.push(0);
}
word_to_save_as_attempt.matchings.push(word.matchings[i]);
i = i + 1;
}
self.attempt_array.push(word_to_save_as_attempt);
return Ok(word);
}
}
impl fmt::Display for Word {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut index = 0;
for c in self.letters.as_str().chars() {
if self.matchings[index] == 2 {
let mut str: String = String::new();
for i in c.to_uppercase() {
str = str + &i.to_string();
}
write!(f, "[{}]", str);
} else if self.matchings[index] == 1 {
let mut str: String = String::new();
for i in c.to_uppercase() {
str = str + &i.to_string();
}
write!(f, "({})", str);
} else {
let mut str: String = String::new();
for i in c.to_uppercase() {
str = str + &i.to_string();
}
write!(f, ">{}<", str);
}
index = index + 1;
}
Ok(())
}
}
impl fmt::Display for Game {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut len = self.winning_word.len();
let mut i = 0;
while i < len {
write!(f, "|_|");
i += 1;
}
i = 0;
while i < self.attempt_array.len() {
write!(f,"\n{}", self.attempt_array[i]);
i = i + 1;
}
Ok(())
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20230111-3772066-zdfbpz/solution) warning: unused import: `ops::Index` --> src/lib.rs:1:16 | 1 | use std::{fmt, ops::Index}; | ^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: variable does not need to be mutable --> src/lib.rs:174:13 | 174 | let mut len = self.winning_word.len(); | ----^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: unused `Result` that must be used --> src/lib.rs:152:17 | 152 | write!(f, "[{}]", str); | ^^^^^^^^^^^^^^^^^^^^^^ | = 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:158:17 | 158 | write!(f, "({})", str); | ^^^^^^^^^^^^^^^^^^^^^^ | = note: this `Result` may be an `Err` variant, which should be handled = 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:164:17 | 164 | write!(f, ">{}<", str); | ^^^^^^^^^^^^^^^^^^^^^^ | = note: this `Result` may be an `Err` variant, which should be handled = 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:177:13 | 177 | write!(f, "|_|"); | ^^^^^^^^^^^^^^^^ | = note: this `Result` may be an `Err` variant, which should be handled = 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:183:13 | 183 | write!(f,"\n{}", self.attempt_array[i]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this `Result` may be an `Err` variant, which should be handled = note: this warning originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info) warning: `solution` (lib) generated 7 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 ... ok test solution_test::test_game_display_cyrillic ... FAILED test solution_test::test_game_display_german ... FAILED test solution_test::test_game_state_1 ... ok test solution_test::test_game_state_2 ... ok test solution_test::test_game_state_3 ... ok test solution_test::test_word_display ... ok test solution_test::test_word_display_bulgarian ... ok 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_not_in_alphabet_on_guess ... ok test solution_test::test_word_display_with_repetitions ... ok test solution_test::test_word_not_in_alphabet_on_guess_cyrillic ... FAILED test solution_test::test_wrong_length ... FAILED failures: ---- solution_test::test_game_display_cyrillic stdout ---- thread 'solution_test::test_game_display_cyrillic' panicked at 'assertion failed: `(left == right)` left: `"|_||_||_||_||_||_|"`, right: `"|_||_||_|"`', tests/solution_test.rs:119:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::test_game_display_german stdout ---- thread 'solution_test::test_game_display_german' panicked at 'assertion failed: `(left == right)` left: `"|_||_||_||_||_|"`, right: `"|_||_||_|"`', tests/solution_test.rs:133:5 ---- solution_test::test_word_display_german stdout ---- thread 'solution_test::test_word_display_german' panicked at 'assertion failed: `(left == right)` left: `"|_||_||_||_||_|"`, right: `"|_||_||_|"`', tests/solution_test.rs:87:5 ---- solution_test::test_word_not_in_alphabet_on_guess_cyrillic stdout ---- thread 'solution_test::test_word_not_in_alphabet_on_guess_cyrillic' panicked at 'Expression Err(WrongLength { expected: 8, actual: 4 }) does not match the pattern "Err(GameError::NotInAlphabet('х'))"', tests/solution_test.rs:46:5 ---- solution_test::test_wrong_length stdout ---- thread 'solution_test::test_wrong_length' panicked at 'Expression Err(WrongLength { expected: 7, actual: 4 }) does not match the pattern "Err(GameError::WrongLength { expected: 4, actual: 7 })"', tests/solution_test.rs:96:5 failures: solution_test::test_game_display_cyrillic solution_test::test_game_display_german solution_test::test_word_display_german solution_test::test_word_not_in_alphabet_on_guess_cyrillic solution_test::test_wrong_length test result: FAILED. 10 passed; 5 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`