yeeeeeeeeeeeee

rgwga

Jun 15th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. -- neuroguess.lua
  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 display(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 tree = textutils.unserialize(f.readAll())
  20. f.close()
  21. return tree
  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. display("You've hit 20 questions!\nContinue? [y/n]")
  37. if read():lower():sub(1,1) ~= "y" then
  38. display("Alright, bailing out.\nPress Enter.")
  39. read()
  40. return node
  41. end
  42. end
  43.  
  44. if node.guess then
  45. display("Is it a " .. node.guess .. "?\n[y/n]")
  46. if read():lower():sub(1,1) == "y" then
  47. display("Nailed it! 😎\nPress Enter.")
  48. read()
  49. else
  50. display("Dang! What was it?")
  51. local correct = read()
  52. display("Give a question to separate " .. correct .. " from " .. node.guess)
  53. local question = read()
  54. display("What’s the answer for " .. correct .. "? [y/n]")
  55. local answer = read():lower():sub(1,1)
  56. local newNode = { question = question }
  57.  
  58. if answer == "y" then
  59. newNode.yes = { guess = correct }
  60. newNode.no = node
  61. else
  62. newNode.no = { guess = correct }
  63. newNode.yes = node
  64. end
  65. return newNode
  66. end
  67. else
  68. display(node.question .. "\n[y/n]")
  69. if read():lower():sub(1,1) == "y" then
  70. node.yes = ask(node.yes, count)
  71. else
  72. node.no = ask(node.no, count)
  73. end
  74. end
  75. return node
  76. end
  77.  
  78. while true do
  79. local tree = loadTree()
  80. display("Think of something...\nPress Enter.")
  81. read()
  82. tree = ask(tree, 0)
  83. saveTree(tree)
  84. display("Play again? [y/n]")
  85. if read():lower():sub(1,1) ~= "y" then break end
  86. end
  87.  
Advertisement
Add Comment
Please, Sign In to add comment