How to resolve the algorithm Wordle comparison step by step in the C programming language
How to resolve the algorithm Wordle comparison step by step in the C programming language
Table of Contents
Problem Statement
While similar to both Bulls and cows and Mastermind, Wordle is a notable variation, having experienced a viral surge in popularity, and reverse engineering the game or creating variants has become a popular programming exercise. However, a sampling of the "code a Wordle clone" videos on YouTube shows that seven of the eight reviewed had a serious flaw in the way that they assigned colours to the letters of a guessed word. This aspect of the game is described here: en.wikipedia.org/wiki/Wordle#Gameplay Create a function or procedure that takes two strings; the answer string, and the guess string, and returns a string, list, dynamic array or other ordered sequence indicating how each letter should be marked as per the description above. (e.g. "green", "yellow", or "grey", or, equivalently, the integers 2, 1, or 0 or suchlike.) You can assume that both the answer string and the guess string are the same length, and contain only printable characters/code points in the ASCII/UniCode Range ! to ~ (hex 20 to 7F) and that case is significant. (The original game only uses strings of 5 characters, all alphabetic letters, all in the same case, but this allows for most existing variants of the game.) Provide test data and show the output here. The test data should include the answer string ALLOW and the guess string LOLLY, and the result should be (yellow, yellow, green, grey, grey) or equivalent.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Wordle comparison step by step in the C programming language
This C program is a simple implementation of the popular word game Wordle. In Wordle, players have six tries to guess a five-letter word. After each guess, the game provides feedback in the form of colored tiles:
- Green: The letter is in the correct position.
- Yellow: The letter is in the word but in the wrong position.
- Gray: The letter is not in the word.
Here's a breakdown of the program:
Function wordle
:
- Accepts three arguments:
answer
: The secret word to guess.guess
: The player's guess.result
: An array to store the feedback for each letter in the guess.
- Compares the guess to the answer and sets the feedback accordingly:
- If a letter in the guess matches a letter in the answer at the same position, it marks that letter as green (with a result of 2).
- If a letter in the guess matches a letter in the answer but in a different position, it marks that letter as yellow (with a result of 1).
- Otherwise, it marks the letter as gray (with a result of 0).
Main Function (main
):
- Initializes arrays for the answer, guess, feedback numbers, and feedback colors.
- Defines five pairs of words to be used as test cases.
- Iterates through the word pairs and calls the
wordle
function for each pair. - Prints the feedback for each pair, both as numbers (green: 2, yellow: 1, gray: 0) and as colored text ("green", "yellow", "gray").
Example Output:
ALLOW v LOLLY => { 2 0 2 1 0 } => { green grey green yellow grey }
BULLY v LOLLY => { 0 2 0 1 0 } => { grey green grey yellow grey }
ROBIN v ALERT => { 0 0 2 0 0 } => { grey grey green grey grey }
ROBIN v SONIC => { 0 0 0 2 0 } => { grey grey grey green grey }
ROBIN v ROBIN => { 2 2 2 2 2 } => { green green green green green }
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void wordle(const char *answer, const char *guess, int *result) {
int i, ix, n = strlen(guess);
char *ptr;
if (n != strlen(answer)) {
printf("The words must be of the same length.\n");
exit(1);
}
char answer2[n+1];
strcpy(answer2, answer);
for (i = 0; i < n; ++i) {
if (guess[i] == answer2[i]) {
answer2[i] = '\v';
result[i] = 2;
}
}
for (i = 0; i < n; ++i) {
if ((ptr = strchr(answer2, guess[i])) != NULL) {
ix = ptr - answer2;
answer2[ix] = '\v';
result[i] = 1;
}
}
}
int main() {
int i, j;
const char *answer, *guess;
int res[5];
const char *res2[5];
const char *colors[3] = {"grey", "yellow", "green"};
const char *pairs[5][2] = {
{"ALLOW", "LOLLY"},
{"BULLY", "LOLLY"},
{"ROBIN", "ALERT"},
{"ROBIN", "SONIC"},
{"ROBIN", "ROBIN"}
};
for (i = 0; i < 5; ++i) {
answer = pairs[i][0];
guess = pairs[i][1];
for (j = 0; j < 5; ++j) res[j] = 0;
wordle(answer, guess, res);
for (j = 0; j < 5; ++j) res2[j] = colors[res[j]];
printf("%s v %s => { ", answer, guess);
for (j = 0; j < 5; ++j) printf("%d ", res[j]);
printf("} => { ");
for (j = 0; j < 5; ++j) printf("%s ", res2[j]);
printf("}\n");
}
return 0;
}
You may also check:How to resolve the algorithm Define a primitive data type step by step in the Ursala programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Topological sort step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Five weekends step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Knight's tour step by step in the CoffeeScript programming language