How to resolve the algorithm Wordle comparison step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Wordle comparison step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

Function wordle(Byval respuesta As String, Byval supuesto As String) As String
    Dim As Integer n, i, k
    Dim As String resultado
    
    n = Len(supuesto)
    If 5 <> Len(respuesta) Then
        Print respuesta; ": Expected 5 character target." : Return ""
    Elseif 5 <> Len(supuesto) Then
        Print supuesto; ": Expected 5 character guess." : Return ""
    Elseif n = Len(respuesta) Then
        resultado = Left("0000000000000000000", n)
        For i = 1 To n
            If Mid(supuesto, i, 1) = Mid(respuesta, i, 1) Then
                Mid(respuesta, i, 1) = "0"
                Mid(resultado, i, 1) = "2"
            End If
        Next i
        For i = 1 To n
            k = Instr(respuesta, Mid(supuesto, i, 1))
            If k Then
                Mid(respuesta, k, 1) = "0"
                Mid(resultado, i, 1) = "1"
            End If
        Next i
    Else
        Print "words must be same length"
    End If
    Return resultado
End Function


Data "ALLOW", "LOLLY", "CHANT", "LATTE", "ROBIN", "ALERT", "ROBIN", "SONIC", "ROBIN", "ROBIN"
Data "BULLY", "LOLLY", "ADAPT", "SÅLÅD", "Ukraine", "Ukraíne","BBAAB", "BBBBBAA", "BBAABBB", "AABBBAA"

Dim As String colores(3), respuesta, supuesto, res, res1
colores(0) = "grey" : colores(1) = "yellow" : colores(2) = "green"

Dim As Integer i, j
For i = 1 To 10 '5
    Read respuesta, supuesto
    res = wordle(respuesta, supuesto)
    
    If res <> "" Then
        res1 = ""
        For j = 1 To Len(res)
            res1 &= Mid(res, j, 1) & ", "
        Next j
        
        Print respuesta; " v "; supuesto; " => ["; Left(res1, Len(res1)-2); "] => ";
        
        For j = 1 To Len(res)
            Print using "\      \"; colores(Val(Mid(res, j, 1)));
        Next j
        Print
    End If
Next i
Sleep

  

You may also check:How to resolve the algorithm Sleeping Beauty problem step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Haskell programming language
You may also check:How to resolve the algorithm Associative array/Iteration step by step in the Wren programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Erlang programming language
You may also check:How to resolve the algorithm Multi-dimensional array step by step in the Phixmonti programming language