I have two words, one being the players guess and one being the actual word.
Lets say the actual word is “WATER”.
Lets say the players guess is “WAATR”
How could I compare the two words so that if the player guess letter is in the same position as the actual word letter it will be green.
If the letter is in the actual word but at a different index then it will be orange.
If the letter doesn’t exist at all in the word it will be white.
So in this scenario it will be Green, Green, White, Orange, Green.
I have tried placing each letter into a table and iterating through but its getting a little confusing.
Maybe someone with a clear mind could offer support. Thanks.
Well, since I don’t have any information on your display system, I’ll just wing it:
local function guessWord(word,guess,frame)
local findList = string.split(string.toUpper(word),"")
local guessList = string.split(string.toUpper(guess),"")
--initialize outputColors, since you're dealing with non-5 letter words.
local outputColors = {}
for i=1,#findList do
table.insert(outputColors,"white")
end
--get greens
for v,i in pairs(guessList) do
if findList[v] == i then
outputColors[v] = "green"
findList[v] = nil
end
end
--now handle yellows
for v,i in pairs(guessList) do
local idx = table.find(findList,i)
if idx then
outputColors[v] = "orange"
findList[idx] = nil
end
end
for v,i in pairs(outputColors) do
local change = frame:FindFirstChild(v).TextLabel
change.Text = guessList[v]
if i == "green" then
change.TextColor3 = Color3.fromRGB(134, 255, 107)
elseif i == "orange" then
change.TextColor3 = Color3.fromRGB(255, 182, 99)
else
change.TextColor3 = Color3.fromRGB(255, 255, 255)
end
end
end
This should do what you’re asking, but better than what I had posted earlier, and should work with your system.
local function fillBoxes(frame, word, guess, correct)
local highWord = string.upper(word)
local highGuess = string.upper(guess)
local wordList = {}
local guessList = {}
local correctWrongTable = {}
for i = 1, #word do
wordList[i] = tostring(string.char(string.byte(highWord, i, i)))
end
print(wordList)
for i = 1, #guess do
guessList[i] = tostring(string.char(string.byte(highGuess, i, i)))
end
print(guessList)
for i = 1, #wordList, 1 do
for v = 1, #guessList, 1 do
if i == v then
if wordList[i] == guessList[i] then
correctWrongTable[i] = "correct"
i += 1
end
end
if wordList[i] == guessList[v] then
correctWrongTable[i] = "exists"
i+=1
end
if correctWrongTable[i] == nil then
correctWrongTable[i] = "wrong"
end
end
end
for i = 1, #guessList, 1 do
frame:FindFirstChild(i).TextLabel.Text = tostring(guessList[i])
end
for i = 1, #guessList, 1 do
if correctWrongTable[i] == "correct" then
frame:FindFirstChild(i).TextLabel.TextColor3 = Color3.fromRGB(134, 255, 107)
elseif correctWrongTable[i] == "exists" then
frame:FindFirstChild(i).TextLabel.TextColor3 = Color3.fromRGB(255, 182, 99)
elseif correctWrongTable[i] == "wrong" then
frame:FindFirstChild(i).TextLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
end
end
end
This is the entire function, it displays it to a GUI on the players screen, the gui is split into x amount of boxes each numbered 1 - x.
In this case the word was “EAR” and I entered “ERB”, B should have been white as it isnt in the word “Ear” and R should have been orange as is in the wrong index.
--now handle yellows
for v,i in pairs(guessList) do
local idx = table.find(wordList, guessList[v])
print(idx)
if idx then
correctWrongTable[v] = "orange"
wordList[idx] = nil
end
end
So I adjusted this part so as your table.find bit wasnt getting the letter from the list. But IDX is printing nil?
Ahh, my bad! I forgot to mention this: I make sure you don’t get false greens or oranges by changing a match in the wordList to nil. So, to guess again, you’d have to reset the wordList, which you already know how to do.