From 7cdf807337a97991a737d59b2c88ce7459f47042 Mon Sep 17 00:00:00 2001 From: dDogge Date: Fri, 3 Jan 2025 16:44:41 +0100 Subject: [PATCH] Finalized --- README.md | 6 ++++++ ordle.cc | 5 ++++- test/test.cc | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2102e19..19a7fd0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ # Ordle: A Wordle Solver +## Douglas Fjällrud, Axel Blomén ## Design The program is structured into three main components that work together to solve Wordle puzzles. The first part handles the loading and preparation of the word list. The *read\_candidates* function reads all words from a file and filters out those that are not exactly five letters long. At the same time, the words are converted to lowercase, and duplicates are removed. This ensures that the program starts with a clean list of potential solutions. +The *build_list* function processes user input to create mappings of letters to their respective indices. This function plays a crucial role in interpreting user-provided data about correct and misplaced letters, converting it into a format usable by the filtering logic. By breaking down input strings into index-letter pairs, *build_list* ensures the program can apply precise filtering conditions. + The second part of the program focuses on filtering words based on user input. Three functors are used to handle different types of requirements: - *wrong\_fn*: Filters out words that contain letters the user has marked as "incorrect". @@ -12,10 +15,12 @@ These functors are applied in the *do\_filter* function, which filters the curre The third component manages user interaction. The *prompt* function is used to gather input from the user, such as letters that do not appear in the word, green letters with their positions, and yellow letters with their incorrect positions. This input is then used to update the filtering process. + The program consists of three files: - **main.cc**: Runs the program and uses logic from **ordle.cc**. - **ordle.cc**: Implements the core logic. - **ordle.h**: Contains the definitions for the functions and functors in *ordle.cc*. +- **test.cc**: contains the unit tests ## Build Instructions Common actions are defined in the provided Justfile and can be viewed with @@ -71,6 +76,7 @@ Common actions are defined in the provided Justfile and can be viewed with | Lint Code | `make clang-tidy` | | Static Analysis | `make cppcheck` | | Format Code | `make format` | +| Run the tests | `make test` | | Watch and Rebuild | `just watch` | --- diff --git a/ordle.cc b/ordle.cc index 75587e4..fbcf53d 100644 --- a/ordle.cc +++ b/ordle.cc @@ -39,8 +39,11 @@ bool correct_fn::operator()(const std::string &word) const { }); } -// TODO:Correct but ugly bool misplaced_fn::operator()(const std::string &word) const { + if (map.empty()) { + return true; + } + if (std::all_of(map.begin(), map.end(), [&word](const auto &pair) { return pair.first < word.size() && word[pair.first] == pair.second; })) { diff --git a/test/test.cc b/test/test.cc index 8ec5385..a906a7f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -114,6 +114,17 @@ void test_build_list() { } } +void test_misplaced_fn_empty() { + std::vector candidates = {"apple", "ample", "angle"}; + std::string wrong = "x"; + IndexMap green{{1, 'p'}}; + IndexMap yellow; + do_filter(candidates, wrong, green, yellow); + assert(candidates.size() == 1); + assert(candidates[0] == "apple"); + std::cout << "test_misplaced_fn_empty passed.\n"; +} + int main() { test_read_candidates(); test_contains_any_of(); @@ -122,6 +133,7 @@ int main() { test_wrong_fn(); test_correct_fn(); test_misplaced_fn(); + test_misplaced_fn_empty(); test_do_filter(); test_build_list();