Sirshark10

hangman

Jul 31st, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. local function explode(str,char)
  2. local tbl = {}
  3. if char == "" then
  4. for i=1, str:len() do
  5. table.insert(tbl,str:sub(i,i))
  6. end
  7. return tbl
  8. elseif not str:find(char) then
  9. return {str}
  10. else
  11. local _,amt = str:gsub(char," ")
  12. for i=1, amt+1 do
  13. if str:find(char) then
  14. local x,y = str:find(char)
  15. local rep = str:sub(1,x-1)
  16. table.insert(tbl,rep)
  17. str = str:sub(y+1, str:len())
  18. else
  19. table.insert(tbl,str)
  20. end
  21. end
  22. end
  23. return tbl
  24. end
  25.  
  26.  
  27.  
  28. local words = {
  29. "test",
  30. "scoopta",
  31. "cloudninja",
  32. "explode",
  33. "wubstep",
  34. "kitty",
  35. "gaming",
  36. "switchcraft",
  37. "programming",
  38. "download"
  39. }
  40.  
  41. local chosenWord = words[math.random(1,#words)]
  42. local wTable = explode(chosenWord,"")
  43. local aTable = {}
  44. local solved = false
  45. local failed = false
  46. local gCorrect = 0
  47. for k,v in pairs(wTable) do
  48. aTable[k] = "_"
  49. end
  50.  
  51. local function eval(inp)
  52. local correct = 0
  53. for k,v in pairs(wTable) do
  54. if string.lower(inp) == string.lower(v) then
  55. correct = correct+1
  56. wTable[k] = "_"
  57. aTable[k] = inp
  58. end
  59. end
  60. local count = 0
  61. for k,v in pairs(wTable) do
  62. if v == "_" then
  63. count = count+1
  64. end
  65. end
  66.  
  67. if count == #wTable then
  68. solved = true
  69. end
  70.  
  71. if correct < 1 then
  72. gCorrect = gCorrect+1
  73. end
  74.  
  75. if gCorrect > 4 then
  76. failed = true
  77. end
  78. end
  79.  
  80. term.clear()
  81. while solved ~= true do
  82. print()
  83. term.clear()
  84. term.setCursorPos(1,1)
  85. print(table.unpack(aTable))
  86. print(" Tries Left: "..(6-gCorrect))
  87. write("Enter a letter: ")
  88. local input = read()
  89. print()
  90. if input == chosenWord then
  91. solved = true
  92. elseif #input < 1 then
  93. write("Please Enter a letter (press a key to continue)")
  94. sleep(0.5)
  95. os.pullEvent("key_up")
  96. elseif failed then
  97. break
  98. else
  99. eval(input)
  100. end
  101. end
  102. if solved then
  103. term.clear()
  104. term.setCursorPos(1,1)
  105. print("Solved")
  106. print("Word was: "..chosenWord)
  107. elseif failed then
  108. term.clear()
  109. term.setCursorPos(1,1)
  110. print("Failed")
  111. print("Word was: "..chosenWord)
  112. end
Advertisement
Add Comment
Please, Sign In to add comment