如何用Lua的for..do循环实现猜词游戏的逐条目遍历逻辑?
Hey there! I see what you're trying to build— a word guessing game that walks through your WordTable one entry at a time, showing a shuffled word, waiting for the user to guess correctly, then moving to the next. The issue with your current code is that the for loop inside getTypingWord() tries to run all iterations in one go, but you need to pause between each entry to wait for the user's input (since they press Enter to submit each guess).
Let's adjust the logic to use a persistent index tracker instead of a synchronous for loop, and make the flow work as intended:
Step-by-Step Fixes
- Track the current position globally: We'll use a variable outside the functions to keep track of which entry we're on, so it persists between
Enterpresses. - Remove the synchronous
forloop: Instead, each time the user presses Enter, we handle the current entry, then move to the next only if the guess is correct. - Add a completion check: Once we've gone through all entries, we can show a final message.
Corrected Code
-- Your word table remains the same WordTable = { {lv = 1, v_random = "LEOHL", v_word = "HELLO"}, {lv = 1, v_random = "GEEINN", v_word = "ENGINE"}, {lv = 1, v_random = "TECHA", v_word = "CHEAT"}, {lv = 2, v_random = "dwIl prEARmgorm", v_word = "WILD PROGRAMMER"}, {lv = 2, v_random = "abDEKory deEPRss", v_word = "KEYBOARD PRESSED"}, } -- Persistent variable to track our current position in the table local currentIndex = 1 local totalPoints = 0 local totalScore = 0 -- Helper function to get table length (yours works fine!) function tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end -- Initialize the first word when the game starts local function initFirstWord() if currentIndex <= tablelength(WordTable) then local tab = WordTable[currentIndex] labelWrd.Caption = tab.v_random end end -- Call this to set up the first word when your game loads initFirstWord() function getTypingWord() -- Check if we've finished all entries local totalEntries = tablelength(WordTable) if currentIndex > totalEntries then showMessage("Game complete! Final score: " .. totalScore) return end local tab = WordTable[currentIndex] local crtWord = tab.v_word local mcWord = string.upper(edtWrd.Text) -- Handle empty input if mcWord == '' or mcWord == nil then showMessage("Please enter a guess!") edtWrd.Text = '' return end -- Check if guess is correct if mcWord == crtWord then showMessage('Great, the answer is '..crtWord) edtWrd.Text = '' -- Update score and UI totalPoints = totalPoints + 1 totalScore = totalPoints * 200 labelLevel.Caption = 'Correct : '..totalPoints labelScore.Caption = 'Score : '..totalScore -- Move to next entry currentIndex = currentIndex + 1 -- Show next word if there are more entries if currentIndex <= totalEntries then local nextTab = WordTable[currentIndex] labelWrd.Caption = nextTab.v_random end else showMessage('Try Again') edtWrd.Text = '' end end -- Wait for Enter key press to submit guess edtWrd.OnKeyPress = function(sender, key) if isKeyPressed(VK_RETURN) then getTypingWord() end return key end
Key Changes Explained
- Persistent
currentIndex: This variable lives outside the functions, so it remembers which entry we're on every time the user presses Enter. - No synchronous
forloop: Instead of trying to loop through all entries in one function call, we handle one entry per Enter press— which aligns perfectly with how the user interacts with the game. - Initialization function:
initFirstWord()sets up the first shuffled word when the game starts, so the user has something to guess right away. - Completion check: When
currentIndexexceeds the total number of entries, we show a game complete message to wrap up the session.
This flow will now correctly walk through each entry in WordTable: show the shuffled word, wait for a correct guess, then move to the next— until all words are guessed!
内容的提问来源于stack exchange,提问作者JoeFern

