How to resolve the algorithm Wordle comparison step by step in the XPL0 programming language
How to resolve the algorithm Wordle comparison step by step in the XPL0 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 XPL0 programming language
Source code in the xpl0 programming language
string 0;
proc ShowColors(Result);
char Result;
int Color, I;
[Color:= ["gray ", "yellow ", "green "];
for I:= 0 to 4 do
Text(0, Color(Result(I)));
CrLf(0);
];
func Wordle(Answer, Guess);
char Answer, Guess, Result;
int I, J;
[Result:= " ";
for I:= 0 to 4 do
if Guess(I) = Answer(I) then
[Result(I):= 2; Answer(I):= 0]
else Result(I):= 0;
for I:= 0 to 4 do
for J:= 0 to 4 do
if Guess(I) = Answer(J) then
[Result(I):= 1; Answer(J):= 0];
return Result;
];
[ShowColors(Wordle("ALLOW", "LOLLY"));
ShowColors(Wordle("BULLY", "LOLLY"));
ShowColors(Wordle("ROBIN", "ALERT"));
ShowColors(Wordle("ROBIN", "SONIC"));
ShowColors(Wordle("ROBIN", "ROBIN"));
]
You may also check:How to resolve the algorithm Comments step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Read entire file step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Shell one-liner step by step in the Prolog programming language
You may also check:How to resolve the algorithm Classes step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the BASIC programming language