Skip to content
Snippets Groups Projects
Commit 08aaf62d authored by tanguy.cavagna's avatar tanguy.cavagna :desktop:
Browse files

Starting the tool

parent c5341462
No related branches found
No related tags found
No related merge requests found
#include "tool/test/tool-test.h"
#include "unity/unity.h" #include "unity/unity.h"
#include "word-bank/test/bank-test.h" #include "word-bank/test/bank-test.h"
#include "wordle/test/wordle-test.h" #include "wordle/test/wordle-test.h"
...@@ -20,5 +21,9 @@ int main(void) { ...@@ -20,5 +21,9 @@ int main(void) {
RUN_TEST(test_answer_shoud_be_balai); RUN_TEST(test_answer_shoud_be_balai);
RUN_TEST(test_validate_letter_should_be_false); RUN_TEST(test_validate_letter_should_be_false);
RUN_TEST(test_validate_letter_should_be_true); 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(); return UNITY_END();
} }
\ No newline at end of file
#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);
}
#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
#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
#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
...@@ -48,12 +48,16 @@ void test_validate_letter_should_be_true(void) { ...@@ -48,12 +48,16 @@ void test_validate_letter_should_be_true(void) {
TEST_ASSERT_TRUE(validate_letter(i)); 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}; int target[WORD_LENGHT] = {0, 0, 0, 0, 0};
set_gamemode(MENU_SOLO); set_gamemode(MENU_SOLO);
set_answer("BALAI"); set_answer("BALAI");
set_try("PONTS"); 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
...@@ -14,6 +14,6 @@ void test_gamemode_should_be_computer(void); ...@@ -14,6 +14,6 @@ void test_gamemode_should_be_computer(void);
void test_answer_shoud_be_balai(void); void test_answer_shoud_be_balai(void);
void test_validate_letter_should_be_false(void); void test_validate_letter_should_be_false(void);
void test_validate_letter_should_be_true(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 #endif
...@@ -53,10 +53,10 @@ void handle_controls(int key) { ...@@ -53,10 +53,10 @@ void handle_controls(int key) {
break; break;
// Validations // Validations
int *tmp = letter_placement_control(chosen_word, tries[current_try_id]); int *pattern = generate_pattern();
for (int i = 0; i < WORD_LENGHT; i++) for (int i = 0; i < WORD_LENGHT; i++)
validations[current_try_id][i] = tmp[i]; validations[current_try_id][i] = pattern[i];
free(tmp); free(pattern);
current_try_id += 1; current_try_id += 1;
current_try_letter_id = 0; current_try_letter_id = 0;
...@@ -134,7 +134,7 @@ bool win_condition() { ...@@ -134,7 +134,7 @@ bool win_condition() {
bool validate_letter(int key) { return (islower(key) || isupper(key)); } bool validate_letter(int key) { return (islower(key) || isupper(key)); }
int *letter_placement_control() { int *generate_pattern() {
int *validation = calloc(WORD_LENGHT, sizeof(int)); int *validation = calloc(WORD_LENGHT, sizeof(int));
char cpy[WORD_LENGHT + 1]; char cpy[WORD_LENGHT + 1];
strcpy(cpy, chosen_word); strcpy(cpy, chosen_word);
...@@ -187,12 +187,14 @@ void set_gamemode(int menu_gamemode) { gamemode = menu_gamemode; } ...@@ -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_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; } int get_gamemode() { return gamemode; }
char *get_answer() { return chosen_word; } char *get_answer() { return chosen_word; }
char *get_try() { return tries[current_try_id]; }
void launch_game() { void launch_game() {
initialize_game(); initialize_game();
......
...@@ -71,7 +71,7 @@ bool validate_letter(int key); ...@@ -71,7 +71,7 @@ bool validate_letter(int key);
/** /**
* @brief Control the placement of each letter * @brief Control the placement of each letter
*/ */
int *letter_placement_control(); int *generate_pattern();
/** /**
* @brief Is a try valid * @brief Is a try valid
...@@ -119,6 +119,13 @@ int get_gamemode(); ...@@ -119,6 +119,13 @@ int get_gamemode();
*/ */
char *get_answer(); char *get_answer();
/**
* @brief Get the try
*
* @return
*/
char *get_try();
/** /**
* @brief Launch the game * @brief Launch the game
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment