Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. -- Global variables
  2. DebugMode = false
  3. Dictionary = io.open("./dictionary.txt", "r"):read("*all")
  4.  
  5. function split(my_str)
  6. local t = {}
  7. for str in string.gmatch(my_str, "[^\n]+") do
  8. table.insert(t, str)
  9. end
  10. return t
  11. end
  12.  
  13. DictionaryList = split(Dictionary)
  14.  
  15. -- Hey! Give me my Underscore String already (US - 'str')
  16. function GetUnderscoreString(Word, CorrectLetters)
  17. US = ""
  18. for Index, Value in pairs(CorrectLetters) do
  19. if Value == true then
  20. Letter = Word:sub(Index,Index)
  21. US = US .. Letter
  22. else
  23. US = US .. "_"
  24. end
  25. end
  26. return US
  27. end
  28.  
  29. -- Hey! Check how many they got right or wrong! (CorrectLetters - 'dict', Changed - 'boolean')
  30. function CheckAnswer(Word, Letter, CorrectLetters)
  31. Index = 1
  32. Changed = false
  33. for wordLetter in string.gmatch(Word,".") do
  34. if wordLetter:lower() == Letter:lower() then
  35. CorrectLetters[Index] = true
  36. Changed = true
  37. end
  38. Index = Index + 1
  39. end
  40. return CorrectLetters, Changed
  41. end
  42.  
  43. -- Is the game complete? (true/false - 'boolean')
  44. function IsComplete(CorrectLetters)
  45. CorrectLetterAmount = 0
  46. for _, Value in pairs(CorrectLetters) do
  47. if Value == true then
  48. CorrectLetterAmount = CorrectLetterAmount + 1
  49. end
  50. end
  51. if CorrectLetterAmount >= #(CorrectLetters) then
  52. return true
  53. end
  54. return false
  55. end
  56.  
  57. -- Run the game please!!!
  58. function main()
  59. -- Main variables
  60. math.randomseed(os.time())
  61. Won = false
  62. CorrectLetters = {}
  63. UsedLetters = {}
  64. Lives = 10
  65. Word = DictionaryList[math.random(1,#(DictionaryList))]
  66.  
  67. -- Debug mode
  68. io.write("Run in debug mode? It tells you the word, mainly used for editing. Y / N? ")
  69. RunInDebug = io.read()
  70. if RunInDebug:lower() == "y" then
  71. DebugMode = true
  72. else
  73. DebugMode = false
  74. end
  75. -- Introduction
  76. print("Hello, welcome to hangman!")
  77. print("My word is " .. #(Word) .. " letters long!")
  78. if DebugMode then
  79. print("Word is " .. Word)
  80. end
  81.  
  82. for Index = 1, #Word do
  83. CorrectLetters[Index] = false
  84. end
  85.  
  86. -- We ask the questions here, bud!
  87. while true do
  88. US = GetUnderscoreString(Word, CorrectLetters)
  89. if IsComplete(CorrectLetters) then
  90. Won = true
  91. break
  92. end
  93.  
  94. if Lives <= 0 then
  95. break
  96. end
  97.  
  98. print("You have " .. Lives .. " / 10 lives left.")
  99. print(US)
  100.  
  101. io.write("Guess a letter: ")
  102. Chosen = io.read()
  103. if #(Chosen) <= 0 then
  104. print("You must input a letter!")
  105. else
  106. RawLetter = Chosen:sub(1,1):lower()
  107.  
  108. if UsedLetters[RawLetter] then
  109. print("Already guessed " .. RawLetter .. "!")
  110. else
  111. UsedLetters[RawLetter] = true
  112.  
  113. CorrectLetters, Changed = CheckAnswer(Word, RawLetter, CorrectLetters)
  114.  
  115. if not Changed then
  116. Lives = Lives - 1
  117. end
  118. end
  119. end
  120. end
  121.  
  122. if Won == true then
  123. print("Congratulations!")
  124. print("You figured out the word! The word was " .. Word .. "!")
  125. else
  126. print("Sorry... You failed...")
  127. print("The word was " .. Word .. "...")
  128. end
  129.  
  130. while true do
  131. do end
  132. end
  133.  
  134. end
  135.  
  136. -- All part of the master plan...
  137. main()
  138.  
  139. -- To be completely honest, I thought this was gonna be so much harder.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement