Advertisement
Guest User

chathangman

a guest
Jul 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.45 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. term.write("Enter in the secret word:")
  4. term.setCursorPos(1,2)
  5. local word = string.lower(string.match(read(),"%a+"))
  6. local chat = peripheral.wrap("chat_box_2")
  7. local correct = {}
  8. for i = 1,string.len(word),1 do
  9.   correct[i] = false
  10. end
  11. local running = true
  12. local status = ""
  13. local retype = true
  14. local getstring = function(tbl,text)
  15.   local out = ""
  16.   for i = 1,table.maxn(tbl),1 do
  17.     if tbl[i] then
  18.       out = out .. string.sub(text,i,i)
  19.     else
  20.       out = out .. "_"
  21.     end
  22.   end
  23.   return out
  24. end
  25. while running do
  26.   if retype then
  27.     if status ~= "" then
  28.       chat.say(status)
  29.     end
  30.     chat.say("Hangman: " .. getstring(correct,word))
  31.     chat.say("Enter in a letter from a-z to guess the word!")
  32.   end
  33.   retype = false
  34.   local event,player,message = os.pullEvent("chat")
  35.   if string.sub(message,1,1) == message then
  36.     retype = true
  37.     message = string.lower(string.sub(message,string.find(message,"%a")))
  38.     if message then
  39.       for i = 1,table.maxn(correct),1 do
  40.         if message == string.sub(word,i,i) then
  41.           correct[i] = true
  42.         end
  43.       end
  44.     else
  45.       status = "Please enter in a letter (a-z)"
  46.     end
  47.   end
  48.   running = false
  49.   for i = 1,string.len(message),1 do
  50.     if not correct[i] then
  51.       running = true
  52.     end
  53.   end
  54.   if message == word then
  55.     running = false
  56.   end
  57.   if not running then
  58.     chat.say(player .. " won the game!")
  59.   end
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement