wordler 0.2.2

Library and cli for Wordle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! A library of structs to represent [Wordle] and its lifecycle.
//!
use crate::dictionary::Dictionary;
use ansi_term::Color::{Green, Red, White, RGB};
use anyhow::Result;
use std::fmt::Display;

/// Represents the Wordle game and its state.
pub struct Wordle<'w> {
    dictionary: &'w dyn Dictionary,
    word: String,
    current_attempt: u8,
    guesses: [TurnInput; 6],
    game_ended_at_attempt: u8,
}

/// Represent the type of match for each letter in user input.
#[derive(Debug, PartialEq)]
pub enum Match {
    /// When user input letter has exact location in actual answer.
    /// For example, if the actual answer is "DREAM" and user enters "CREAM",
    ///  all 4 letters in the end "REAM" have `ExactLocation` match.
    ExactLocation,
    /// When user input letter is present in the actual answer, but
    /// user provided letter at a different location from actual.
    /// For example, if the actual answer is "AGILE" and user enters "EAGLE",
    /// then "AG" is present in the word just at a different location.
    PresentInWord,
    /// When user input letter is not present in actual answer.
    /// For example, if the actual answer is "GREAT" and user enters
    /// "TWIST", then last 4 letters "WIST" are absent in the word.
    AbsentInWord,
}

impl Default for Match {
    fn default() -> Self {
        Match::AbsentInWord
    }
}

/// Represents each letter entered by user and its [Match] to actual answer.
#[derive(Debug, Default)]
pub struct Input {
    chr: u8,
    mch: Match,
}

/// Represents all 5 letters of user input and thier [Match] outcome for actual answer.
pub type TurnInput = [Input; 5];

/// Output of a single game play.
pub enum PlayResult<'w> {
    /// When game has not ended, we let user know the match that occured for thier play.
    TurnResult(&'w TurnInput),
    /// When user guesses actual answer.
    YouWon(&'w TurnInput),
    /// When user exhaust all of the 6 attempts, we let them know the actual answer.
    YouLost(&'w str),
}

impl<'w> Wordle<'w> {
    /// Create a new Wordle game with given [Dictionary]
    ///
    /// To have actual answer seeded from outside instead of
    /// a random word from dictionary use `SEED` enviroment variable.
    ///
    /// ```bash no_run
    /// SEED=dream wordlers
    /// ```
    pub fn new(dictionary: &'w dyn Dictionary) -> Self {
        let word: String;
        if let Ok(seed) = std::env::var("SEED") {
            let seed = seed.to_uppercase();
            if !dictionary.is_valid_word(seed.as_str()) {
                panic!("SEED ({}) is not a valid word in dictionary.", seed);
            }
            word = seed;
        } else {
            word = dictionary.random_word().to_uppercase();
        }

        Wordle {
            dictionary,
            word,
            current_attempt: Default::default(),
            guesses: Default::default(),
            game_ended_at_attempt: 128,
        }
    }

    /// The attempt number for the current play.
    pub fn current_attempt(&self) -> u8 {
        self.current_attempt + 1
    }

    /// Take user input as `word` and return the play outcome.
    pub fn play(&mut self, word: &str) -> Result<PlayResult> {
        if self.game_ended_at_attempt <= self.current_attempt + 1 {
            return Err(anyhow::anyhow!("Game Ended"));
        }

        if word.len() > 5 || word.len() < 5 {
            return Err(anyhow::anyhow!("Please enter a valid word with 5 letters."));
        }

        let word = word.to_uppercase();
        if self.dictionary.is_valid_word(word.as_str()) {
            let current_attempt = self.current_attempt as usize;
            self.current_attempt += 1;
            let mut input_letter_count = [0_u8; 26];
            for ch in self.word.as_bytes() {
                input_letter_count[(*ch - b'A') as usize] += 1
            }

            let mut processed: Vec<i8> = vec![1, 2, 3, 4, 5];
            let turn_input = &mut self.guesses[current_attempt];

            // first process exact matches
            for (idx, ch) in word.as_bytes().iter().enumerate() {
                turn_input[idx].chr = *ch;
                if self.word.as_bytes()[idx] == *ch {
                    turn_input[idx].mch = Match::ExactLocation;
                    input_letter_count[(ch - b'A') as usize] -= 1;
                    processed[idx] = -processed[idx];
                }
            }

            // process remaining letters (not present in word, or present in word)
            for position in processed.iter() {
                if *position > 0_i8 {
                    let index = (*position - 1) as usize;
                    let input_ch = word.as_bytes().get(index).unwrap();
                    let index_in_count = (*input_ch - b'A') as usize;
                    if input_letter_count[index_in_count] > 0 {
                        turn_input[index].mch = Match::PresentInWord;
                        input_letter_count[index_in_count] -= 1;
                    }
                }
            }

            if word == self.word {
                self.game_ended_at_attempt = self.current_attempt;
                return Ok(PlayResult::YouWon(&self.guesses[current_attempt]));
            }

            if self.current_attempt == 6 {
                self.game_ended_at_attempt = self.current_attempt;
                return Ok(PlayResult::YouLost(self.word.as_str()));
            } else {
                return Ok(PlayResult::TurnResult(&self.guesses[current_attempt]));
            }
        }

        Err(anyhow::anyhow!(
            "Please enter a valid word with 5 letters. Word not in dictionary: {}",
            word
        ))
    }
}

fn fmt_turn_input(f: &mut std::fmt::Formatter<'_>, turn_input: &TurnInput) -> std::fmt::Result {
    for input in turn_input {
        let letters = [b' ', input.chr, b' '];
        let letter = std::str::from_utf8(letters.as_slice()).unwrap();
        match input.mch {
            Match::AbsentInWord => write!(f, "{:3}", White.bold().on(Red).paint(letter))?,
            Match::ExactLocation => write!(f, "{:3}", RGB(0, 0, 0).bold().on(Green).paint(letter))?,
            Match::PresentInWord => {
                write!(
                    f,
                    "{:3}",
                    RGB(0, 0, 0)
                        .bold()
                        .on(RGB(255, 255, 0) /* Custom Yellow */)
                        .paint(letter)
                )?
            }
        }
    }
    Ok(())
}

impl<'w> Display for PlayResult<'w> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self {
            PlayResult::TurnResult(turn_input) => fmt_turn_input(f, turn_input),
            PlayResult::YouLost(word) => writeln!(f, "The word is {}. Ouch! 🤕", word),
            PlayResult::YouWon(turn_input) => {
                fmt_turn_input(f, turn_input)?;
                writeln!(f, "\nCongratulations you won! 🎉")
            }
        }
    }
}

mod tests {
    use super::*;

    struct TestDict;
    impl Dictionary for TestDict {
        fn random_word(&self) -> &str {
            "ARIEL"
        }

        fn is_valid_word(&self, word: &str) -> bool {
            ["ARIEL", "DREAM", "DRINK", "GLIDE", "GREAT", "TREAT"].contains(&word)
        }
    }
    #[test]
    fn test_win_single_attempt() {
        let test_dict = TestDict {};
        let mut wordle = Wordle::new(&test_dict);
        let play_result = wordle.play("ArIeL");
        assert!(play_result.is_ok());
        let play_result = play_result.unwrap();

        let expected_turn_input = [
            Input {
                chr: b'A',
                mch: Match::ExactLocation,
            },
            Input {
                chr: b'R',
                mch: Match::ExactLocation,
            },
            Input {
                chr: b'I',
                mch: Match::ExactLocation,
            },
            Input {
                chr: b'E',
                mch: Match::ExactLocation,
            },
            Input {
                chr: b'L',
                mch: Match::ExactLocation,
            },
        ];

        match play_result {
            PlayResult::YouWon(computed) => {
                assert_eq!(computed.len(), expected_turn_input.len());
                assert!(computed
                    .iter()
                    .zip(expected_turn_input.iter())
                    .all(|(com, exp)| com.chr == exp.chr && com.mch == exp.mch))
            }
            _ => panic!(),
        }

        // game ended
        assert!(wordle.play("dream").is_err());
    }

    #[test]
    fn test_win_six_attempts() {
        let test_dict = TestDict {};
        let mut wordle = Wordle::new(&test_dict);
        for word in ["DREAM", "DRINK", "GLIDE", "GREAT", "TREAT"] {
            let play_result = wordle.play(word);
            assert!(play_result.is_ok());
            assert!(matches!(play_result, Ok(PlayResult::TurnResult(_))));
        }

        let play_result = wordle.play("ariel");
        assert!(play_result.is_ok());
        assert!(matches!(play_result, Ok(PlayResult::YouWon(_))));
        // game ended
        assert!(wordle.play("ariel").is_err());
    }

    #[test]
    fn test_duplicate() {
        struct DupDict;
        impl Dictionary for DupDict {
            fn random_word(&self) -> &str {
                "GREED"
            }

            fn is_valid_word(&self, word: &str) -> bool {
                ["GREED", "ELITE"].contains(&word)
            }
        }

        let dup_dict = DupDict {};
        let mut wordle = Wordle::new(&dup_dict);
        let play_result = wordle.play("ELITE");
        assert!(play_result.is_ok());
        let play_result = play_result.unwrap();

        let expected_turn_input = [
            Input {
                chr: b'E',
                mch: Match::PresentInWord,
            },
            Input {
                chr: b'L',
                mch: Match::AbsentInWord,
            },
            Input {
                chr: b'I',
                mch: Match::AbsentInWord,
            },
            Input {
                chr: b'T',
                mch: Match::AbsentInWord,
            },
            Input {
                chr: b'E',
                mch: Match::PresentInWord,
            },
        ];

        match play_result {
            PlayResult::TurnResult(computed) => {
                assert_eq!(computed.len(), expected_turn_input.len());
                assert!(computed
                    .iter()
                    .zip(expected_turn_input.iter())
                    .all(|(com, exp)| com.chr == exp.chr && com.mch == exp.mch))
            }
            _ => panic!(),
        }
    }

    #[test]
    fn test_double_letters() {
        struct DupDict;
        impl Dictionary for DupDict {
            fn random_word(&self) -> &str {
                "GLIDE"
            }

            fn is_valid_word(&self, word: &str) -> bool {
                ["GLIDE", "GREED"].contains(&word)
            }
        }

        let dup_dict = DupDict {};
        let mut wordle = Wordle::new(&dup_dict);
        let play_result = wordle.play("GREED");
        assert!(play_result.is_ok());
        let play_result = play_result.unwrap();

        let expected_turn_input = [
            Input {
                chr: b'G',
                mch: Match::ExactLocation,
            },
            Input {
                chr: b'R',
                mch: Match::AbsentInWord,
            },
            Input {
                chr: b'E',
                mch: Match::PresentInWord,
            },
            Input {
                chr: b'E',
                mch: Match::AbsentInWord,
            },
            Input {
                chr: b'D',
                mch: Match::PresentInWord,
            },
        ];

        match play_result {
            PlayResult::TurnResult(computed) => {
                assert_eq!(computed.len(), expected_turn_input.len());
                assert!(computed
                    .iter()
                    .zip(expected_turn_input.iter())
                    .all(|(com, exp)| com.chr == exp.chr && com.mch == exp.mch))
            }
            _ => panic!(),
        }
    }

    #[test]
    fn test_double_letters_input_match_in_future() {
        struct DupDict;
        impl Dictionary for DupDict {
            fn random_word(&self) -> &str {
                "TRULY"
            }

            fn is_valid_word(&self, word: &str) -> bool {
                ["TRULY", "KELLY"].contains(&word)
            }
        }

        let dup_dict = DupDict {};
        let mut wordle = Wordle::new(&dup_dict);
        let play_result = wordle.play("KELLY");
        assert!(play_result.is_ok());
        let play_result = play_result.unwrap();

        let expected_turn_input = [
            Input {
                chr: b'K',
                mch: Match::AbsentInWord,
            },
            Input {
                chr: b'E',
                mch: Match::AbsentInWord,
            },
            Input {
                chr: b'L',
                mch: Match::AbsentInWord,
            },
            Input {
                chr: b'L',
                mch: Match::ExactLocation,
            },
            Input {
                chr: b'Y',
                mch: Match::ExactLocation,
            },
        ];

        match play_result {
            PlayResult::TurnResult(computed) => {
                assert_eq!(computed.len(), expected_turn_input.len());
                assert!(computed
                    .iter()
                    .zip(expected_turn_input.iter())
                    .all(|(com, exp)| com.chr == exp.chr && com.mch == exp.mch))
            }
            _ => panic!(),
        }
    }
}