Clean coding

Hi!

I have a hangman game that is working but I was wondering if there was a better way to code the function that checks to see if the entire word has been found. Here is the code that works but is long…\

local t=#currentWord
if 3 == t and currentWord[1].isVisible == true and currentWord[2].isVisible == true and currentWord[3].isVisible == true then
winCondition()
print(“win”)
elseif 4 == t and currentWord[1].isVisible == true and currentWord[2].isVisible == true and currentWord[3].isVisible == true and currentWord[4].isVisible == true then
winCondition()
print(“win”)
elseif 5 == t and currentWord[1].isVisible == true and currentWord[2].isVisible == true and currentWord[3].isVisible == true and currentWord[4].isVisible == true and currentWord[5].isVisible == true then
winCondition()
print(“win”)
elseif 6 == t and currentWord[1].isVisible == true and currentWord[2].isVisible == true and currentWord[3].isVisible == true and currentWord[4].isVisible == true and currentWord[5].isVisible == true and currentWord[6].isVisible == true then
winCondition()
print(“win”)
elseif 7 == t and currentWord[1].isVisible == true and currentWord[2].isVisible == true and currentWord[3].isVisible == true and currentWord[4].isVisible == true and currentWord[5].isVisible == true and currentWord[6].isVisible == true and currentWord[7].isVisible == true then
winCondition()
print(“win”)

end

All input is appreaciated!!! [import]uid: 170524 topic_id: 31015 reply_id: 331015[/import]

How about something like this:

[blockcode]
local t = #currentWord
local isWon = true

for i = 1,t do
if currentWord[i].isVisible == false then
isWon = false
end
end

if isWon == true then
winCondition()
print(“win”)
end
[/blockcode]

  • Andrew [import]uid: 109711 topic_id: 31015 reply_id: 124010[/import]

That’s fantastic! Thank you, I had tried that but I did the reverse where isWon = false to start and then turned true. It didn’t work lol.

Thanks for the help! [import]uid: 170524 topic_id: 31015 reply_id: 124054[/import]

How about something like this:

[blockcode]
local t = #currentWord
local isWon = true

for i = 1,t do
if currentWord[i].isVisible == false then
isWon = false
end
end

if isWon == true then
winCondition()
print(“win”)
end
[/blockcode]

  • Andrew [import]uid: 109711 topic_id: 31015 reply_id: 124010[/import]

That’s fantastic! Thank you, I had tried that but I did the reverse where isWon = false to start and then turned true. It didn’t work lol.

Thanks for the help! [import]uid: 170524 topic_id: 31015 reply_id: 124054[/import]