diff --git a/test.c b/test.c index edd9bc63a7523aff7e554fe21bebceab8c1bc468..f955a189f672cf9c84db24ff4b399d81b99fb78d 100644 --- a/test.c +++ b/test.c @@ -1,3 +1,4 @@ +#include "tool/test/tool-test.h" #include "unity/unity.h" #include "word-bank/test/bank-test.h" #include "wordle/test/wordle-test.h" @@ -20,5 +21,9 @@ int main(void) { RUN_TEST(test_answer_shoud_be_balai); RUN_TEST(test_validate_letter_should_be_false); RUN_TEST(test_validate_letter_should_be_true); + RUN_TEST(test_generate_pattern_should_return_all_0); + + // Tool + RUN_TEST(test_compute_matches_should_be_empty); return UNITY_END(); } \ No newline at end of file diff --git a/tool/test/tool-test.c b/tool/test/tool-test.c new file mode 100644 index 0000000000000000000000000000000000000000..18c1a4e4dcb08f0b6a332399e733985913e25a2d --- /dev/null +++ b/tool/test/tool-test.c @@ -0,0 +1,19 @@ +#include "tool-test.h" + +void test_compute_matches_should_be_empty(void) { + set_gamemode(MENU_SOLO); + init_tool(); + + set_answer("BALAI"); + + set_try("BALAE"); + int *pattern = generate_pattern(); + possibility_t **p = compute_matches("BALAE", pattern); + + printf("%d\n", get_remaning_bank_count()); + // for (int i = 0; i < get_remaning_bank_count(); i++) + // printf("%s\n", p[i]->word); + + free(pattern); + destroy_possibilities(p); +} diff --git a/tool/test/tool-test.h b/tool/test/tool-test.h new file mode 100644 index 0000000000000000000000000000000000000000..b36957d0dd41b8a72f3abca0c0dba367d6d005f5 --- /dev/null +++ b/tool/test/tool-test.h @@ -0,0 +1,10 @@ +#ifndef TOOL_TEST_H +#define TOOL_TEST_H + +#include "../../unity/unity.h" +#include "../../wordle/wordle.h" +#include "../tool.h" + +void test_compute_matches_should_be_empty(void); + +#endif diff --git a/tool/tool.c b/tool/tool.c new file mode 100644 index 0000000000000000000000000000000000000000..b120d8b623d59be113b75d6dfe948293c6bf2b47 --- /dev/null +++ b/tool/tool.c @@ -0,0 +1,84 @@ +#include "tool.h" + +//========================== +// PRIVATE +//========================== +int possibility_count = 0; +char **remaning_bank; +int remaning_bank_count; +possibility_t **possibilities; + +possibility_t *create_possibility(char *word, double probability) { + possibility_t *p = malloc(sizeof(possibility_t)); + p->word = word; + p->probability = probability; + return p; +} + +//========================== +// PUBLIC +//========================== +void init_tool() { + remaning_bank = get_bank(); + remaning_bank_count = get_bank_size(); +} + +void destroy_possibilities(possibility_t **p) { + for (int i = 0; i < possibility_count; i++) + free(p[i]); + free(p); +} + +possibility_t **compute_matches(char *word, int *pattern) { + possibility_count = 0; + char tmp_poss[remaning_bank_count][WORD_LENGHT + 1]; + + for (int i = 0; i < remaning_bank_count; i++) + *tmp_poss[i] = *"\0"; + + char **tmp_bank = remaning_bank; + for (int j = 0; j < remaning_bank_count; j++) { + int valid_word = 0; + + for (int i = 0; i < WORD_LENGHT; i++) { + if (pattern[i] == LETTER_NOT_PRESENT) + continue; + + char tmp_c[2] = {word[i], '\0'}; + + if (pattern[i] == LETTER_PLACED) { + if (strcspn(tmp_bank[j], tmp_c) == i) { + printf("%ld\t%c\t%s\t%s\n", strcspn(tmp_bank[j], tmp_c), word[i], tmp_bank[j], word); + strcpy(tmp_poss[j], tmp_bank[j]); + possibility_count++; + continue; + } + } + } + } + + possibilities = calloc(possibility_count, sizeof(possibility_t *)); + int idx = 0; + for (int i = 0; i < remaning_bank_count; i++) { + if (strlen(tmp_poss[i]) != 0) { + possibilities[idx] = create_possibility(tmp_poss[i], 0.0); + idx++; + } + } + + return possibilities; +} + +int get_remaning_bank_count() { return remaning_bank_count; } + +char **agregate_possibilities(char *word, int *pattern) { + char **words = malloc(sizeof(char *) * POSSIBILITY_SET); + possibility_t **matches = compute_matches(word, pattern); + + for (int i = 0; i < 2; i++) + words[i] = matches[i]->word; + + // sort by probability ASC + + return words; +} \ No newline at end of file diff --git a/tool/tool.h b/tool/tool.h new file mode 100644 index 0000000000000000000000000000000000000000..8ea019c7e3c001253f71d023a93de25cfd46a9a2 --- /dev/null +++ b/tool/tool.h @@ -0,0 +1,51 @@ +#ifndef TOOL_H +#define TOOL_H + +#include "../word-bank/bank.h" +#include "../wordle/wordle.h" + +// Tool maximum possibility set +#define POSSIBILITY_SET 10 + +typedef struct _possibility_t { + char *word; + double probability; +} possibility_t; + +/** + * @brief Initialize the tool + */ +void init_tool(); + +/** + * @brief Destroy possibilities + * @param p + */ +void destroy_possibilities(possibility_t **p); + +/** + * @brief Compute all possible matches + * + * @param word + * @param pattern + * @return + */ +possibility_t **compute_matches(char *word, int *pattern); + +/** + * @brief Get the remaning bank count + * + * @return + */ +int get_remaning_bank_count(); + +/** + * @brief Agregate a defined set of possibile words from a given pattern + * + * @param word + * @param pattern + * @return + */ +char **agregate_possibilities(char *word, int *pattern); + +#endif diff --git a/wordle/test/wordle-test.c b/wordle/test/wordle-test.c index 9acefaa2d880acb5e21969b6fcc72656ca117487..8d372f2c95f21aa695c3171be9bb9db3fca8d267 100644 --- a/wordle/test/wordle-test.c +++ b/wordle/test/wordle-test.c @@ -48,12 +48,16 @@ void test_validate_letter_should_be_true(void) { TEST_ASSERT_TRUE(validate_letter(i)); } -void test_letter_placement_control_should_return_all_0(void) { +void test_generate_pattern_should_return_all_0(void) { int target[WORD_LENGHT] = {0, 0, 0, 0, 0}; set_gamemode(MENU_SOLO); set_answer("BALAI"); set_try("PONTS"); - TEST_ASSERT_EQUAL_INT_ARRAY(target, letter_placement_control(), WORD_LENGHT); + int *pattern = generate_pattern(); + + TEST_ASSERT_EQUAL_INT_ARRAY(target, pattern, WORD_LENGHT); + + free(pattern); } \ No newline at end of file diff --git a/wordle/test/wordle-test.h b/wordle/test/wordle-test.h index 980f67f979c94fb99a93cc6825427a3ca37e30db..355ed0d08532ad5af12b7cd1070b1d9965ab9386 100644 --- a/wordle/test/wordle-test.h +++ b/wordle/test/wordle-test.h @@ -14,6 +14,6 @@ void test_gamemode_should_be_computer(void); void test_answer_shoud_be_balai(void); void test_validate_letter_should_be_false(void); void test_validate_letter_should_be_true(void); -void test_letter_placement_control_should_return_all_0(void); +void test_generate_pattern_should_return_all_0(void); #endif diff --git a/wordle/wordle.c b/wordle/wordle.c index 3ae5fbe7457b35724f63d37b6c5b420fe1b05f5d..9da722e81dd5f584488639fab0120f2deae9c119 100644 --- a/wordle/wordle.c +++ b/wordle/wordle.c @@ -53,10 +53,10 @@ void handle_controls(int key) { break; // Validations - int *tmp = letter_placement_control(chosen_word, tries[current_try_id]); + int *pattern = generate_pattern(); for (int i = 0; i < WORD_LENGHT; i++) - validations[current_try_id][i] = tmp[i]; - free(tmp); + validations[current_try_id][i] = pattern[i]; + free(pattern); current_try_id += 1; current_try_letter_id = 0; @@ -134,7 +134,7 @@ bool win_condition() { bool validate_letter(int key) { return (islower(key) || isupper(key)); } -int *letter_placement_control() { +int *generate_pattern() { int *validation = calloc(WORD_LENGHT, sizeof(int)); char cpy[WORD_LENGHT + 1]; strcpy(cpy, chosen_word); @@ -187,12 +187,14 @@ void set_gamemode(int menu_gamemode) { gamemode = menu_gamemode; } void set_answer(char answer[WORD_LENGHT]) { chosen_word = answer; } -void set_try(char current_try[WORD_LENGHT]) { tries[current_try_id] = current_try; } +void set_try(char current_try[WORD_LENGHT]) { strcpy(tries[current_try_id], current_try); } int get_gamemode() { return gamemode; } char *get_answer() { return chosen_word; } +char *get_try() { return tries[current_try_id]; } + void launch_game() { initialize_game(); diff --git a/wordle/wordle.h b/wordle/wordle.h index 871bf274ccdb97dd0673b3e8eac6dcff602633ce..9f86eda80b22d2f45c3e5568478519b2c56f4ea8 100644 --- a/wordle/wordle.h +++ b/wordle/wordle.h @@ -71,7 +71,7 @@ bool validate_letter(int key); /** * @brief Control the placement of each letter */ -int *letter_placement_control(); +int *generate_pattern(); /** * @brief Is a try valid @@ -119,6 +119,13 @@ int get_gamemode(); */ char *get_answer(); +/** + * @brief Get the try + * + * @return + */ +char *get_try(); + /** * @brief Launch the game *