diff --git a/solutions/wordle_server.c b/solutions/wordle_server.c index 0147529..711f286 100644 --- a/solutions/wordle_server.c +++ b/solutions/wordle_server.c @@ -10,11 +10,12 @@ * we will actually randomise the word the user is guessing. */ char word[WORD_LENGTH] = { 'h', 'e', 'l', 'l', 'o' }; +enum character_state states[WORD_LENGTH] = { INCORRECT }; #define CLIENT_CHANNEL 1 #define VMM_CHANNEL 2 -bool is_character_in_word(char *word, int ch) { +bool char_in_word(char *word, char ch) { for (int i = 0; i < WORD_LENGTH; i++) { if (word[i] == ch) { return true; @@ -24,13 +25,28 @@ bool is_character_in_word(char *word, int ch) { return false; } -enum character_state char_to_state(int ch, char *word, uint64_t index) { - if (ch == word[index]) { - return CORRECT_PLACEMENT; - } else if (is_character_in_word(word, ch)) { - return INCORRECT_PLACEMENT; - } else { - return INCORRECT; +void calculate_states(char *word, char *guess , enum character_state *states) { + int counts[26] = { 0 }; + + for (int i = 0; i < WORD_LENGTH; i++) { + counts[word[i] - 'a']++; + } + + for (int i = 0; i < WORD_LENGTH; i++) { + if (guess[i] == word[i]) { + states[i] = CORRECT_PLACEMENT; + counts[guess[i] - 'a']--; + } else { + states[i] = INCORRECT; + } + } + + for (int i = 0; i < WORD_LENGTH; i++) { + if (guess[i] != word[i] && char_in_word(word, guess[i]) + && counts[guess[i] - 'a'] > 0) { + states[i] = INCORRECT_PLACEMENT; + counts[guess[i] - 'a']--; + } } } @@ -44,9 +60,14 @@ microkit_msginfo protected(microkit_channel channel, microkit_msginfo msginfo) { switch (channel) { case CLIENT_CHANNEL: + char guess[WORD_LENGTH]; + for (int i = 0; i < WORD_LENGTH; i++) { + guess[i] = microkit_mr_get(i); + } + + calculate_states(word, guess, states); for (int i = 0; i < WORD_LENGTH; i++) { - char ch = microkit_mr_get(i); - microkit_mr_set(i, char_to_state(ch, word, i)); + microkit_mr_set(i, states[i]); } return microkit_msginfo_new(0, WORD_LENGTH); break; diff --git a/tutorial/wordle_server.c b/tutorial/wordle_server.c index baceb15..91d5f1c 100644 --- a/tutorial/wordle_server.c +++ b/tutorial/wordle_server.c @@ -10,8 +10,9 @@ * we will actually randomise the word the user is guessing. */ char word[WORD_LENGTH] = { 'h', 'e', 'l', 'l', 'o' }; +enum character_state states[WORD_LENGTH] = { INCORRECT }; -bool is_character_in_word(char *word, int ch) { +bool char_in_word(char *word, char ch) { for (int i = 0; i < WORD_LENGTH; i++) { if (word[i] == ch) { return true; @@ -21,13 +22,28 @@ bool is_character_in_word(char *word, int ch) { return false; } -enum character_state char_to_state(int ch, char *word, uint64_t index) { - if (ch == word[index]) { - return CORRECT_PLACEMENT; - } else if (is_character_in_word(word, ch)) { - return INCORRECT_PLACEMENT; - } else { - return INCORRECT; +void calculate_states(char *word, char *guess , enum character_state *states) { + int counts[26] = { 0 }; + + for (int i = 0; i < WORD_LENGTH; i++) { + counts[word[i] - 'a']++; + } + + for (int i = 0; i < WORD_LENGTH; i++) { + if (guess[i] == word[i]) { + states[i] = CORRECT_PLACEMENT; + counts[guess[i] - 'a']--; + } else { + states[i] = INCORRECT; + } + } + + for (int i = 0; i < WORD_LENGTH; i++) { + if (guess[i] != word[i] && char_in_word(word, guess[i]) + && counts[guess[i] - 'a'] > 0) { + states[i] = INCORRECT_PLACEMENT; + counts[guess[i] - 'a']--; + } } }