Решение на Wordle от Таня Димова
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug)]
#[derive(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 word_to_guess: Word,
pub alphabet: String,
pub history: String,
}
#[derive(Debug)]
#[derive(PartialEq)]
pub enum LetterState {
FullMatch,
PartialMatch,
NoMatch,
}
#[derive(Debug)]
pub struct Word {
pub word: String,
pub letter_states: Vec<LetterState>
}
impl PartialEq<GameStatus> for Game {
fn eq(&self, other: &GameStatus) -> bool {
self.status == *other
}
}
impl Word {
pub fn word_to_string(&self) -> String{
let mut result = String::from("");
for index in 0..self.word.chars().count(){
let mut letter = String::from("");
if self.letter_states[index] == LetterState::FullMatch{
letter.push('[');
letter.push_str(&self.word.chars().nth(index).unwrap().to_uppercase().to_string());
letter.push(']');
result.push_str(&letter);
}
else if self.letter_states[index] == LetterState::PartialMatch{
letter.push('(');
letter.push_str(&self.word.chars().nth(index).unwrap().to_uppercase().to_string());
letter.push(')');
result.push_str(&letter);
}
else{
letter.push('>');
letter.push_str(&self.word.chars().nth(index).unwrap().to_uppercase().to_string());
letter.push('<');
result.push_str(&letter);
}
}
return result
}
}
impl Game {
pub fn new(alphabet: &str, word: &str) -> Result<Self, GameError> {
match word.chars().all(|x| alphabet.contains(x)){
true => {
let mut init_hist = String::from("");
let mut vec_states: Vec::<LetterState> = Vec::new();
for _i in 0..word.chars().count(){
init_hist.push_str("|_|");
vec_states.push(LetterState::FullMatch)
}
let original_word = Word{word: String::from(word), letter_states: vec_states};
Ok(Self {status: GameStatus::InProgress, attempts: 0, word_to_guess: original_word,
alphabet: String::from(alphabet), history: init_hist})
}
false => {
let mut err_char = ' ';
for c in word.chars(){
if !alphabet.contains(c){
err_char = c;
break;
}
}
let error = GameError::NotInAlphabet(err_char);
Err(error)
}
}
}
pub fn guess_word(&mut self, guess: &str) -> Result<Word, GameError> {
if self == &GameStatus::Won{
return Err(GameError::GameIsOver(GameStatus::Won))
}
if self == &GameStatus::Lost{
return Err(GameError::GameIsOver(GameStatus::Lost))
}
match self.word_to_guess.word.chars().count() == guess.chars().count(){
true => match guess.chars().all(|x| self.alphabet.contains(x)){
false => {
let mut err_char = ' ';
for c in guess.chars(){
if !self.alphabet.contains(c){
err_char = c;
break;
}
}
let error = GameError::NotInAlphabet(err_char);
Err(error)
},
true => {
self.attempts = self.attempts + 1;
let mut vec_states: Vec::<LetterState> = Vec::new();
let mut index = 0;
for w in guess.chars(){
if self.word_to_guess.word.contains(w){
if self.word_to_guess.word.chars().nth(index).unwrap() == w{
vec_states.push(LetterState::FullMatch);
}
else {
vec_states.push(LetterState::PartialMatch);
}
}
else {
vec_states.push(LetterState::NoMatch);
}
index = index + 1;
}
let word_guess = Word{word: String::from(guess), letter_states: vec_states};
let word_to_history = word_guess.word_to_string();
self.history.push('\n');
self.history.push_str(&word_to_history[..]);
if self.word_to_guess.word.eq(guess){
self.status = GameStatus::Won;
}
else if self.attempts == 5{
self.status = GameStatus::Lost;
}
Ok(word_guess)
}
},
false => Err(GameError::WrongLength {expected: self.word_to_guess.word.chars().count(), actual: guess.len()}),
}
}
}
use std::fmt;
impl fmt::Display for Word {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.word_to_string()[..])
}
}
impl fmt::Display for Game {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.history[..])
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20230111-3772066-amvbbd/solution) Finished test [unoptimized + debuginfo] target(s) in 0.77s 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_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 ... ok test solution_test::test_word_display_with_repetitions ... ok 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 test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s