Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wordle logic #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions solutions/wordle_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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']--;
}
}
}

Expand All @@ -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;
Expand Down
32 changes: 24 additions & 8 deletions tutorial/wordle_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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']--;
}
}
}

Expand Down