Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Global variables
- DebugMode = false
- Dictionary = io.open("./dictionary.txt", "r"):read("*all")
- function split(my_str)
- local t = {}
- for str in string.gmatch(my_str, "[^\n]+") do
- table.insert(t, str)
- end
- return t
- end
- DictionaryList = split(Dictionary)
- -- Hey! Give me my Underscore String already (US - 'str')
- function GetUnderscoreString(Word, CorrectLetters)
- US = ""
- for Index, Value in pairs(CorrectLetters) do
- if Value == true then
- Letter = Word:sub(Index,Index)
- US = US .. Letter
- else
- US = US .. "_"
- end
- end
- return US
- end
- -- Hey! Check how many they got right or wrong! (CorrectLetters - 'dict', Changed - 'boolean')
- function CheckAnswer(Word, Letter, CorrectLetters)
- Index = 1
- Changed = false
- for wordLetter in string.gmatch(Word,".") do
- if wordLetter:lower() == Letter:lower() then
- CorrectLetters[Index] = true
- Changed = true
- end
- Index = Index + 1
- end
- return CorrectLetters, Changed
- end
- -- Is the game complete? (true/false - 'boolean')
- function IsComplete(CorrectLetters)
- CorrectLetterAmount = 0
- for _, Value in pairs(CorrectLetters) do
- if Value == true then
- CorrectLetterAmount = CorrectLetterAmount + 1
- end
- end
- if CorrectLetterAmount >= #(CorrectLetters) then
- return true
- end
- return false
- end
- -- Run the game please!!!
- function main()
- -- Main variables
- math.randomseed(os.time())
- Won = false
- CorrectLetters = {}
- UsedLetters = {}
- Lives = 10
- Word = DictionaryList[math.random(1,#(DictionaryList))]
- -- Debug mode
- io.write("Run in debug mode? It tells you the word, mainly used for editing. Y / N? ")
- RunInDebug = io.read()
- if RunInDebug:lower() == "y" then
- DebugMode = true
- else
- DebugMode = false
- end
- -- Introduction
- print("Hello, welcome to hangman!")
- print("My word is " .. #(Word) .. " letters long!")
- if DebugMode then
- print("Word is " .. Word)
- end
- for Index = 1, #Word do
- CorrectLetters[Index] = false
- end
- -- We ask the questions here, bud!
- while true do
- US = GetUnderscoreString(Word, CorrectLetters)
- if IsComplete(CorrectLetters) then
- Won = true
- break
- end
- if Lives <= 0 then
- break
- end
- print("You have " .. Lives .. " / 10 lives left.")
- print(US)
- io.write("Guess a letter: ")
- Chosen = io.read()
- if #(Chosen) <= 0 then
- print("You must input a letter!")
- else
- RawLetter = Chosen:sub(1,1):lower()
- if UsedLetters[RawLetter] then
- print("Already guessed " .. RawLetter .. "!")
- else
- UsedLetters[RawLetter] = true
- CorrectLetters, Changed = CheckAnswer(Word, RawLetter, CorrectLetters)
- if not Changed then
- Lives = Lives - 1
- end
- end
- end
- end
- if Won == true then
- print("Congratulations!")
- print("You figured out the word! The word was " .. Word .. "!")
- else
- print("Sorry... You failed...")
- print("The word was " .. Word .. "...")
- end
- while true do
- do end
- end
- end
- -- All part of the master plan...
- main()
- -- To be completely honest, I thought this was gonna be so much harder.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement