How to resolve the algorithm Wordle comparison step by step in the Java programming language
How to resolve the algorithm Wordle comparison step by step in the Java 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:
Explanation of the Java code for Wordle comparison
This code defines a class named WordleComparison
that simulates Wordle gameplay and displays the color-coded result for each guess compared to the answer.
1. Imports and Class Definition:
java.util.List
: Used to hold a collection of word pairs.java.util.stream.Collectors
: Used to collect results from streams.java.util.stream.Stream
: Used to generate a stream of elements.
2. Main Method:
- Creates a list of
TwoWords
objects, each containing an answer and a guess. - Iterates through the list and prints the answer, guess, and the result of comparing them using the
wordle
method.
3. wordle Method:
- Takes the answer and guess strings as input.
- Checks if the lengths are equal, throwing an assertion error if not.
- Creates a copy of the answer string to avoid modifying the original.
- Initializes a list of
Colour
enums with the same length as the words, all set toGREY
. - Iterates through each character in the guess:
- If the character matches the corresponding character in the answer:
- Replaces the matched character in the answer copy with a special character (
NULL
) to avoid matching it again. - Sets the color of the guess character in the result list to
GREEN
.
- Replaces the matched character in the answer copy with a special character (
- If the character matches the corresponding character in the answer:
- Iterates through each character in the guess again:
- If the character exists in the remaining answer copy (excluding matched characters):
- Replaces the first occurrence of the character in the answer copy with
NULL
. - Sets the color of the guess character in the result list to
YELLOW
.
- Replaces the first occurrence of the character in the answer copy with
- If the character exists in the remaining answer copy (excluding matched characters):
- Returns the final list of colors representing the comparison result.
4. Additional Elements:
- Colour enum: Defines the three possible colors (GREEN, GREY, YELLOW) used to represent the match status of each character in the guess.
- TwoWords record: Defines a record with two fields -
answer
andguess
- to hold word pairs efficiently. - NULL constant: Defines a special character (
\0
) used to mark matched characters in the answer copy to avoid repeating matches in the second loop.
Summary:
This code simulates the Wordle logic by comparing two strings (answer and guess) and assigns color codes based on the match status of each character:
- Green: Character is in the correct position and matches the answer.
- Yellow: Character is present in the answer but not in the correct position.
- Grey: Character is not present in the answer.
Step by Step solution about How to resolve the algorithm Wordle comparison step by step in the Java programming language
Source code in the java programming language
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class WordleComparison {
public static void main(String[] args) {
List<TwoWords> pairs = List.of( new TwoWords("ALLOW", "LOLLY"), new TwoWords("ROBIN", "SONIC"),
new TwoWords("CHANT", "LATTE"), new TwoWords("We're", "She's"), new TwoWords("NOMAD", "MAMMA") );
for ( TwoWords pair : pairs ) {
System.out.println(pair.answer + " v " + pair.guess + " -> " + wordle(pair.answer, pair.guess));
}
}
private static List<Colour> wordle(String answer, String guess) {
final int guessLength = guess.length();
if ( answer.length() != guessLength ) {
throw new AssertionError("The two words must be of the same length.");
}
String answerCopy = answer;
List<Colour> result = Stream.generate( () -> Colour.GREY ).limit(guessLength).collect(Collectors.toList());
for ( int i = 0; i < guessLength; i++ ) {
if ( answer.charAt(i) == guess.charAt(i) ) {
answerCopy = answerCopy.substring(0, i) + NULL + answerCopy.substring(i + 1);
result.set(i, Colour.GREEN);
}
}
for ( int i = 0; i < guessLength; i++ ) {
int index = answerCopy.indexOf(guess.charAt(i));
if ( index >= 0 ) {
answerCopy = answerCopy.substring(0, index) + NULL + answerCopy.substring(index + 1);
result.set(i, Colour.YELLOW);
}
}
return result;
}
private enum Colour { GREEN, GREY, YELLOW }
private static record TwoWords(String answer, String guess) {}
private static final char NULL = '\0';
}
You may also check:How to resolve the algorithm Dot product step by step in the FALSE programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the CLU programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the AWK programming language
You may also check:How to resolve the algorithm Resistor mesh step by step in the Python programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the Prolog programming language