This one was pretty tough for me. Not necessarily the coding concepts, but the grammar concepts. I actually failed my English II class in high school and was never really good in English class. However this code demonstrates how to create a complex sentence from various words that are stored in arrays.
Code:-- Create the random seed for the random function math.randomseed(os.time()) -- Our grammar arrays articles = {"the", "a", "one", "some"} nouns = {"baby", "boy", "girl", "child", "man", "woman", "dog", "cat", "town", "car"} verbs = {"drove", "jumped", "ran", "walked", "skipped", "joke"} prepositions = {"to", "from", "over", "under", "on", "about"} while true do -- Tell the user to press enter io.write("Press enter to generate a sentence.") io.read() -- Set article1 to some random number article1 = math.random(1, table.getn(articles)) -- Do the same for article two, but don't allow for duplicates repeat article2 = math.random(1, table.getn(articles)) until article1 ~= article2 -- Set noun1 to some random number noun1 = math.random(1, table.getn(nouns)) -- Do the same for noun two, but don't allow for duplicates repeat noun2 = math.random(1, table.getn(nouns)) until noun1 ~= noun2 -- Set the verb to some random number verb = math.random(1, table.getn(verbs)) -- Set the preposition to some random number preposition = math.random(1, table.getn(prepositions)) --Print out our random sentence by getting string that is associated with the array io.write(articles[article1] .. " " .. nouns[noun1] .. " " .. verbs[verb] .. " " .. prepositions[preposition] .. " " .. articles[article2] .. " " .. nouns[noun2] .. "\n\n") end




Reply With Quote