From 738d08fb451e3eed1e98a558c8c36cb3a1f3c2cc Mon Sep 17 00:00:00 2001
From: "tanguy.cavagna" <tanguy.cavagna@etu.hesge.ch>
Date: Mon, 16 May 2022 09:25:17 +0200
Subject: [PATCH] Better control logic and added correct letters in next try by
 default

---
 ui/ui.c         | 13 ++++++----
 wordle/wordle.c | 63 +++++++++++++++++++++++++++++++++----------------
 2 files changed, 51 insertions(+), 25 deletions(-)

diff --git a/ui/ui.c b/ui/ui.c
index 7ad15a4..995b115 100644
--- a/ui/ui.c
+++ b/ui/ui.c
@@ -237,7 +237,14 @@ void toggle_help() {
 void show_gameboard(int tries_count, int word_length, char **tries, int **validations, int score) {
     for (int i = 0; i < tries_count; i++) {
         // Draw current try letters
-        for (size_t j = 0; j < strlen(tries[i]); j++) {
+        for (int j = 0; j < word_length; j++) {
+            // Placeholder
+            if (tries[i][j] == '\0') {
+                mvaddch(GAMEBOARD_Y_OFFSET + i, TERM_MID_COLS - word_length + (j * 2), '_');
+                continue;
+            }
+
+            // Letters
             if (validations[i][j] == 1)
                 attron(COLOR_PAIR(ORANGE_PAIR));
             if (validations[i][j] == 2)
@@ -247,10 +254,6 @@ void show_gameboard(int tries_count, int word_length, char **tries, int **valida
             attroff(COLOR_PAIR(GREEN_PAIR));
             attroff(COLOR_PAIR(ORANGE_PAIR));
         }
-
-        // Draw remaning try letters placeholder
-        for (size_t j = 0; j < word_length - strlen(tries[i]); j++)
-            mvaddstr(GAMEBOARD_Y_OFFSET + i, TERM_MID_COLS + (word_length / 2) - (j * 2), " _");
     }
 
     move(GAMEBOARD_Y_OFFSET - 2, TERM_MID_COLS - word_length - 3);
diff --git a/wordle/wordle.c b/wordle/wordle.c
index 010e15f..21af6b5 100644
--- a/wordle/wordle.c
+++ b/wordle/wordle.c
@@ -20,6 +20,7 @@ void initialize_game() {
     _game_finished = false;
     current_try_id = 0;
     current_try_letter_id = 0;
+    score = 0;
 
     // Tries and answer setup
     chosen_word = get_random_word();
@@ -125,9 +126,9 @@ void render_game() {
 bool validate_letter(int key) { return (islower(key) || isupper(key)); }
 
 /**
- * @brief Validate the last guess
+ * @brief Control the placement of each letter
  */
-int *validate_guess(char answer[WORD_LENGHT], char try[WORD_LENGHT]) {
+int *letter_placement_control(char answer[WORD_LENGHT], char try[WORD_LENGHT]) {
     int *validation = calloc(WORD_LENGHT, sizeof(int));
     char cpy[WORD_LENGHT + 1];
     strcpy(cpy, answer);
@@ -153,6 +154,36 @@ int *validate_guess(char answer[WORD_LENGHT], char try[WORD_LENGHT]) {
     return validation;
 }
 
+/**
+ * @brief Is a try valid
+ *
+ * @param try
+ * @return true
+ * @return false
+ */
+bool validate_guess(char try[WORD_LENGHT]) {
+    // Length check
+    if (strlen(try) < WORD_LENGHT)
+        return false;
+
+    // Presence check
+    char **bank = get_bank();
+    bool present = false;
+    for (int i = 0; i < get_bank_size(); i++) {
+        if (!strcmp(bank[i], try))
+            present = true;
+    }
+
+    if (!present) {
+        current_try_letter_id = 0;
+        for (int i = 0; i < WORD_LENGHT; i++)
+            tries[current_try_id][i] = '\0';
+        return false;
+    }
+
+    return true;
+}
+
 /**
  * @brief Handle controls
  *
@@ -169,33 +200,25 @@ void handle_controls(int key) {
         break;
 
     case KEYBIND_GUESS:
-        // Length check
-        if (strlen(tries[current_try_id]) < WORD_LENGHT)
+        if (!validate_guess(tries[current_try_id]))
             break;
 
-        // Presence check
-        char **bank = get_bank();
-        bool present = false;
-        for (int i = 0; i < get_bank_size(); i++) {
-            if (!strcmp(bank[i], tries[current_try_id]))
-                present = true;
-        }
-
-        if (!present) {
-            current_try_letter_id = 0;
-            for (int i = 0; i < WORD_LENGHT; i++)
-                tries[current_try_id][i] = '\0';
-            break;
-        }
-
         // Validations
-        int *tmp = validate_guess(chosen_word, tries[current_try_id]);
+        int *tmp = letter_placement_control(chosen_word, tries[current_try_id]);
         for (int i = 0; i < WORD_LENGHT; i++)
             validations[current_try_id][i] = tmp[i];
         free(tmp);
 
         current_try_id += 1;
         current_try_letter_id = 0;
+
+        // Place correct letters from previous in the current one
+        if (current_try_id < TRIES_COUNT) {
+            for (int i = 0; i < WORD_LENGHT; i++) {
+                if (validations[current_try_id - 1][i] == LETTER_PLACED)
+                    tries[current_try_id][i] = tries[current_try_id - 1][i];
+            }
+        }
         break;
     }
 }
-- 
GitLab