Bolodefchoco_LUAXML

[Learning] Forca

Jan 20th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. math.randomseed = os.time()
  2.  
  3. _F = "Score: %d\n\n|---| %s\n| %s\n|%s%s%s\n|%s %s\n|\n|>%s"
  4.  
  5. words = {"apple","dog","juice","cookies","bring","ring","snow","german","sandwich","sadness","happiness","pressure","depression","sing","marriage","battery","halloween","christmas","reindeer","bee"}
  6.  
  7. table.find = function(list,value)
  8.     for k,v in next,list do
  9.         if v == value then
  10.             return true
  11.         end
  12.     end
  13.     return false
  14. end
  15.  
  16. newWord = ""
  17. user = {
  18.     [1] = {}, -- Word
  19.     [2] = {}, -- Is correct
  20.     [3] = {}, -- Fail
  21.     [4] = 0 -- Score
  22. }
  23.  
  24. play = function()
  25.     user = {{},{},{},user[4]}
  26.  
  27.     local nextWord = ""
  28.     repeat
  29.         nextWord = words[math.random(1,#words)]
  30.     until newWord ~= nextWord
  31.     newWord = nextWord
  32.  
  33.     local update = function()
  34.         os.execute("cls")
  35.         local man = {}
  36.         for k,v in next,{"O","/","|","\\","/","\\"} do
  37.             man[#man+1] = (#user[3] >= k and v or "")
  38.         end
  39.         man[#man+1] = table.concat(user[1])
  40.  
  41.         print(_F:format(user[4],table.concat(user[3]," "),unpack(man)))
  42.     end
  43.  
  44.     while #user[3] < 6 do
  45.         for i = 1,#newWord do
  46.             local letter = newWord:sub(i,i)
  47.             user[1][i] = " "..(table.find(user[2],letter) and letter or "_")
  48.         end
  49.  
  50.         update()
  51.  
  52.         if not table.find(user[1]," _") then
  53.             print("\nSuccess!\n")
  54.             break
  55.         end
  56.  
  57.         local newLetter = ""
  58.         repeat
  59.             io.write("\nLetter: ")
  60.             newLetter = io.read()
  61.             newLetter = newLetter:lower()
  62.         until #newLetter == 1 and newLetter:find("^%a") and not table.find(user[2],newLetter) and not table.find(user[3],newLetter)
  63.        
  64.         local index = (newWord:find(newLetter) and 2 or 3)
  65.         user[index][#user[index]+1] = newLetter
  66.     end
  67.  
  68.     if #user[3] >= 6 then
  69.         update()
  70.         print("\nThe word were \""..newWord.."\"...\n")
  71.     else
  72.         user[4] = user[4] + 1
  73.     end
  74.  
  75.     os.execute("pause")
  76.     play()
  77. end
  78.  
  79. play()
Add Comment
Please, Sign In to add comment