CloudyPSP

Hangman

May 16th, 2012
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.59 KB | None | 0 0
  1. local dictionary = {}
  2. local termWidth, termHeight
  3. local hangManGfx = {}
  4. hangManGfx[1] = [[
  5.  ___________
  6. |     |
  7. |    _|_
  8. |   /x x\
  9. |  |  _  |
  10. |   \___/
  11. |     |
  12. |     |
  13. |    /|\
  14. |   / | \
  15. |  /  |  \
  16. |    / \
  17. |   /   \
  18. |  /     \
  19. |___________
  20. ]]
  21. hangManGfx[2] = [[
  22.  ___________
  23. |     |
  24. |    _|_
  25. |   /   \
  26. |  |     |
  27. |   \___/
  28. |     |
  29. |     |
  30. |    /|\
  31. |   / | \
  32. |  /  |  \
  33. |    /
  34. |   /
  35. |  /
  36. |___________
  37. ]]
  38. hangManGfx[3] = [[
  39.  ___________
  40. |     |
  41. |    _|_
  42. |   /   \
  43. |  |     |
  44. |   \___/
  45. |     |
  46. |     |
  47. |    /|\
  48. |   / | \
  49. |  /  |  \
  50. |
  51. |
  52. |
  53. |___________
  54. ]]
  55. hangManGfx[4] = [[
  56.  ___________
  57. |     |
  58. |    _|_
  59. |   /   \
  60. |  |     |
  61. |   \___/
  62. |     |
  63. |     |
  64. |    /|
  65. |   / |
  66. |  /  |
  67. |
  68. |
  69. |
  70. |___________
  71. ]]
  72. hangManGfx[5] = [[
  73.  ___________
  74. |     |
  75. |    _|_
  76. |   /   \
  77. |  |     |
  78. |   \___/
  79. |     |
  80. |     |
  81. |     |
  82. |     |
  83. |     |
  84. |
  85. |
  86. |
  87. |___________
  88. ]]
  89. hangManGfx[6] = [[
  90.  ___________
  91. |     |
  92. |    _|_
  93. |   /   \
  94. |  |     |
  95. |   \___/
  96. |
  97. |
  98. |
  99. |
  100. |
  101. |
  102. |
  103. |
  104. |___________
  105. ]]
  106. hangManGfx[7] = [[
  107.  ___________
  108. |     |
  109. |     |
  110. |
  111. |
  112. |
  113. |
  114. |
  115. |
  116. |
  117. |
  118. |
  119. |
  120. |
  121. |___________
  122. ]]
  123. hangManGfx[8] = [[
  124.  ___________
  125. |
  126. |
  127. |
  128. |
  129. |
  130. |
  131. |
  132. |
  133. |
  134. |
  135. |
  136. |
  137. |
  138. |___________
  139. ]]
  140.  
  141. hangManGfx[9] = [[
  142.  
  143. |
  144. |
  145. |
  146. |
  147. |
  148. |
  149. |
  150. |
  151. |
  152. |
  153. |
  154. |
  155. |
  156. |___________
  157. ]]
  158.  
  159. hangManGfx[10] = [[
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  ___________
  175. ]]
  176.  
  177. function setupDictionary()
  178.     local files = fs.list(shell.dir())
  179.  
  180.     for k, v in ipairs(files) do
  181.         if string.sub(v, #v - 4, #v) ~= ".dict" then
  182.             table.remove(files, k)
  183.         end
  184.     end
  185.    
  186.        
  187.     if #files < 1 then
  188.         print("No .dict files found. Please place .dict files in "..shell.dir())
  189.         return false
  190.     end
  191.  
  192.     local selection
  193.     while true do
  194.         print("Please pick a dictionary to use by typing its number.")
  195.         for i = 1, #files do
  196.             print(i, ". ", files[i])
  197.         end
  198.         selection = tonumber(read())
  199.         if selection and selection >= 1 and selection <= #files then
  200.             break
  201.         else
  202.             print("Invalid number."..#files)
  203.         end
  204.        
  205.     end
  206.    
  207.     print(selection)
  208.    
  209.     local file = io.open(files[selection], "r")
  210.     for line in file:lines() do
  211.         table.insert(dictionary, line)
  212.     end
  213.     file:close()
  214.     return true
  215. end
  216.  
  217. function getWord()
  218.     math.randomseed(os.clock())
  219.     return dictionary[math.random(1, #dictionary)]
  220. end
  221.  
  222. function obsfucateWord(word, replaceChar)
  223.     replaceChar = replaceChar or "_"
  224.     return string.gsub(word, "%a", replaceChar)
  225. end
  226.  
  227. function removeLetter(word, obsfucatedWord, letter)
  228.     local matchesFound = {}
  229.     local start = 0
  230.     local foundPos
  231.    
  232.     while true do
  233.         foundPos = string.find(string.lower(word), string.lower(letter), start)
  234.         if foundPos then
  235.             table.insert(matchesFound, foundPos)
  236.             start = foundPos + 1
  237.         else
  238.             break
  239.         end
  240.     end
  241.    
  242.     if #matchesFound == 0 then return nil end
  243.    
  244.     local newString = obsfucatedWord
  245.     local nextStart = 0
  246.    
  247.     term.setCursorPos(1, termHeight / 2)
  248.    
  249.     for k, v in pairs(matchesFound) do
  250.         newString = string.sub(newString, 0, v - 1)..string.sub(word, v, v)..string.sub(newString, v + 1, string.len(newString))
  251.     end
  252.    
  253.     return newString
  254. end
  255.  
  256. function getTableSpaces(tab)
  257.     local writeStr = ""
  258.     for i = 1, #tab do
  259.         writeStr = writeStr..tab[i].." "
  260.     end
  261.     return writeStr
  262. end
  263.    
  264.  
  265. function drawHangman(triesLeft)
  266.     local x = termWidth / 2
  267.     local y = (termHeight - 16) / 2
  268.     for line in hangManGfx[triesLeft]:gmatch("[^\r\n]+") do
  269.         term.setCursorPos(x, y)
  270.         write(line)
  271.         y = y + 1
  272.     end
  273. end
  274.  
  275. function confirm(prompt)
  276.     term.setCursorPos(1, termHeight)
  277.     term.clearLine(termHeight)
  278.     write(prompt.." (Y/N)")
  279.     local event, param
  280.     while true do
  281.         event, param = os.pullEvent()
  282.         if event == "char" then
  283.             if string.lower(param) == "y" then
  284.                 return true
  285.             elseif string.lower(param) == "n" then
  286.                 term.clearLine(termHeight)
  287.                 term.setCursorPos(1, termHeight)
  288.                 write("Press Backspace to quit")
  289.                 return false
  290.             end
  291.         end
  292.     end
  293. end
  294.  
  295. function main()
  296.     term.setCursorPos(1, 1)
  297.     term.clear()
  298.     if not setupDictionary() then return end
  299.     while true do
  300.         termWidth, termHeight = term.getSize()
  301.         term.setCursorPos(1, 1)
  302.         term.clear()
  303.         local wordPicked = getWord()
  304.         local underlineWord = obsfucateWord(wordPicked)
  305.         term.setCursorPos(1, termHeight - 1)
  306.         print(underlineWord)
  307.         write("Press Backspace to quit")
  308.         local event, param
  309.         local triesLeft = 11
  310.         local usedLetters = {}
  311.         while true do
  312.             event, param = os.pullEvent()
  313.             if event == "char" then
  314.                 local newObs = removeLetter(wordPicked, underlineWord, param)
  315.                 if not newObs then
  316.                     triesLeft = triesLeft - 1
  317.                     drawHangman(triesLeft)
  318.                     table.insert(usedLetters, string.lower(param))
  319.                     term.setCursorPos(1, termHeight - 2)
  320.                     write("Used Letters: "..getTableSpaces(usedLetters))
  321.                     if triesLeft <= 1 then
  322.                         term.setCursorPos(1, termHeight - 1)
  323.                         write(wordPicked)
  324.                         if not confirm("Game over! Would you like to try again?") then
  325.                             return
  326.                         else
  327.                             break
  328.                         end
  329.                     end
  330.                 else
  331.                     underlineWord = newObs
  332.                     term.setCursorPos(1, termHeight - 1)
  333.                     term.clearLine(termHeight - 1)
  334.                     write(underlineWord)
  335.                    
  336.                     if wordPicked == underlineWord then
  337.                         if not confirm("You won! Would you like to play again?") then
  338.                             return
  339.                         else
  340.                             break
  341.                         end
  342.                     end
  343.                    
  344.                 end
  345.             elseif event == "key" and param == 14 then -- handle Backspace
  346.                 if confirm("Are you sure you want to quit?") then
  347.                     return
  348.                 end
  349.             end  
  350.         end
  351.     end
  352. end
  353.  
  354. main()
  355. term.clear()
  356. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment