yeeeeeeeeeeeee

eeeeeee

Jun 15th, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. -- 20q.lua with extended question count
  2. local treeFile = "tree.db"
  3. local monitor = peripheral.find("monitor") or error("No monitor attached!")
  4. monitor.setTextScale(0.5)
  5.  
  6. local function clear(text)
  7. monitor.clear()
  8. monitor.setCursorPos(1, 1)
  9. for line in text:gmatch("[^\n]+") do
  10. monitor.write(line)
  11. local _, y = monitor.getCursorPos()
  12. monitor.setCursorPos(1, y + 1)
  13. end
  14. end
  15.  
  16. local function loadTree()
  17. if fs.exists(treeFile) then
  18. local f = fs.open(treeFile, "r")
  19. local data = textutils.unserialize(f.readAll())
  20. f.close()
  21. return data
  22. else
  23. return { question = "Is it a living thing?", yes = { guess = "cat" }, no = { guess = "rock" } }
  24. end
  25. end
  26.  
  27. local function saveTree(tree)
  28. local f = fs.open(treeFile, "w")
  29. f.write(textutils.serialize(tree))
  30. f.close()
  31. end
  32.  
  33. local function ask(node, count)
  34. count = count + 1
  35. if count == 21 then
  36. clear("You've hit 20 questions!\nContinue? [y/n]")
  37. if read():lower():sub(1,1) ~= "y" then
  38. clear("Giving up early? Maybe next time.\nPress Enter.")
  39. read()
  40. return node
  41. end
  42. end
  43.  
  44. if node.guess then
  45. clear("Is it a " .. node.guess .. "?\n[y]es / [n]o")
  46. local reply = read()
  47. if reply:lower():sub(1,1) == "y" then
  48. clear("Haha! I got it!\nPress Enter.")
  49. read()
  50. else
  51. clear("Darn! What was it?")
  52. local correct = read()
  53. clear("Give a question to split " .. correct .. " vs. " .. node.guess)
  54. local question = read()
  55. clear("What's the answer for " .. correct .. "? [y/n]")
  56. local answer = read():lower():sub(1,1)
  57.  
  58. local newNode = { question = question }
  59. if answer == "y" then
  60. newNode.yes = { guess = correct }
  61. newNode.no = node
  62. else
  63. newNode.no = { guess = correct }
  64. newNode.yes = node
  65. end
  66. return newNode
  67. end
  68. else
  69. clear(node.question .. "\n[y]es / [n]o")
  70. local reply = read()
  71. if reply:lower():sub(1,1) == "y" then
  72. node.yes = ask(node.yes, count)
  73. else
  74. node.no = ask(node.no, count)
  75. end
  76. end
  77. return node
  78. end
  79.  
  80. while true do
  81. local tree = loadTree()
  82. clear("Think of something...\nPress Enter.")
  83. read()
  84. tree = ask(tree, 0)
  85. saveTree(tree)
  86. clear("Play again? [y/n]")
  87. if read():lower():sub(1,1) ~= "y" then break end
  88. end
  89.  
Advertisement
Add Comment
Please, Sign In to add comment