Решение на Wordle от Димитър Тагарев
Към профила на Димитър Тагарев
Резултати
- 11 точки от тестове
- 0 бонус точки
- 11 точки общо
- 8 успешни тест(а)
- 7 неуспешни тест(а)
Код
#[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 answer: String,
pub alphabet: String,
pub word_archive: Vec<String>
}
#[derive(Debug)]
pub struct Word {
pub word: String
}
fn contains_char( word: &str, ch: char) -> bool{
for c in word.chars() {
if c == ch { return true; }
}
false
}
fn count_length(word: &str) -> usize {
let mut counter = 0;
for c in word.chars() {
counter += 1;
}
counter
}
impl Game {
fn return_first_wrong( alphabet: &str, word: &str) -> char {
for c in word.chars() {
if !contains_char(alphabet, c) {
return c
}
}
return '?'
}
fn correct_word( alphabet: &str, word: &str) -> bool {
for c in word.chars() {
if !contains_char(alphabet, c) {
return false
}
}
true
}
fn create_empty_game_string(word: &str) -> String {
let mut str = String::new();
for c in word.chars() {
str.push_str("|_|");
}
str
}
pub fn new(alphabet: &str, word: &str) -> Result<Self, GameError> {
let mut archive = Vec::new();
archive.push(Self::create_empty_game_string(&word));
if Self::correct_word(alphabet, word) {
Ok(
Game { status: GameStatus::InProgress,
attempts: 0,
answer: String::from(word),
alphabet: String::from(alphabet),
word_archive: archive
}
)
}
else {
Err(GameError::NotInAlphabet(Self::return_first_wrong( alphabet, word)))
}
}
fn compare(&mut self, guess: &str ) -> bool {
if self.answer.eq(&String::from(guess)) {
return true;
}
else {
return false;
}
}
pub fn guess_word(&mut self, guess: &str) -> Result<Word, GameError> {
if self.word_archive.is_empty() {
self.word_archive.push(Self::create_empty_game_string(&self.answer));
}
if count_length(&guess) != count_length(&self.answer) {
return Err(GameError::WrongLength{expected: count_length(&self.answer),
actual: count_length(&guess) });
}
if !Self::correct_word(&self.alphabet, guess) {
return Err(GameError::NotInAlphabet(Self::return_first_wrong(&self.alphabet, guess)))
}
self.attempts += 1;
if self.compare(guess) {
self.status = GameStatus::Won;
}
if self.attempts == 5 {
self.status = GameStatus::Lost;
}
self.word_archive.push(Word::new(&self.answer, &guess).word);
Ok(Word::new(&self.answer, &guess))
}
}
impl Word {
fn find_char_spot(word: &str, ch: char) -> u8 {
let mut counter = 0;
for c in word.chars() {
if c == ch {
return counter;
}
counter += 1;
}
counter
}
fn correct_spot(word: &str, guess: &str, ch: char) -> bool {
let spot = Self::find_char_spot(guess, ch);
let mut counter = 0;
for c in word.chars() {
if counter == spot {
if c == ch {
return true;
}
else {
return false;
}
}
counter += 1;
}
false
}
pub fn new(answer: &str, guess: &str) -> Word {
let mut new_word = String::new();
for c in guess.chars() {
if contains_char(answer, c) {
if Self::correct_spot(answer.clone(),guess.clone(), c) {
// let ch = c.to_uppercase().to_string().as_bytes()[0] as char;
let ch = c.to_uppercase().next().unwrap();
new_word.push('[');
new_word.push(ch);
new_word.push(']');
}
else {
// let ch = c.to_uppercase().to_string().as_bytes()[0] as char;
let ch = c.to_uppercase().next().unwrap();
new_word.push('(');
new_word.push(ch);
new_word.push(')');
}
}
else {
// let ch = c.to_uppercase().to_string().as_bytes()[0] as char;
let ch = c.to_uppercase().next().unwrap();
new_word.push('>');
new_word.push(ch);
new_word.push('<');
}
}
Word { word: new_word }
}
}
use std::fmt::{self, Write};
impl fmt::Display for Word {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.word)
}
}
impl fmt::Display for Game{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// porblem - prints everything on one line
for word in &self.word_archive {
f.write_str(word)?;
}
Ok(())
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20230111-3772066-1j37228/solution) warning: unused import: `Write` --> src/lib.rs:193:22 | 193 | use std::fmt::{self, Write}; | ^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused variable: `c` --> src/lib.rs:39:9 | 39 | for c in word.chars() { | ^ help: if this is intentional, prefix it with an underscore: `_c` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `c` --> src/lib.rs:67:13 | 67 | for c in word.chars() { | ^ help: if this is intentional, prefix it with an underscore: `_c` warning: `solution` (lib) generated 3 warnings Finished test [unoptimized + debuginfo] target(s) in 0.73s 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 ... FAILED test solution_test::test_game_state_1 ... FAILED test solution_test::test_game_state_2 ... FAILED test solution_test::test_game_state_3 ... ok test solution_test::test_word_display ... ok test solution_test::test_word_display_german ... FAILED test solution_test::test_word_display_bulgarian ... ok test solution_test::test_word_display_with_repetitions ... 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_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 'assertion failed: `(left == right)` left: `"|_||_||_||_|(P)(E)(A)>K<"`, right: `"|_||_||_||_|\n(P)(E)(A)>K<"`', tests/solution_test.rs:109:5 ---- solution_test::test_game_display_cyrillic stdout ---- thread 'solution_test::test_game_display_cyrillic' panicked at 'assertion failed: `(left == right)` left: `"|_||_||_|>О<>П<(А)"`, right: `"|_||_||_|\n>О<>П<(А)"`', tests/solution_test.rs:121: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: `"|_||_||_|>F<[Ü][S]"`, right: `"|_||_||_|\n>F<[Ü][SS]"`', tests/solution_test.rs:135:5 ---- solution_test::test_game_state_1 stdout ---- thread 'solution_test::test_game_state_1' panicked at 'Expression Ok(Word { word: "[A][B][C]" }) does not match the pattern "Err(GameError::GameIsOver(GameStatus::Won))"', tests/solution_test.rs:146:5 ---- solution_test::test_game_state_2 stdout ---- thread 'solution_test::test_game_state_2' panicked at 'Expression Ok(Word { word: "[A][B][C]" }) does not match the pattern "Err(GameError::GameIsOver(GameStatus::Lost))"', tests/solution_test.rs:160:5 ---- solution_test::test_word_display_german stdout ---- thread 'solution_test::test_word_display_german' panicked at 'assertion failed: `(left == right)` left: `">F<[Ü][S]"`, 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<"`, 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_display_german solution_test::test_game_state_1 solution_test::test_game_state_2 solution_test::test_word_display_german solution_test::test_word_display_with_repetitions test result: FAILED. 8 passed; 7 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`