Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dictionary = {}
- local termWidth, termHeight
- local hangManGfx = {}
- hangManGfx[1] = [[
- ___________
- | |
- | _|_
- | /x x\
- | | _ |
- | \___/
- | |
- | |
- | /|\
- | / | \
- | / | \
- | / \
- | / \
- | / \
- |___________
- ]]
- hangManGfx[2] = [[
- ___________
- | |
- | _|_
- | / \
- | | |
- | \___/
- | |
- | |
- | /|\
- | / | \
- | / | \
- | /
- | /
- | /
- |___________
- ]]
- hangManGfx[3] = [[
- ___________
- | |
- | _|_
- | / \
- | | |
- | \___/
- | |
- | |
- | /|\
- | / | \
- | / | \
- |
- |
- |
- |___________
- ]]
- hangManGfx[4] = [[
- ___________
- | |
- | _|_
- | / \
- | | |
- | \___/
- | |
- | |
- | /|
- | / |
- | / |
- |
- |
- |
- |___________
- ]]
- hangManGfx[5] = [[
- ___________
- | |
- | _|_
- | / \
- | | |
- | \___/
- | |
- | |
- | |
- | |
- | |
- |
- |
- |
- |___________
- ]]
- hangManGfx[6] = [[
- ___________
- | |
- | _|_
- | / \
- | | |
- | \___/
- |
- |
- |
- |
- |
- |
- |
- |
- |___________
- ]]
- hangManGfx[7] = [[
- ___________
- | |
- | |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |___________
- ]]
- hangManGfx[8] = [[
- ___________
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |___________
- ]]
- hangManGfx[9] = [[
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |___________
- ]]
- hangManGfx[10] = [[
- ___________
- ]]
- function setupDictionary()
- local files = fs.list(shell.dir())
- for k, v in ipairs(files) do
- if string.sub(v, #v - 4, #v) ~= ".dict" then
- table.remove(files, k)
- end
- end
- if #files < 1 then
- print("No .dict files found. Please place .dict files in "..shell.dir())
- return false
- end
- local selection
- while true do
- print("Please pick a dictionary to use by typing its number.")
- for i = 1, #files do
- print(i, ". ", files[i])
- end
- selection = tonumber(read())
- if selection and selection >= 1 and selection <= #files then
- break
- else
- print("Invalid number."..#files)
- end
- end
- print(selection)
- local file = io.open(files[selection], "r")
- for line in file:lines() do
- table.insert(dictionary, line)
- end
- file:close()
- return true
- end
- function getWord()
- math.randomseed(os.clock())
- return dictionary[math.random(1, #dictionary)]
- end
- function obsfucateWord(word, replaceChar)
- replaceChar = replaceChar or "_"
- return string.gsub(word, "%a", replaceChar)
- end
- function removeLetter(word, obsfucatedWord, letter)
- local matchesFound = {}
- local start = 0
- local foundPos
- while true do
- foundPos = string.find(string.lower(word), string.lower(letter), start)
- if foundPos then
- table.insert(matchesFound, foundPos)
- start = foundPos + 1
- else
- break
- end
- end
- if #matchesFound == 0 then return nil end
- local newString = obsfucatedWord
- local nextStart = 0
- term.setCursorPos(1, termHeight / 2)
- for k, v in pairs(matchesFound) do
- newString = string.sub(newString, 0, v - 1)..string.sub(word, v, v)..string.sub(newString, v + 1, string.len(newString))
- end
- return newString
- end
- function getTableSpaces(tab)
- local writeStr = ""
- for i = 1, #tab do
- writeStr = writeStr..tab[i].." "
- end
- return writeStr
- end
- function drawHangman(triesLeft)
- local x = termWidth / 2
- local y = (termHeight - 16) / 2
- for line in hangManGfx[triesLeft]:gmatch("[^\r\n]+") do
- term.setCursorPos(x, y)
- write(line)
- y = y + 1
- end
- end
- function confirm(prompt)
- term.setCursorPos(1, termHeight)
- term.clearLine(termHeight)
- write(prompt.." (Y/N)")
- local event, param
- while true do
- event, param = os.pullEvent()
- if event == "char" then
- if string.lower(param) == "y" then
- return true
- elseif string.lower(param) == "n" then
- term.clearLine(termHeight)
- term.setCursorPos(1, termHeight)
- write("Press Backspace to quit")
- return false
- end
- end
- end
- end
- function main()
- term.setCursorPos(1, 1)
- term.clear()
- if not setupDictionary() then return end
- while true do
- termWidth, termHeight = term.getSize()
- term.setCursorPos(1, 1)
- term.clear()
- local wordPicked = getWord()
- local underlineWord = obsfucateWord(wordPicked)
- term.setCursorPos(1, termHeight - 1)
- print(underlineWord)
- write("Press Backspace to quit")
- local event, param
- local triesLeft = 11
- local usedLetters = {}
- while true do
- event, param = os.pullEvent()
- if event == "char" then
- local newObs = removeLetter(wordPicked, underlineWord, param)
- if not newObs then
- triesLeft = triesLeft - 1
- drawHangman(triesLeft)
- table.insert(usedLetters, string.lower(param))
- term.setCursorPos(1, termHeight - 2)
- write("Used Letters: "..getTableSpaces(usedLetters))
- if triesLeft <= 1 then
- term.setCursorPos(1, termHeight - 1)
- write(wordPicked)
- if not confirm("Game over! Would you like to try again?") then
- return
- else
- break
- end
- end
- else
- underlineWord = newObs
- term.setCursorPos(1, termHeight - 1)
- term.clearLine(termHeight - 1)
- write(underlineWord)
- if wordPicked == underlineWord then
- if not confirm("You won! Would you like to play again?") then
- return
- else
- break
- end
- end
- end
- elseif event == "key" and param == 14 then -- handle Backspace
- if confirm("Are you sure you want to quit?") then
- return
- end
- end
- end
- end
- end
- main()
- term.clear()
- term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment